{"id":13599934,"url":"https://github.com/probablykasper/cpc","last_synced_at":"2025-04-05T11:09:00.806Z","repository":{"id":45836538,"uuid":"226201955","full_name":"probablykasper/cpc","owner":"probablykasper","description":"Text calculator with support for units and conversion","archived":false,"fork":false,"pushed_at":"2024-01-13T10:08:41.000Z","size":344,"stargazers_count":139,"open_issues_count":2,"forks_count":14,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T10:06:17.079Z","etag":null,"topics":["calculator","cli","conversion","converter","library","math","package","units","units-converter"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/cpc","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/probablykasper.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":["probablykasper"]}},"created_at":"2019-12-05T22:45:04.000Z","updated_at":"2025-01-27T14:14:42.000Z","dependencies_parsed_at":"2023-02-01T02:30:15.705Z","dependency_job_id":"b4d4f47b-14c1-44a2-9ca4-e468665652ba","html_url":"https://github.com/probablykasper/cpc","commit_stats":{"total_commits":201,"total_committers":6,"mean_commits":33.5,"dds":0.08955223880597019,"last_synced_commit":"e4ccc60a1a2a8b7d0756680cb2505cfc812ff811"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/probablykasper%2Fcpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/probablykasper%2Fcpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/probablykasper%2Fcpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/probablykasper%2Fcpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/probablykasper","download_url":"https://codeload.github.com/probablykasper/cpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247325693,"owners_count":20920714,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["calculator","cli","conversion","converter","library","math","package","units","units-converter"],"created_at":"2024-08-01T17:01:19.724Z","updated_at":"2025-04-05T11:09:00.788Z","avatar_url":"https://github.com/probablykasper.png","language":"Rust","readme":"# cpc\n\ncalculation + conversion\n\ncpc parses and evaluates strings of math, with support for units and conversion. 128-bit decimal floating points are used for high accuracy.\n\nIt also lets you mix units, so for example `1 km - 1m` results in `999 Meter`.\n\n[![Crates.io](https://img.shields.io/crates/v/cpc.svg)](https://crates.io/crates/cpc)\n[![Documentation](https://docs.rs/cpc/badge.svg)](https://docs.rs/cpc)\n\n[List of all supported units](https://docs.rs/cpc/latest/cpc/units/enum.Unit.html)\n\n\u003e [!TIP]\n\u003e [fend](https://github.com/printfn/fend) is a great alternative to cpc\n\n## CLI Installation\nInstall using `cargo`:\n```\ncargo install cpc\n```\n\nTo install it manually, grab the appropriate binary from the [GitHub Releases page](https://github.com/probablykasper/cpc/releases) and place it wherever you normally place binaries on your OS.\n\n## CLI Usage\n```\ncpc '2h/3 to min'\n```\n\n## API Installation\nAdd `cpc` as a dependency in `Cargo.toml`.\n\n## API Usage\n\n```rust\nuse cpc::eval;\nuse cpc::units::Unit;\n\nmatch eval(\"3m + 1cm\", true, Unit::Celsius, false) {\n    Ok(answer) =\u003e {\n        // answer: Number { value: 301, unit: Unit::Centimeter }\n        println!(\"Evaluated value: {} {:?}\", answer.value, answer.unit)\n    },\n    Err(e) =\u003e {\n        println!(\"{e}\")\n    }\n}\n```\n\n## Examples\n```\n3 + 4 * 2\n\n8 % 3\n\n(4 + 1)km to light years\n\n10m/2s * 5 trillion s\n\n1 lightyear * 0.001mm in km2\n\n1m/s + 1mi/h in kilometers per h\n\nround(sqrt(2)^4)! liters\n\n10% of abs(sin(pi)) horsepower to watts\n```\n\n## Supported unit types\n- Normal numbers\n- Time\n- Length\n- Area\n- Volume\n- Mass\n- Digital storage (bytes etc)\n- Energy\n- Power\n- Electric current\n- Resistance\n- Voltage\n- Pressure\n- Frequency\n- Speed\n- Temperature\n\n## Accuracy\ncpc 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.\n\n## Performance\nIt'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.\n\nTo see how fast it is, you can pass the `--verbose` flag in CLI, or the `verbose` argument to `eval()`.\n\n## Dev Instructions\n\n### Get started\nInstall [Rust](https://www.rust-lang.org).\n\nRun cpc with a CLI argument as input:\n```\ncargo run -- '100ms to s'\n```\n\nRun in verbose mode, which shows some extra logs:\n```\ncargo run -- '100ms to s' --verbose\n```\n\nRun tests:\n```\ncargo test\n```\n\nBuild:\n```\ncargo build\n```\n\n### Adding a unit\n\nNice resources for adding units:\n- https://github.com/ryantenney/gnu-units/blob/master/units.dat\n- https://support.google.com/websearch/answer/3284611 (unit list)\n- https://translatorscafe.com/unit-converter (unit conversion)\n- https://calculateme.com (unit conversion)\n- https://wikipedia.org\n\n#### 1. Add the unit\nIn `src/units.rs`, units are specified like this:\n```rs\npub enum UnitType {\n  Time,\n  // etc\n}\n\n// ...\n\ncreate_units!(\n  Nanosecond:         (Time, d128!(1)),\n  Microsecond:        (Time, d128!(1000)),\n  // etc\n)\n```\n\nThe 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`.\n\n#### 2. Add a test for the unit\nMake sure to also add a test for each unit. The tests look like this:\n```rs\nassert_eq!(convert_test(1000.0, Meter, Kilometer), 1.0);\n```\nBasically, 1000 Meter == 1 Kilometer.\n\n#### 3. Add the unit to the lexer\nText is turned into tokens (some of which are units) in `lexer.rs`. Here's one example:\n```rs\n// ...\nmatch string {\n  \"h\" | \"hr\" | \"hrs\" | \"hour\" | \"hours\" =\u003e tokens.push(Token::Unit(Hour)),\n  // etc\n}\n// ...\n```\n\n### Potential Improvements\n- Support for conversion between Power, Current, Resistance and Voltage. Multiplication and division is currently supported, but not conversions using sqrt or pow.\n- Move to pure-rust decimal implementation\n  - `rust_decimal`: Only supports numbers up to ~1E+29\n  - `bigdecimal`: Lacking math functions\n- E notation, like 2E+10\n- Unit types\n  - Currency: How to go about dynamically updating the weights?\n    - https://api.exchangerate-api.com/v4/latest/USD\n    - https://www.coingecko.com/en/api\n    - https://developers.coinbase.com/api/v2\n  - Timezones\n  - Binary/octal/decimal/hexadecimal/base32/base64\n  - Fuel consumption\n  - Data transfer rate\n  - Color codes\n  - Force\n  - Roman numerals\n  - Angles\n  - Flow rate\n\n### Releasing a new version\n\n1. Update `CHANGELOG.md`\n2. Bump the version number in `Cargo.toml`\n3. Run `cargo test`\n4. Create a git tag in format `v#.#.#`\n5. Add release notes to the generated GitHub release and publish it\n6. Run `cargo publish`\n","funding_links":["https://github.com/sponsors/probablykasper"],"categories":["Applications","Rust","Libraries","库 Libraries"],"sub_categories":["Text processing","文本处理 Text processing","System tools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprobablykasper%2Fcpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprobablykasper%2Fcpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprobablykasper%2Fcpc/lists"}