Expand description
calculation + conversion
cpc parses and evaluates strings of math, with support for units and conversion. 128-bit decimal floating points are used for high accuracy.
cpc lets you mix units, so for example 1 km - 1m results in Number { value: 999, unit: Meter }.
Check out the list of supported units
§Example usage
use cpc::eval;
use cpc::units::Unit;
match eval("3m + 1cm", true, false) {
Ok(answer) => {
// answer: Number { value: 301, unit: Unit::Centimeter }
println!("Evaluated value: {} {:?}", answer.value, answer.unit)
},
Err(e) => {
println!("{e}")
}
}Modules§
- evaluator
- Turns an
AstNodeinto aNumber - lexer
- Turns a string into
Tokens - parser
- Turns
Tokens into anAstNode - units
- Units, and functions you can use with them
Macros§
Structs§
- Number
- A number with a
Unit.
Enums§
- Constant
- A constant like
PiorE. - Function
Identifier - Functions identifiers like
Sqrt,Sin,Round, etc. - Lexer
Keyword - A temporary enum used by the
lexerto later determine whatTokenit is. - Named
Number - A named number like
Million. - Operator
- Math operators like
Multiply, parentheses, etc. - Text
Operator - A Text operator like
ToorOf. - Token
- A token like a
Number,Operator,Unitetc. - Unary
Operator - Unary operators like
PercentandFactorial.