AncDec is a Rust crate for decimal arithmetic

⚓ Rust    📅 2026-03-06    👤 surdeus    👁️ 3      

surdeus

Anchored Decimal

Floating point type is fast, but not accurate.
With finite bits, binary cannot represent decimal fractions.

Fixed point type is accurate, but not independent.
Conversely, more integer digits means fewer fractional digits.

Rust decimal is comfortable, but still a fixed point decimal.
Internally, an integer with a scale factor and a sign bit.

Anchored decimal is fast, accurate, independent, and comfortable.
Natively, two independent integers, each with nineteen digits.

Benchmarks

All Types vs rust_decimal

Operation AncDec8 AncDec32 AncDec AncDec128 rust_decimal
add 2.8 ns 4.0 ns 6.3 ns 14.5 ns 11.4 ns
sub 3.2 ns 3.9 ns 6.2 ns 14.9 ns 11.4 ns
mul 4.4 ns 4.3 ns 7.4 ns 13.5 ns 11.1 ns
div 3.8 ns 5.5 ns 13.4 ns 20.3 ns 20.5 ns
cmp 1.2 ns 3.0 ns 4.4 ns 8.0 ns 5.1 ns
parse 5.7 ns 10.5 ns 10.8 ns 14.6 ns 10.4 ns

Speedup vs rust_decimal

Operation AncDec8 AncDec32 AncDec AncDec128
add 4.07x 2.85x 1.81x 0.79x
sub 3.56x 2.92x 1.84x 0.77x
mul 2.52x 2.58x 1.50x 0.82x
div 5.39x 3.73x 1.53x 1.01x
cmp 4.25x 1.70x 1.16x 0.64x
parse 1.82x ~1.0x ~1.0x 0.71x

AncDec128 High Precision

Operation AncDec128 rust_decimal Ratio
mul 19.6 ns 18.3 ns 1.07x
div 54.5 ns 55.8 ns 0.98x
parse 34.7 ns 30.3 ns 1.15x

AncDec128 is comparable to rust_decimal while supporting 38+38 digit precision vs rust_decimal's 28 shared digits.

Benchmarked on Intel Core i7-10750H @ 2.60GHz, Rust 1.87.0, release mode

https://crates.io/crates/ancdec

1 post - 1 participant

Read full topic

🏷️ Rust_feed