Skip to main content

Crate ordecimal

Crate ordecimal 

Source
Expand description

§ordecimal

An implementation of the decimalInfinite binary encoding format for arbitrary-precision decimal numbers with order-preserving properties.

This encoding scheme, described in the paper “decimalInfinite: All Decimals In Bits. No Loss. Same Order. Simple.” by Ghislain Fourny, provides:

  • Arbitrary precision: No loss of information for any decimal number
  • Order preservation: Lexicographic comparison of encoded bytes matches numerical comparison
  • Variable length: Compact encoding that scales with the number’s size
  • Simplicity: Straightforward algorithm based on scientific notation (STEM format)

§Use Cases

  • Database indexing (sort keys, range queries)
  • Storing decimals in binary formats while maintaining sort order
  • Efficient range queries on decimal values

§Examples

use ordecimal::Decimal;

// Parse a decimal from string (encodes immediately)
let value: Decimal = "123.456".parse().unwrap();

// Zero-copy access to encoded bytes
let encoded = value.as_bytes();

// Decode from bytes
let decoded = Decimal::from_bytes(encoded).unwrap();

// Order preservation: lexicographic byte comparison = numerical comparison
let a: Decimal = "1.5".parse().unwrap();
let b: Decimal = "2.5".parse().unwrap();
assert!(a < b); // Direct comparison on Decimal works!

§Format Overview

The encoding uses a STEM format (Sign, exponent sign (T), Exponent, Mantissa/significand):

  • S (2 bits): Overall sign (00=negative, 10=positive, also handles special values)
  • T (1 bit): Exponent sign
  • E (variable): Exponent using modified Elias Gamma code
  • M (variable): Significand using tetrades (4 bits) and declets (10 bits per 3 digits)

Structs§

Decimal
Represents a decimal number in pre-encoded STEM format
DecodedDecimal
Decoded decimal with semantic fields (for when field access is needed)

Enums§

DecodeError
Errors that can occur during decoding of decimalInfinite format
EncodeError
Errors that can occur during encoding
SpecialValue
Special decimal values

Type Aliases§

DecodeResult
Result type for decoding operations
EncodeResult
Result type for encoding operations