mathrc 0.1.2

Rust Mathematics Library
Documentation

MathRC

CI Crates.io Docs.rs Downloads Rust

MathRC is a lightweight Rust mathematics library that provides basic mathematical utilities, fraction handling, and a simple arithmetic expression calculator.

Installation

Add MathRC to your Cargo.toml:

[dependencies]

mathrc = "0.1.0"

Or, if you want to use the Git repository directly:

[dependencies]

mathrc = { git = "https://github.com/as7ar/mathrc" }

Usage

Basic arithmetic

use mathrc::math::Math;

fn main() {
let sum = Math::add(10, 5);
let difference = Math::sub(10, 5);
let product = Math::mul(10, 5);
let quotient = Math::div(10, 5);

    println!("sum: {}", sum);
    println!("difference: {}", difference);
    println!("product: {}", product);
    println!("quotient: {}", quotient);
}

Mathematical constants

use mathrc::math::Math;

fn main() {
println!("PI: {}", Math::PI);
println!("E: {}", Math::E);
}

Square root

use mathrc::math::Math;

fn main() {
let result = Math::sqrt(16);

    match result {
        Some(value) => println!("sqrt: {}", value),
        None => println!("cannot calculate square root of a negative number"),
    }
}

Power and logarithm

use mathrc::math::Math;

fn main() {
let powered = Math::pow(2, 3);
let logged = Math::log(8, 2);

    println!("2^3 = {}", powered);

    if let Some(value) = logged {
        println!("log base 2 of 8 = {}", value);
    }
}

GCD and LCM

use mathrc::math::Math;

fn main() {
let gcd = Math::gcd(12, 18);
let lcm = Math::lcm(12, 18);

    println!("gcd: {}", gcd);
    println!("lcm: {}", lcm);
}

Calculator

use mathrc::calculator::Calculator;

fn main() {
let result = Calculator::calc("3 + 2 * 4");

    match result {
        Some(value) => println!("result: {}", value),
        None => println!("invalid expression"),
    }
}

Supported calculator operators:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
(...) Parentheses

The tokenizer also supports the following LaTeX-style operators:

Input Description
\times Multiplication
\div Division

Example:

use mathrc::calculator::Calculator;

fn main() {
let result = Calculator::calc("6 \\div 2");

    assert_eq!(result, Some(3.0));
}

Decimal to fraction

use mathrc::calculator::Calculator;

fn main() {
let fraction = Calculator::dec_to_frac(0.5);

    println!("{}", fraction);
}

Output:

text

1/2

Status

MathRC is currently in early development. APIs may change as the library grows.

License

This project is licensed under the license specified in the LICENSE file.