Skip to content

Latest commit

 

History

History
89 lines (56 loc) · 2.81 KB

File metadata and controls

89 lines (56 loc) · 2.81 KB

🔢    Eval Math Expression

A simple library for evaluating mathematical expressions in Rust.

🔧      Installation

cargo add evalmath

💻    Usage

extern crate evalmath;
use evalmath::calculate;

fn main() {
    let expr = "-6 + 6 * 6";
    let result = calculate!(expr);
    match result {
        Ok(res) => println!("{} = {}",expr, res),
        Err(e) => println!("Error: {}", e),
    }
}

This will output: -6 + 6 * 6 = 36
Note: This example is just a basic example, the library support more advanced mathematical expressions.

Want to see evalmath in action? Simply run cargo run --example main.rs in project root

📚    Supported Operators and Functions

  • The library supports all common mathematical operators such as +, -, *, /, ^, !, etc.
  • It also supports a wide range of mathematical functions like sqrt, log, ln, floor, sin, cos, sinh, cosh, tan, tanh, abs, max, min, var, round, etc...
  • It also supports conversion functions like bin, hex, oct, etc...

You can find a complete list of supported operators and functions in the tokens.rs file, specifically in the TokenType enum.

📝    Notation Support

This library can convert mathematical expressions to a tree, postfix, and prefix notation, and display the results in a user-friendly way.

🚫    Limitations

  • Only support f64 numbers
  • Only support basic mathematical operations, for example does not support adding functions or variables for now

🌳    Algorithms

This library by default use the Shunting Yard algorithm for evaluating. Additionally, it also supports expression tree, Direct Algebraic Logic (DAL) algorithm.

use evalmath::calculate::{calculate, Calculatealgorithm};

let result = calculate(parse!("2^10"), Calculatealgorithm::ExpressionTree);

📐    Angle Mode

Trigonometric functions read a single, global angle unit (radians by default):

use evalmath::calculate::{set_angle_mode, AngleMode};

set_angle_mode(AngleMode::Degree);

🔢    Base Conversion

bin, oct, hex, dec and tobase(value, base) convert a number into another positional base (2-36). Since every result is still a plain f64, digits above 9 (as in hexadecimal) are packed as their two-digit decimal value instead of a letter, e.g. hex(255) evaluates to 1515 (F F15 15).

🤗    Contributions

Got an idea or a fix? I'm all ears! :)

⚠️    Note

Please note that this library is in an early stage of development. Use at your own risk.

📜    License

This project is licensed under the MIT License