decimal-scaled
Docs • Benchmarks • Algorithms • Roadmap • API reference
Const-generic base-10 fixed-point decimals for Rust — bit-exact,
≤ 0.5 ULP correctly-rounded integer-only transcendentals,
deterministic on every platform, no_std-friendly.
Install
[]
= { = "0.4", = ["macros"] }
First use
use ;
let price: D38s12 = "19.99".parse.unwrap;
let qty = from_int;
let total = price * qty; // 59.97 exactly
assert_eq!;
assert_eq!;
// Transcendentals are correctly rounded to ≤ 0.5 ULP, integer-only,
// and bit-identical across platforms.
let sqrt2 = d38!.sqrt_strict;
What it does
Every value is raw × 10^(-SCALE) for a compile-time SCALE. There is
exactly one bit pattern per logical value — no normalisation, no
per-value scale byte, no heap allocation. 0.1 + 0.2 == 0.3 is true,
and so is hash(1.10) == hash(1.1) at the same scale.
Thirteen storage widths from D9 (32-bit, ~9 decimal digits) to
D1232 (4096-bit, ~1232 decimal digits) share an identical API.
Pick the narrowest width that covers your range.
The two guarantees nothing else on crates.io currently combines:
- ≤ 0.5 ULP correctness on every transcendental —
ln/exp/sin/cos/tan/sqrt/cbrt/powf/asin/acos/atan/atan2/sinh/cosh/tanh/asinh/acosh/atanh/to_degrees/to_radianslands within half an ULP of the mathematically exact result, and the bit pattern is identical on every machine. The defaultstrictpath is integer-only andno_std-compatible. - Caller-chosen rounding mode at every lossy operation. The
default is
HalfToEven(IEEE 754 default). Every lossy entry point (*///%, therescalefamily, every strict transcendental) ships a*_with(mode)sibling that takes aRoundingMode:HalfToEven·HalfAwayFromZero·HalfTowardZero·Ceiling·Floor·Trunc. The crate-wide default is also selectable at compile time via therounding-*Cargo features.
Documentation
In-depth guides live under docs/:
- Getting started — constructing values, arithmetic, formatting, parsing.
- The width family —
D9throughD1232, scale aliases, theDecimaltrait, picking a tier. - Conversions — integers, floats, cross-width widening / narrowing, the float bridge.
- Rounding —
RoundingMode, the_withpairs,rescale, the compile-timerounding-*features. - Strict mode — integer-only
*_stricttranscendentals, the ≤ 0.5 ULP guarantee. - The
d*!macros — compile-time decimal literals, scale inference, scientific / radix notation. - Cargo features — every feature flag with what it enables and the common configurations.
- Benchmarks — head-to-head against
bnum,ruint,rust_decimal,fixed,fastnum,dashu-float,bigdecimal, and the fast-vs-strict trade. - Algorithms — every kernel with its citation (Möller–Granlund, Brent, Knuth, Karatsuba, Burnikel–Ziegler, Mercator / Cody–Waite, …).
- Roadmap — what's queued (signed
SCALE, RNG surface, wide-tier perf catch-up, downstream adapter / ecosystem crates). - Changelog — release-by-release notes.
- Contributing — algorithm-library tour, adding a per-(width, scale) override, performance gates, license-compatibility rules.
API reference: https://docs.rs/decimal-scaled/.
License
Licensed under either of:
- MIT license (LICENSES/MIT.md)
- Apache License, Version 2.0 (LICENSES/Apache-2.0.md)
at your option.
Copyright 2026 John Moxley. Third-party code attributions are listed in LICENSE-THIRD-PARTY.