mathengine
A complete mathematical expression evaluator with comprehensive unit conversion support.
Features
- Simple API: One function to evaluate any mathematical expression
- Arithmetic Operations:
+,-,*,/,^with proper precedence - Unit Conversions: Length, temperature, and more dimensions
- Type Safety: Returns proper
Valuetypes, not strings - Comprehensive Errors: Detailed error types for debugging
Quick Start
use ;
Working with Values
The library returns a Value enum that can be pattern matched:
use ;
match evaluate_expression?
Error Handling
The library provides detailed error information:
use ;
match evaluate_expression
Supported Operations
Arithmetic
- Addition:
2 + 3 - Subtraction:
5 - 2 - Multiplication:
3 * 4 - Division:
8 / 2 - Power:
2^3 - Parentheses:
2 * (3 + 4)
Units
- Length:
m,cm,mm,km,ft,in,yd,mi - Temperature:
C,F,K - Conversions:
10m to feet,23C to F
Mixed Operations
10m + 5(adds 5 meters)10ft * 2(multiplies by scalar)100cm - 1m(automatic conversion)
Advanced Usage
For more control, you can use the individual components:
use Lexer;
use Parser;
use evaluate;
// Manual pipeline
let tokens = new.tokenize?;
let ast = new.parse?;
let result = evaluate?;
Crate Structure
This is the main crate that ties together:
mathengine-lexer: Tokenizationmathengine-parser: AST generationmathengine-evaluator: Expression evaluationmathengine-units: Unit conversion system
You can also use these crates individually for more fine-grained control.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.