#math #expression #evaluate #units #convert

bin+lib cpc

evaluates math expressions, with support for units and conversion between units

15 stable releases

1.7.0 Jul 13, 2021
1.5.1 Jun 10, 2021
1.4.0 Mar 15, 2021
1.3.0 Nov 29, 2020

#30 in Math

Download history 63/week @ 2021-04-06 26/week @ 2021-04-13 74/week @ 2021-04-20 7/week @ 2021-04-27 7/week @ 2021-05-04 10/week @ 2021-05-11 20/week @ 2021-05-18 3/week @ 2021-05-25 7/week @ 2021-06-01 43/week @ 2021-06-08 15/week @ 2021-06-15 22/week @ 2021-06-22 45/week @ 2021-06-29 19/week @ 2021-07-06 35/week @ 2021-07-13 89/week @ 2021-07-20

100 downloads per month

MIT license

195KB
3.5K SLoC

cpc

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.

It also lets you mix units, so for example 1 km - 1m results in Number { value: 999, unit: Meter }.

Crates.io Documentation

List of all supported units

CLI Installation

Install using cargo:

cargo install cpc

To install it manually, grab the appropriate binary from the GitHub Releases page and place it wherever you normally place binaries on your OS.

CLI Usage

cpc '2h/3 to min'

API Installation

Add cpc as a dependency in Cargo.toml.

API 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)
    }
}

Examples

3 + 4 * 2

8 % 3

(4 + 1)km to light years

10m/2s * 5 trillion s

1 lightyear * 0.001mm in km2

1m/s + 1mi/h in kilometers per h

round(sqrt(2)^4)! liters

10% of abs(sin(pi)) horsepower to watts

Supported unit types

  • Normal numbers
  • Time
  • Length
  • Area
  • Volume
  • Mass
  • Digital storage (bytes etc)
  • Energy
  • Power
  • Electric current
  • Resistance
  • Voltage
  • Pressure
  • Frequency
  • Speed
  • Temperature

Accuracy

cpc uses 128-bit Decimal Floating Point (d128) numbers instead of Binary Coded Decimals for better accuracy. The result cpc gives will still not always be 100% accurate. I would recommend rounding the result to 20 decimals or less.

Performance

It's pretty fast and scales well. In my case, it usually runs in under 0.1ms. The biggest performance hit is functions like log(). log(12345) evaluates in 0.12ms, and log(e) in 0.25ms.

To see how fast it is, you can pass the --verbose flag in CLI, or the verbose argument to eval().

Dev Instructions

Get started

Install Rust.

Run cpc with a CLI argument as input:

cargo run -- '100ms to s'

Run in verbose mode, which shows some extra logs:

cargo run -- '100ms to s' --verbose

Run tests:

cargo test

Build:

cargo build

Adding a unit

Nice resources for adding units:

1. Add the unit

In src/units.rs, units are specified like this:

pub enum UnitType {
  Time,
  // etc
}

// ...

create_units!(
  Nanosecond:         (Time, d128!(1)),
  Microsecond:        (Time, d128!(1000)),
  // etc
)

The number associated with a unit is it's "weight". For example, if a second's weight is 1, then a minute's weight is 60.

2. Add a test for the unit

Make sure to also add a test for each unit. The tests look like this:

assert_eq!(convert_test(1000.0, Meter, Kilometer), 1.0);

Basically, 1000 Meter == 1 Kilometer.

3. Add the unit to the lexer

Text is turned into tokens (some of which are units) in lexer.rs. Here's one example:

// ...
match string {
  "h" | "hr" | "hrs" | "hour" | "hours" => tokens.push(Token::Unit(Hour)),
  // etc
}
// ...

Potential Improvements

  • Support for conversion between Power, Current, Resistance and Voltage. Multiplication and division is currently supported, but not conversions using sqrt or pow.
  • Move to pure-rust decimal implementation
    • rust_decimal: Only supports numbers up to ~1E+29
    • bigdecimal: Lacking math functions
  • E notation, like 2E+10
  • Unit types

Cross-compiling (from x86_64 macOS)

  1. Install Docker
  2. Install cross. cross works by installing toolchains for whatever target you're building for, then using those toolchains to compile in Docker containers
    cargo install cross
    
  3. Set rustup profile to minimal. This means things like rustdoc won't be included in new toolchain installations. You can run rustup set profile default to reset this afterwards.
    rustup set profile minimal
    
  4. Build for x86_64 macOS, Linux and Windows. For more targets, check out the targets cross supports
    cargo build --release --target x86_64-apple-darwin &&
    cross build --release --target x86_64-unknown-linux-musl &&
    cross build --release --target x86_64-pc-windows-gnu
    
  5. The compiled binaries will now be available inside target/<target>/release/. The filename will be either cpc or cpc.exe.

Releasing a new version

  1. Update CHANGELOG.md
  2. Bump the version number in Cargo.toml
  3. Run cargo test
  4. Cross-compile cpc by following the steps above
  5. Commit and tag in format v#.#.#
  6. Publish on crates.io:
    1. Login by running cargo login and following the instructions
    2. Test publish to ensure there are no issues
      cargo publish --dry-run
      
    3. Publish
      cargo publish
      
  7. Publish on GitHub
    1. Zip the binaries and rename them like cpc-v#.#.#-macos-x64
    2. Create GitHub release with release notes and attach the zipped binaries

Dependencies

~2MB
~32K SLoC