| Crates.io | prexel |
| lib.rs | prexel |
| version | 0.1.9 |
| created_at | 2022-02-02 22:55:49.808436+00 |
| updated_at | 2022-03-16 04:08:49.231242+00 |
| description | A math expression evaluator |
| homepage | https://crates.io/crates/prexel/ |
| repository | https://github.com/Neo-Ciber94/prexel-rs/ |
| max_upload_size | |
| id | 525911 |
| size | 359,668 |
An evaluator for math expressions.
[dependencies]
prexel = "0.1.0"
You can use Evaluator<N> for evaluate an expression.
fn main(){
let evaluator: Evaluator<f64> = Evaluator::new();
println!("{:?}", evaluator.eval("2 + 3 * 5"));
}
This library is not stable and could have breaking changes in any time.
There are 3 steps for evaluating each expression:
Tokenization: A string is converted into an array of tokens.
Conversion: The array of tokens is converted from infix to postfix notation using the Shunting Yard Algorithm.
Evaluation: The resulting RPN (Reverse Polish Notation) expression is evaluated.
This is done using the Tokenizer, Evaluator and Context. The Tokenizer converts an str to Tokens
and the Evaluator process and evaluates the tokens.
The Context is where all the functions, variables, constants and additional information used for evaluation
is stored. You can use the implementation provided by DefaultContext.
Some math functions implemented in prexel::ops::math like trigonometric functions
use internally f64 for the calculations using the traits FromPrimitive and ToPrimitive
what may lead to precision errors.
If you need higher precision make use of the decimal feature to enable a
128 bits decimal number:
use prexel::context::DefaultContext;
use prexel::evaluator::Evaluator;
fn main(){
let context = DefaultContext::new_decimal();
let evaluator = Evaluator::with_context(context);
println!("{:?}", evaluator.eval("Cos(180) * 10!"));
}