Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork13
Text calculator with support for units and conversion
License
probablykasper/cpc
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
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
.
Try it out atcpc.kasper.space
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.
cpc '2h/3 to min'
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
- Normal numbers
- Time
- Length
- Area
- Volume
- Mass
- Digital storage (bytes etc)
- Energy
- Power
- Electric current
- Resistance
- Voltage
- Pressure
- Frequency
- Speed
- Temperature
Addcpc
as a dependency inCargo.toml
.
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}")}}
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.
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
Nice resources for adding units:
- https://github.com/ryantenney/gnu-units/blob/master/units.dat
- https://support.google.com/websearch/answer/3284611 (unit list)
- https://translatorscafe.com/unit-converter (unit conversion)
- https://calculateme.com (unit conversion)
- https://wikipedia.org
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
.
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.
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}// ...
- Support for conversion between Power, Current, Resistance and Voltage. Multiplication and division is currently supported, but not conversions using sqrt or pow.
- E notation, like 2E+10
- Unit types
- Currency: How to go about dynamically updating the weights?
- Timezones
- Binary/octal/decimal/hexadecimal/base32/base64
- Fuel consumption
- Data transfer rate
- Color codes
- Force
- Roman numerals
- Angles
- Flow rate
- Update
CHANGELOG.md
- Bump the version number in
Cargo.toml
- Run
cargo test
- Create a git tag in format
v#.#.#
- Add release notes to the generated GitHub release and publish it
- Run
cargo publish
About
Text calculator with support for units and conversion
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.