Crate cpc[][src]

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, Unit::Celsius, false) {
    Ok(answer) => {
        // answer: Number { value: 301, unit: Unit::Centimeter }
        println!("Evaluated value: {} {:?}", answer.value, answer.unit)
    Err(e) => {
        println!("{}", e)


Turns an AstNode into a Number

Turns a string into Tokens

Turns Tokens into an AstNode

Units, and functions you can use with them



A number with a Unit.


A constant like Pi or E.

Functions identifiers like Sqrt, Sin, Round, etc.

A temporary enum used by the lexer to later determine what Token it is.

A named number like Million.

Math operators like Multiply, parentheses, etc.

A Text operator like To or Of.

A token like a Number, Operator, Unit etc.

Unary operators like Percent and Factorial.


Evaluates a string into a resulting Number.