Skip to main content

evaluate_expression

Function evaluate_expression 

Source
pub fn evaluate_expression(expression: &str) -> Result<f64, CalculatorError>
Expand description

Evaluates an arithmetic expression.

Supports + - * / % // (floor division), ^ and ** (power, right associative), unary signs, parentheses, scientific notation (1.5e3), ×/÷, the constants pi, e and tau, and the functions listed in CalculatorError::UnknownName. A math. prefix (math.sqrt(2)) is accepted, since models often write Python.

use langchain_rust::tools::evaluate_expression;
assert_eq!(evaluate_expression("2 + 3 * 4").unwrap(), 14.0);
assert_eq!(evaluate_expression("2 ^ 3 ^ 2").unwrap(), 512.0);
assert_eq!(evaluate_expression("sqrt(16) + max(1, 5, 3)").unwrap(), 9.0);