Expand description
Functions for handling fixed-point arithmetic.
This module provides constants and functions that enforce a fixed-point precision strategy, ensuring consistent precision and scaling across various types and calculations.
§Raw Value Requirements
When constructing value types like Price or Quantity using from_raw, the raw value
must be a valid multiple of the scale factor for the given precision. Valid raw values
should ideally come from:
- Accessing the
.rawfield of an existing value (e.g.,price.raw) - Using the fixed-point conversion functions in this module
- Values from Nautilus-produced Arrow data
Raw values that are not valid multiples will cause a panic on construction in debug builds, and may result in incorrect values in release builds.
§Legacy Catalog Data and Floating-Point Errors
Data written to catalogs using V2 wranglers before 16th December 2025 may contain raw values with floating-point precision errors. This occurred because the wranglers used:
int(value * FIXED_SCALAR) # Introduces floating-point errorsinstead of the correct precision-aware approach:
round(value * 10^precision) * scale # Correct§Raw Value Correction
To handle legacy data with floating-point errors, the Arrow decode path uses correction
functions (correct_raw_i64, correct_raw_i128, etc.) to round raw values to the
nearest valid multiple. This ensures backward compatibility with existing catalogs.
Note: This correction adds a small amount of overhead during decoding. In a future version, once catalogs have been repaired or migrated, this correction will become opt-in.
Constants§
- FIXED_
PRECISION - The maximum fixed-point precision.
- FIXED_
SCALAR - The scalar value corresponding to the maximum precision (10^16).
- FIXED_
SIZE_ BINARY - The data type name for the Arrow fixed-size binary representation.
- MAX_
FLOAT_ PRECISION - The maximum precision that can be safely used with f64-based constructors.
- PRECISION_
BYTES - The width in bytes for fixed-point value types in high-precision mode (128-bit).
- PRECISION_
DIFF_ SCALAR - The scalar representing the difference between high-precision and standard-precision modes.
Statics§
- HIGH_
PRECISION_ MODE - Indicates if high-precision mode is enabled.
Functions§
- bankers_
round - Rounds a mantissa by removing
excessdecimal digits using banker’s rounding (half to even). - check_
fixed_ precision - Checks if a given
precisionvalue is within the allowed fixed-point precision range. - check_
fixed_ raw_ i64 - Checks that a raw signed fixed-point value (64-bit) has no spurious bits.
- check_
fixed_ raw_ i128 - Checks that a raw signed fixed-point value has no spurious bits beyond the precision scale.
- check_
fixed_ raw_ u64 - Checks that a raw unsigned fixed-point value (64-bit) has no spurious bits.
- check_
fixed_ raw_ u128 - Checks that a raw unsigned fixed-point value has no spurious bits beyond the precision scale.
- correct_
price_ raw - Rounds a raw price value to the nearest valid multiple of the scale for the given precision.
- correct_
quantity_ raw - Rounds a raw quantity value to the nearest valid multiple of the scale for the given precision.
- correct_
raw_ i64 - Rounds a raw
i64value to the nearest valid multiple of the scale for the given precision. - correct_
raw_ i128 - Rounds a raw
i128value to the nearest valid multiple of the scale for the given precision. - correct_
raw_ u64 - Rounds a raw
u64value to the nearest valid multiple of the scale for the given precision. - correct_
raw_ u128 - Rounds a raw
u128value to the nearest valid multiple of the scale for the given precision. - f64_
to_ fixed_ i64 - Converts an
f64value to a raw fixed-pointi64representation with a specified precision. - f64_
to_ fixed_ i128 - Converts an
f64value to a raw fixed-pointi128representation with a specified precision. - f64_
to_ fixed_ u64 - Converts an
f64value to a raw fixed-pointu64representation with a specified precision. - f64_
to_ fixed_ u128 - Converts an
f64value to a raw fixed-pointu128representation with a specified precision. - fixed_
i64_ to_ f64 - Converts a raw fixed-point
i64value back to anf64value. - fixed_
i128_ to_ f64 - Converts a raw fixed-point
i128value back to anf64value. - fixed_
u64_ to_ f64 - Converts a raw fixed-point
u64value back to anf64value. - fixed_
u128_ to_ f64 - Converts a raw fixed-point
u128value back to anf64value. - mantissa_
exponent_ to_ fixed_ i128 - Converts a mantissa/exponent pair to a raw fixed-point
i128value at the given precision. - raw_
scales_ match - Returns
truewhen two precisions encode theirrawvalues at the same scale.