Version 3 - mainnet - 0xa4...c97d1a
Readme
Versions
3
Dependencies
0
Dependents
0
Analytics
On the way
openzeppelin_fp_math[!WARNING] This package is open-source and not actively maintained.
The
UpgradeCapfor packages published to the Move Registry under@openzeppelin-move/*has been made immutable, so these packages can no longer be upgraded by OpenZeppelin or anyone else.
Fixed-point decimal types with 9 decimals (10^9), matching Sui coin precision.
[dependencies]
openzeppelin_fp_math = { r.mvr = "@openzeppelin-move/fixed-point-math" }
[!NOTE]
openzeppelin_fp_mathdepends onopenzeppelin_mathfor rounding modes and widened integer arithmetic.
UD30x9: Unsigned decimal fixed-point (internal: 0 to 2^128 - 1; decimal: 0 to ~3.4e29)SD29x9: Signed decimal fixed-point (two's complement, internal: -2^127 to 2^127 - 1; decimal: ~-1.7e29 to ~1.7e29)add, sub, mul, mul_trunc, mul_away, div, div_trunc, div_away, pow, unchecked_add, unchecked_sub, mod, sqrtceil, floor (round to whole units)SD29x9 also exposes sign and truncated-remainder helpers: abs, negate, remlog2, ln, log10cdf (standard-normal CDF Φ), pdf (standard-normal PDF φ), inverse_cdf (standard-normal quantile Φ⁻¹)eq, neq, gt, gte, lt, lte, is_zeroUD30x9 also exposes bitwise helpers: and, and2, or, xor, not, lshift, rshift, unchecked_lshift, unchecked_rshiftThis package uses casting and converting for different operations:
10^9.10^9 scale factor.Rule of thumb:
42 and you
mean 42.0.The core wrap / unwrap APIs are raw casts. They preserve the
underlying fixed-point representation and do not multiply or divide by 10^9.
u128 -> UD30x9: into_UD30x9UD30x9 -> SD29x9: into_SD29x9, try_into_SD29x9SD29x9 -> UD30x9: into_UD30x9, try_into_UD30x9zero, one, max, wrapSD29x9 only: min, from_bitsuse openzeppelin_fp_math::sd29x9;
use openzeppelin_fp_math::ud30x9;
let one = ud30x9::wrap(1_000_000_000); // 1.0
let raw = ud30x9::wrap(42); // Raw bits, not 42.0
let positive = sd29x9::wrap(1_000_000_000, false); // 1.0
let negative = sd29x9::wrap(42, true); // Raw bits for -0.000000042
Casting also includes moves between fixed-point types that keep the same scaled numeric meaning and only validate signedness or range.
use openzeppelin_fp_math::{sd29x9, ud30x9_convert};
let unsigned = ud30x9_convert::from_u128(42); // 42.0
let signed = unsigned.into_SD29x9(); // 42.0 as SD29x9
let roundtrip = signed.into_UD30x9(); // 42.0 as UD30x9
These casts do not rescale the value. For example, 42.123456789 stays
42.123456789; only the target type changes.
Use the conversion modules when you want semantic integer conversions that apply the fixed-point scale for you.
use openzeppelin_fp_math::{sd29x9_convert, ud30x9_convert};
let whole = ud30x9_convert::from_u128(42); // 42.0
let back = whole.to_u128_trunc(); // 42
let delta = sd29x9_convert::from_u128(5, true); // -5.0
let (magnitude, is_negative) = delta.to_parts_trunc();
// magnitude == 5, is_negative == true
Because Move does not provide a native signed integer type for this package,
SD29x9 conversions use an unsigned magnitude plus a sign flag instead of a
single i128-style input or output.
log2, ln, and log10 are computed from a shared log2 kernel; ln and
log10 apply a base-conversion factor on top.
0 < x < 1 because the fixed-point logarithm would be
negative; UD30x9 also aborts for x == 0 because the log operation is
undefined/invalid. Rounds down.x <= 0. Rounds toward zero, matching mul_trunc,
div_trunc, and pow in the same module.log10 is exact on integer powers of ten (including sub-unit 10^-k on
SD29x9): log10(10^k) == k * SCALE.
use openzeppelin_fp_math::{sd29x9, ud30x9_convert};
let two = ud30x9_convert::from_u128(2);
let _ = two.log2(); // 1.0
let _ = two.ln(); // 0.693147180
let _ = two.log10(); // 0.301029995
let half = sd29x9::wrap(500_000_000, false); // 0.5
let _ = half.log2(); // -1.0
Φ(z) is the standard-normal cumulative distribution function: the probability
that a standard-normal draw lands at or below z (the area under the bell curve
to the left of z). It is a building block for options pricing, risk models,
and sampling. Both fixed-point types expose it:
UD30x9::cdf takes non-negative z and returns Φ(z) ∈ [0.5, 1].SD29x9::cdf takes signed z and returns Φ(z) ∈ [0, 1].Properties:
≤ 5 × 10⁻⁹ (5 ULP at the 10⁹ scale);
empirical worst case ~5 × 10⁻¹⁰.|z| ≤ 6.109410205; beyond that the result
saturates.0 for z ≤ -6.109410205 (SD29x9) and exactly 1 for
z ≥ 6.109410205 - the analytical points where Φ rounds to the endpoint at
10⁻⁹ resolution.0.5.z ≥ 4, where the per-step increment is smallest).cdf(z) + cdf(z.negate()) is exactly 1 for every SD29x9
input except min(), whose negation is not representable.use openzeppelin_fp_math::sd29x9;
use openzeppelin_fp_math::ud30x9;
let z = ud30x9::wrap(1_000_000_000); // 1.0
let p = z.cdf(); // 0.841344746 (P(Z ≤ 1))
let neg = sd29x9::wrap(1_000_000_000, true); // -1.0
let q = neg.cdf(); // 0.158655254 (P(Z ≤ -1))
Limitations: the approximation is defined on |z| ≤ 6.109410205 and saturates
outside that range; 10⁻⁹ is the finest distinction the output can represent.
There is no floating point - results are exact fixed-point integer arithmetic.
φ(z) is the standard-normal probability density function
e^(-z^2/2) / sqrt(2*pi): the height of the bell curve at z, and the
derivative of Φ. It appears in options Greeks (gamma, vega), maximum-likelihood
objectives, and density estimation. Both fixed-point types expose it:
UD30x9::pdf takes non-negative z and returns φ(z) ∈ [0, φ(0)].SD29x9::pdf takes signed z and returns φ(z) ∈ [0, φ(0)] (always non-negative).Properties:
≤ 5 × 10⁻⁹ (5 ULP at the 10⁹ scale);
empirical worst case ~5 × 10⁻¹⁰.|z| ≤ 6.402729806; beyond that the result
saturates to 0.φ(0) = 0.398942280 (1/sqrt(2*pi)), returned exactly.|z| between every pair of adjacent
representable inputs (no 1-ULP tail inversions); the codegen gate confirms this
exhaustively over the at-risk tail (|z| ≥ 4).pdf(z) equals pdf(z.negate()) for every SD29x9
input except min(), whose negation is not representable.use openzeppelin_fp_math::sd29x9;
use openzeppelin_fp_math::ud30x9;
let z = ud30x9::wrap(1_000_000_000); // 1.0
let d = z.pdf(); // 0.241970725 (height of the bell curve at z = 1)
let neg = sd29x9::wrap(1_000_000_000, true); // -1.0
let e = neg.pdf(); // 0.241970725 (φ is even)
Limitations: the approximation is defined on |z| ≤ 6.402729806 and saturates to
0 outside that range; 10⁻⁹ is the finest distinction the output can represent.
Φ⁻¹(p) is the standard-normal quantile function (a.k.a. probit): the value z
such that Φ(z) = p. It inverts cdf, turning a probability into a z-score -
confidence-interval bounds, Value-at-Risk thresholds, and inverse-transform
sampling. Both fixed-point types expose it:
UD30x9::inverse_cdf takes p ∈ [0.5, 1] and returns z ∈ [0, 6.109410205]
(the non-negative half; an unsigned type cannot represent a negative quantile).SD29x9::inverse_cdf takes p ∈ [0, 1] and returns the signed
z ∈ [-6.109410205, 6.109410205].Properties:
≤ 5 × 10⁻⁹ (5 ULP at the 10⁹ scale);
the deterministic validation grid found no result more than 1 ULP from the
correctly rounded output.0.0.5 - inverse_cdf(p) = inverse_cdf(1 - p).negate().p = 1 returns +6.109410205 and p = 0 returns
-6.109410205 (Φ⁻¹ is ±∞ there and unrepresentable); the clamp equals the
CDF saturation bound, so cdf maps the clamped values back to exactly 1 and
0.cdf/pdf, the quantile has invalid inputs. inverse_cdf
aborts if p ∉ [0, 1]; the UD30x9 variant additionally aborts if p < 0.5,
whose quantile would be negative.cdf(inverse_cdf(p)) recovers p to within a few ULP.p = 1 the quantile is intrinsically steep - the two
largest representable inputs differ by ≈ 0.11 in z - so a 1-ULP change in
p maps to a large change in z. This is a property of Φ⁻¹, not the
approximation.use openzeppelin_fp_math::sd29x9;
use openzeppelin_fp_math::ud30x9;
let p = ud30x9::wrap(975_000_000); // 0.975
let z = p.inverse_cdf(); // ≈ 1.959963985 (the 1.96 of a 95% confidence interval)
let lower = sd29x9::wrap(25_000_000, false); // 0.025
let zl = lower.inverse_cdf(); // ≈ -1.959963985
Limitations: p must be a valid probability (see Aborts); the output range is
|z| ≤ 6.109410205 and 10⁻⁹ is the finest distinction it can represent. In
the deep tail the achievable z-resolution is bounded by the 10⁻⁹ resolution
of the input p, not by the approximation. The public input and output remain
at 10⁹; the tail carries ln and sqrt intermediates at 10¹⁸ to avoid
losing precision before the final rounding. inverse_cdf is therefore slightly
heavier than cdf/pdf.
use openzeppelin_fp_math::{sd29x9, sd29x9_convert, ud30x9, ud30x9_convert};
let price1 = ud30x9_convert::from_u128(1).add(ud30x9::wrap(500_000_000)); // 1.5
let price2 = ud30x9_convert::from_u128(2); // 2.0
let total = price1.add(price2); // 3.5
let balance = sd29x9_convert::from_u128(10, false); // 10.0
let adjustment = sd29x9_convert::from_u128(2, true).add(sd29x9::wrap(500_000_000, true)); // -2.5
let new_balance = balance.add(adjustment); // 7.5
let one = ud30x9::wrap(1000000000); // 1.0
let third_down = one.div_trunc(ud30x9::wrap(3000000000)); // 0.333333333
let third_up = one.div_away(ud30x9::wrap(3000000000)); // 0.333333334
[!Warning] These are unaudited illustrations of how the primitives can be integrated, not production-ready code.
Complete, compilable integration examples live in examples/:
ud30x9 - a fixed-rate compound-interest / APY calculator: build a fractional rate from integers (from_u64 + div), compound with pow, apply to a scaled principal with mul, and convert back to coin units with to_u64_trunc. Exact, not approximate.sd29x9 - a downside-risk helper: a signed z-score (value - mean) / stddev (frequently negative) fed through the standard-normal cdf for a tail probability, demonstrating sub / div sign propagation, negate (via the Φ(z) + Φ(-z) = 1 symmetry), is_negative, and narrowing to UD30x9.The standard-normal CDF (cdf), PDF (pdf), and quantile (inverse_cdf) are
backed by fixed-degree rational approximations seeded by AAA and refined for
fixed-point rounding. Their coefficients and test vectors are generated offline
and must not be hand-edited (each carries an AUTO-GENERATED banner):
sources/internal/cdf_coefficients.movesources/internal/pdf_coefficients.movesources/internal/inverse_cdf_coefficients.movetests/{sd29x9_tests,ud30x9_tests}/cdf_test_vectors.movetests/{sd29x9_tests,ud30x9_tests}/pdf_test_vectors.movetests/{sd29x9_tests,ud30x9_tests}/inverse_cdf_test_vectors.moveTo regenerate them - or to re-validate the committed coefficients against their
reference oracles - see scripts/gaussian_codegen/.
llms.txt: discovery entry point for AI integratorsYou can install this package in your Move project by calling
mvr add @openzeppelin-move/fixed-point-mathFixed-point types with 9 decimals (10^9), matching Sui coin precision.