Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Text calculator with support for units and conversion

License

NotificationsYou must be signed in to change notification settings

probablykasper/cpc

Repository files navigation

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 example1 km - 1m results in999 Meter.

Crates.ioDocumentation

List of all supported units

Try it out atcpc.kasper.space

CLI Installation

Install usingcargo:

cargo install cpc

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

CLI Usage

cpc '2h/3 to min'

Examples

3 + 4 * 28 % 3(4 + 1)km to light years10m/2s * 5 trillion s1 lightyear * 0.001mm in km21m/s + 1mi/h in kilometers per hround(sqrt(2)^4)! liters10% 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

API Installation

Addcpc as a dependency inCargo.toml.

API Usage

use cpc::eval;use cpc::units::Unit;matcheval("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}")}}

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.

Dev Instructions

Get started

InstallRust.

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

Insrc/units.rs, units are specified like this:

pubenumUnitType{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 is1, then a minute's weight is60.

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) inlexer.rs. Here's one example:

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

Potential Improvements

Releasing a new version

  1. UpdateCHANGELOG.md
  2. Bump the version number inCargo.toml
  3. Runcargo test
  4. Create a git tag in formatv#.#.#
  5. Add release notes to the generated GitHub release and publish it
  6. Runcargo publish

About

Text calculator with support for units and conversion

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors7

Languages


[8]ページ先頭

©2009-2025 Movatter.jp