A simple library for evaluating mathematical expressions in Rust.
cargo add evalmathextern 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
- 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.
This library can convert mathematical expressions to a tree, postfix, and prefix notation, and display the results in a user-friendly way.
- Only support f64 numbers
- Only support basic mathematical operations, for example does not support adding functions or variables for now
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);Trigonometric functions read a single, global angle unit (radians by default):
use evalmath::calculate::{set_angle_mode, AngleMode};
set_angle_mode(AngleMode::Degree);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 F → 15 15).
Got an idea or a fix? I'm all ears! :)
Please note that this library is in an early stage of development. Use at your own risk.
This project is licensed under the MIT License