Expand description
§decimal-bytes
Arbitrary precision decimals with lexicographically sortable byte encoding.
This crate provides a Decimal type that stores decimal numbers as bytes
in a format that preserves numerical ordering when compared lexicographically.
This makes it ideal for use in databases and search engines where efficient
range queries on decimal values are needed.
§Features
- Bytes-first storage: The primary representation is a compact byte array
- Lexicographic ordering: Byte comparison matches numerical comparison
- Arbitrary precision: Supports up to 131,072 digits before and 16,383 after decimal
- PostgreSQL NUMERIC compatibility: Full support for precision, scale, and special values
- Special values: Infinity, -Infinity, and NaN with correct PostgreSQL sort order
§Example
use decimal_bytes::Decimal;
use std::str::FromStr;
// Create decimals
let a = Decimal::from_str("123.456").unwrap();
let b = Decimal::from_str("123.457").unwrap();
// Byte comparison matches numerical comparison
assert!(a.as_bytes() < b.as_bytes());
assert!(a < b);
// Display the value
assert_eq!(a.to_string(), "123.456");
// Special values (PostgreSQL compatible)
let inf = Decimal::infinity();
let nan = Decimal::nan();
assert!(a < inf);
assert!(inf < nan);§Sort Order
The lexicographic byte order matches PostgreSQL NUMERIC:
-Infinity < negative numbers < zero < positive numbers < +Infinity < NaN§Special Value Semantics (PostgreSQL vs IEEE 754)
This library follows PostgreSQL semantics for special values, which differ from IEEE 754 floating-point:
| Behavior | PostgreSQL / decimal-bytes | IEEE 754 float |
|---|---|---|
NaN == NaN | true | false |
NaN ordering | Greatest value (> Infinity) | Unordered |
Infinity == Infinity | true | true |
use decimal_bytes::Decimal;
let nan1 = Decimal::nan();
let nan2 = Decimal::nan();
let inf = Decimal::infinity();
// NaN equals itself (PostgreSQL behavior, unlike IEEE 754)
assert_eq!(nan1, nan2);
// NaN is greater than everything, including Infinity
assert!(nan1 > inf);This makes Decimal suitable for use in indexes, sorting, and deduplication
where consistent ordering and equality semantics are required.
Structs§
- Decimal
- An arbitrary precision decimal number stored as sortable bytes.
Enums§
- Decimal
Error - Errors that can occur during decimal encoding/decoding.
- Special
Value - Special decimal values (IEEE 754 / PostgreSQL compatible)