Skip to main content

Crate tcal_rs

Crate tcal_rs 

Source
Expand description

§tcal_rs - Number Theory & Calculator Library

A comprehensive Rust library for number theory operations and mathematical calculations, ported from libqalculate.

§Overview

This library provides:

§Number Theory Operations

  • GCD/LCM: Greatest common divisor and least common multiple
  • Modular Arithmetic: Remainder, modulo, modular exponentiation
  • Prime Numbers: Primality testing, generation, counting
  • Euler’s Totient: φ(n) function implementation
  • Bernoulli Numbers: Rational sequence computation
  • Rounding: Nine standard rounding modes
  • Parity: Even/odd checking

§Calculator Engine

  • Expression Evaluation: Parse and compute mathematical expressions
  • Built-in Functions: Trigonometry, logarithms, roots
  • Variable Storage: Assign and use variables
  • Multiple Number Bases: Hex, binary, octal support

§Quick Start

§Number Theory

use tcal_rs::*;

// GCD and LCM
assert_eq!(gcd(48, 18), 6);
assert_eq!(lcm(21, 6), 42);

// Modular arithmetic
assert_eq!(powmod(4, 13, 497), 445);

// Prime operations
assert!(is_prime(7919));  // 1000th prime
assert_eq!(next_prime(100), 101);

// Euler's totient
assert_eq!(totient(30), 8);

// Rounding
use tcal_rs::RoundingMode;
assert_eq!(round(5, 2, RoundingMode::HalfToEven), 2);

§Calculator

use tcal_rs::calculator::engine::Engine;

let mut engine = Engine::new();

// Basic arithmetic
assert_eq!(engine.eval("2 + 2").unwrap(), "4");

// Trigonometry
assert_eq!(engine.eval("sin(pi/2)").unwrap(), "1");

// Number theory
assert_eq!(engine.eval("totient(30)").unwrap(), "8");

// Variables
engine.eval("x = 5").unwrap();
assert_eq!(engine.eval("x * 2 + 3").unwrap(), "13");

§Module Organization

tcal_rs/
├── number_theory/    # Core number theory functions
│   ├── parity.rs    # Even/odd checking
│   ├── primes.rs    # Prime operations
│   ├── rounding.rs  # Rounding modes
│   ├── totient.rs   # Euler's totient
│   └── traits.rs    # Extension traits
├── calculator/      # Expression calculator
│   ├── lexer.rs     # Tokenization
│   ├── parser.rs    # AST generation
│   ├── evaluator.rs # Expression evaluation
│   └── engine.rs    # Main calculator engine
└── fprice.rs        # Price formatting

Re-exports§

pub use number_theory::parity::is_even;
pub use number_theory::parity::is_odd;
pub use number_theory::primes::bernoulli;
pub use number_theory::primes::is_prime;
pub use number_theory::primes::next_prime;
pub use number_theory::primes::nth_prime;
pub use number_theory::primes::prev_prime;
pub use number_theory::primes::prime_count;
pub use number_theory::primes::primes_up_to;
pub use number_theory::rounding::RoundingMode;
pub use number_theory::rounding::abs;
pub use number_theory::rounding::abs_integer;
pub use number_theory::rounding::ceil;
pub use number_theory::rounding::floor;
pub use number_theory::rounding::round;
pub use number_theory::rounding::signum;
pub use number_theory::rounding::signum_integer;
pub use number_theory::rounding::trunc;
pub use number_theory::totient::totient;
pub use number_theory::denominator;
pub use number_theory::frac;
pub use number_theory::gcd;
pub use number_theory::lcm;
pub use number_theory::modulo;
pub use number_theory::numerator;
pub use number_theory::powmod;
pub use number_theory::rem;
pub use number_theory::traits::Divisors;
pub use number_theory::traits::Gcd;
pub use number_theory::traits::Lcm;

Modules§

calculator
fprice
number_theory
Number Theory Functions Module