| Crates.io | mathengine-evaluator |
| lib.rs | mathengine-evaluator |
| version | 0.1.0 |
| created_at | 2025-10-06 23:48:51.120299+00 |
| updated_at | 2025-10-06 23:48:51.120299+00 |
| description | Type-safe evaluation engine for mathematical expressions with comprehensive unit conversion |
| homepage | https://github.com/javif89/mathengine |
| repository | https://github.com/javif89/mathengine |
| max_upload_size | |
| id | 1871006 |
| size | 13,617 |
A type-safe evaluation engine for mathematical expressions with comprehensive unit conversion support.
use mathengine_lexer::Lexer;
use mathengine_parser::Parser;
use mathengine_evaluator::evaluate;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = "10m + 5ft to inches";
// Parse the expression
let tokens = Lexer::new(input).tokenize()?;
let ast = Parser::new(tokens).parse()?;
// Evaluate
let result = evaluate(&ast)?;
println!("Result: {}", result); // Result: 196.85in
Ok(())
}
2 + 3, 10m + 5ft5 - 2, 100cm - 1m3 * 4, 10m * 28 / 2, 20ft / 42^3, 3^210m to feet, 5mi to km23C to F, 300K to celsius10m + 5 (adds 5 meters)2 * 10kg (multiplies unit by scalar)Comprehensive error types for robust applications:
use mathengine_evaluator::{evaluate, EvalError};
match evaluate(&ast) {
Ok(result) => println!("Success: {}", result),
Err(EvalError::DivisionByZero) => {
println!("Cannot divide by zero");
}
Err(EvalError::IncompatibleUnits { left_unit, right_unit, operation }) => {
println!("Cannot {} {} and {}", operation, left_unit, right_unit);
}
Err(EvalError::UnknownUnit { unit }) => {
println!("Unknown unit: '{}'", unit);
}
Err(e) => println!("Evaluation error: {}", e),
}
Works seamlessly with the mathengine type system:
use mathengine_parser::types::{Value, Number, UnitValue};
let result = evaluate(&ast)?;
match result {
Value::Number(n) => println!("Pure number: {}", n),
Value::UnitValue(uv) => println!("Value with unit: {}", uv),
}
Operations are evaluated according to mathematical precedence rules, as determined by the parser.
10m + 5ft (converts to common unit)10m + 5kg ❌ (error)10m * 2 ✅ (multiplies by scalar)to operatorPart of the mathengine workspace. This crate provides the evaluation engine that computes final results from parsed ASTs, handling all mathematical operations and unit conversions.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.