Expand description
mathematical expression evaluator.
How to Use
use mexe::eval;
fn main() {
let forty_six = eval("(5 * 8) + 6").unwrap();
let two = eval("1 + 1").unwrap();
println!("{} & {}", forty_six, two);
assert_eq!(forty_six, 46.0);
assert_eq!(two, 2.0);
}Note: the above assert_eqs work, but for float comparison in general use a
crate such as float-cmp.
Why?
If you need to evaluate simple arithmetic expressions, this crate offers a fast and lightweight solution.
In our current benchmarks,
it’s about 4-10x faster than meval, about 2x faster than fasteval, and
the fully-featured evalexpr is generally the slowest. Note that those crates
do much more than mexe – especially evalexpr. Our focus on a very small
problem makes it easier for us to ship a fast and lean library.
Includes
- sum
- subtraction
- multiplication
- division
- integers
- floats
- parentheses
- arbitrary whitespace
Goals
- Minimal
- Fast: O(n)
- No allocations if possible
- We can assume the input is ASCII, and throw an error otherwise
- Thoroughly tested
- Maybe try to make it no-std
Grammar
See readme.
Grammar idea adapted from this post.
Our first implementation uses an LL(1) parser.
Similar Projects
Links
Enums
Represents any errors that may occur in this library
Functions
Evaluates a numeric expression.
Evaluates a numeric expression assuming it is just one operation between
two numbers, without parentheses. Whitespace is ignored.
Floating point numbers must be represented in the X.Y form, where X and
Y are non-empty sequence of digits. The notation with the exponent is not
currently supported.
Type Definitions
Represents the result of any fallible operation in this library