#decimal #decimal-number

no-std fdecimal

Arbitrary precision fast decimal numbers

2 releases

0.0.2 Oct 21, 2024
0.0.1 Oct 21, 2024

#1599 in Math

Download history 269/week @ 2024-10-21

269 downloads per month

MIT license

5KB

fdecimal

Arbitrary-precision fast decimal numbers implemented in pure Rust.


lib.rs:

Decimal

Decimal allows storing real number to arbitrary precision; which avoids common floating point errors (such as 0.1 + 0.2 ≠ 0.3) at the cost of complexity.

Internally, Decimal uses a 256-bit integer, paired with a 64-bit integer which determines the position of the decimal point. Therefore, the precision is not actually arbitrary, but limited to 263 decimal places.

Common numerical operations are overloaded, so we can treat them the same way we treat other numbers.

It is not recommended to convert a floating point number to a decimal directly, as the floating point representation may be unexpected.

Example

use fdecimal::Decimal;
use std::str::FromStr;

let input = "0.8";
let dec = Decimal::from_str(&input).unwrap();
let float = f32::from_str(&input).unwrap();

println!("Input ({}) with decimals: {} vs {})", input, dec, float);

No runtime deps