Crates.io | evaluatorrs |
lib.rs | evaluatorrs |
version | 0.0.1 |
source | src |
created_at | 2023-09-14 18:49:05.23632 |
updated_at | 2023-09-14 18:49:05.23632 |
description | Tools for runtime evaluation of mathematical expressions |
homepage | |
repository | https://github.com/Rosdf/evaluatorrs |
max_upload_size | |
id | 972982 |
size | 106,339 |
This library provides ways to evaluate mathematical expressions at runtime.
std
(default): enables use of std librarylibm
: enables use of mathematical functions from libm, useful for no_std crates and non-standard functionsuse evaluatorrs::eval;
use evaluatorrs::formulas::ParserError;
fn evaluate() -> Result<(), ParserError> {
let expression = "1 + 2";
let result = eval(expression)?;
debug_assert_eq!(result, 3.0);
Ok(())
}
use evaluatorrs::formulas::{Evaluate, RootFormula};
use evaluatorrs::function_stores::EmptyFunctionStore;
use evaluatorrs::variable_stores::{HashMapVariableStore, SetVariable};
fn example() -> Result<(), ParserError> {
let formula = RootFormula::parse("a + b", &EmptyFunctionStore)?;
let mut variable_store = HashMapVariableStore::new();
variable_store.set("a", RootFormula::parse("1", &EmptyFunctionStore)?);
variable_store.set("b", RootFormula::parse("10", &EmptyFunctionStore)?);
let evaluated = formula.eval(&variable_store);
assert!(evaluated.is_ok());
let evaluated = evaluated?;
assert_eq!(evaluated, 11.0);
}