pub struct UnsignedNumeric {
pub value: InnerUint,
}Expand description
A UnsignedNumeric is an unsigned 192-bit fixed-point number with 18 decimal places of
precision.
§Internal Representation
Internally, the value is stored as a InnerUint, which wraps a little-endian array [u64; 3].
This means the layout is:
InnerUint([lo, mid, hi])
// equivalent to:
// value = lo + (mid << 64) + (hi << 128)Each component contributes to the full 192-bit value:
value = (hi × 2^128) + (mid × 2^64) + lo§Fixed-Point Scaling
All values are scaled by ONE (10^18). That is, the internal number is interpreted
as raw / ONE to recover its real-world value.
Examples:
InnerUint([1_000_000_000_000_000_000, 0, 0])→ 1.0InnerUint([500_000_000_000_000_000, 0, 0])→ 0.5InnerUint([2_000_000_000_000_000_000, 0, 0])→ 2.0
§Example: High-Bit Usage
When you write:
let a = UnsignedNumeric::from([0, 0, 1]);This initializes the internal 192-bit value with the array [0, 0, 1].
In this representation:
0is the least significant 64 bits (lo)0is the middle 64 bits (mid)1is the most significant 64 bits (hi)
The actual 192-bit value is computed as:
value = (1 × 2^128) + (0 × 2^64) + 0 = 2^128
= 340282366920938463463374607431768211456Since this is a fixed-point number, the real-world value is:
real_value = value / 10^18 = 340282366920938463463.374607431768211456This system allows for both extremely high precision and a vast dynamic range,
making UnsignedNumeric ideal for financial, scientific, or blockchain applications
where f64 or even u128 would lose accuracy or overflow.
Fields§
§value: InnerUintInternal value stored as a 192-bit integer, scaled by ONE (10^18).
Implementations§
Source§impl UnsignedNumeric
impl UnsignedNumeric
Sourcepub fn new(value: u128) -> Self
pub fn new(value: u128) -> Self
Constructs a UnsignedNumeric from an integer value by scaling it by ONE (10^18).
For example, new(7) produces 7.0.
Sourcepub fn from_scaled_u128(value: u128) -> Self
pub fn from_scaled_u128(value: u128) -> Self
Constructs a UnsignedNumeric from a u128 that is already scaled by ONE (i.e. in fixed-point space).
This bypasses internal multiplication and is useful for constants or pre-scaled data.
Sourcepub fn from_values(lo: u64, mid: u64, hi: u64) -> Self
pub fn from_values(lo: u64, mid: u64, hi: u64) -> Self
Constructs a UnsignedNumeric directly from a raw [u64; 3] value.
The input is interpreted as already scaled (fixed-point).
Layout is little-endian: [lo, mid, hi] = lo + (mid << 64) + (hi << 128).
Sourcepub fn to_bytes(&self) -> [u8; 24]
pub fn to_bytes(&self) -> [u8; 24]
Converts this UnsignedNumeric into a raw [u8; 24] representation.
Sourcepub fn from_bytes(bytes: &[u8; 24]) -> Self
pub fn from_bytes(bytes: &[u8; 24]) -> Self
Converts a raw [u8; 24] representation into a UnsignedNumeric.
Sourcepub fn to_imprecise(&self) -> Option<u128>
pub fn to_imprecise(&self) -> Option<u128>
Converts this UnsignedNumeric into a regular u128 by dividing by ONE.
Applies rounding correction to avoid always flooring the result.
Returns None if the division would overflow or the result exceeds u128::MAX.
Sourcepub fn signed(&self) -> SignedNumeric
pub fn signed(&self) -> SignedNumeric
Converts this UnsignedNumeric into a signed version,
wrapping it in a SignedNumeric with is_negative = false.
Useful when beginning arithmetic that may result in negative values.
Sourcepub fn almost_eq(&self, rhs: &Self, precision: InnerUint) -> bool
pub fn almost_eq(&self, rhs: &Self, precision: InnerUint) -> bool
Compares two UnsignedNumerics for approximate equality,
allowing for a configurable precision window.
Sourcepub fn greater_than(&self, rhs: &Self) -> bool
pub fn greater_than(&self, rhs: &Self) -> bool
Returns true if self > rhs.
Sourcepub fn less_than_or_equal(&self, rhs: &Self) -> bool
pub fn less_than_or_equal(&self, rhs: &Self) -> bool
Returns true if self <= rhs.
Sourcepub fn greater_than_or_equal(&self, rhs: &Self) -> bool
pub fn greater_than_or_equal(&self, rhs: &Self) -> bool
Returns true if self >= rhs.
Sourcepub fn floor(&self) -> Option<Self>
pub fn floor(&self) -> Option<Self>
Rounds down to the nearest whole number by truncating fractional digits.
Sourcepub fn checked_div(&self, rhs: &Self) -> Option<Self>
pub fn checked_div(&self, rhs: &Self) -> Option<Self>
Divides self / rhs in fixed-point space, maintaining precision.
Applies rounding correction to minimize truncation error.
Returns None on divide-by-zero or overflow.
Sourcepub fn checked_mul(&self, rhs: &Self) -> Option<Self>
pub fn checked_mul(&self, rhs: &Self) -> Option<Self>
Multiplies two UnsignedNumerics and returns the result in fixed-point space.
Automatically divides by ONE to maintain correct scaling, and applies rounding correction.
Falls back to a reduced-precision path if full multiplication would overflow.
Sourcepub fn checked_add(&self, rhs: &Self) -> Option<Self>
pub fn checked_add(&self, rhs: &Self) -> Option<Self>
Adds two precise numbers. Returns None on overflow.
Sourcepub fn checked_sub(&self, rhs: &Self) -> Option<Self>
pub fn checked_sub(&self, rhs: &Self) -> Option<Self>
Subtracts rhs from self. Returns None if the result would be negative.
Sourcepub fn unsigned_sub(&self, rhs: &Self) -> (Self, bool)
pub fn unsigned_sub(&self, rhs: &Self) -> (Self, bool)
Computes the absolute difference between two numbers. Returns the result and a boolean indicating whether the result was originally negative.
Source§impl UnsignedNumeric
impl UnsignedNumeric
Sourcepub fn frexp(&self) -> Option<(Self, i64)>
pub fn frexp(&self) -> Option<(Self, i64)>
Frexp breaks f into a normalized fraction and an integral power of two. It returns frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).
Special cases are: Frexp(±0) = ±0, 0 Frexp(±Inf) = ±Inf, 0 Frexp(NaN) = NaN, 0
Source§impl UnsignedNumeric
impl UnsignedNumeric
Sourcepub fn log(&self) -> Option<SignedNumeric>
pub fn log(&self) -> Option<SignedNumeric>
Log returns the natural logarithm of x.
Special cases are: Log(+Inf) = +Inf Log(0) = -Inf Log(x < 0) = NaN
Trait Implementations§
Source§impl Add<&UnsignedNumeric> for &UnsignedNumeric
impl Add<&UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
+ operator.Source§impl Add<&UnsignedNumeric> for UnsignedNumeric
impl Add<&UnsignedNumeric> for UnsignedNumeric
Source§impl Add<UnsignedNumeric> for &UnsignedNumeric
impl Add<UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
+ operator.Source§impl Add for UnsignedNumeric
impl Add for UnsignedNumeric
Source§impl Clone for UnsignedNumeric
impl Clone for UnsignedNumeric
Source§fn clone(&self) -> UnsignedNumeric
fn clone(&self) -> UnsignedNumeric
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UnsignedNumeric
impl Debug for UnsignedNumeric
Source§impl Div<&UnsignedNumeric> for &UnsignedNumeric
impl Div<&UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
/ operator.Source§impl Div<&UnsignedNumeric> for UnsignedNumeric
impl Div<&UnsignedNumeric> for UnsignedNumeric
Source§impl Div<UnsignedNumeric> for &UnsignedNumeric
impl Div<UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
/ operator.Source§impl Div for UnsignedNumeric
impl Div for UnsignedNumeric
Source§impl Mul<&UnsignedNumeric> for &UnsignedNumeric
impl Mul<&UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
* operator.Source§impl Mul<&UnsignedNumeric> for UnsignedNumeric
impl Mul<&UnsignedNumeric> for UnsignedNumeric
Source§impl Mul<UnsignedNumeric> for &UnsignedNumeric
impl Mul<UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
* operator.Source§impl Mul for UnsignedNumeric
impl Mul for UnsignedNumeric
Source§impl PartialEq for UnsignedNumeric
impl PartialEq for UnsignedNumeric
Source§impl Sub<&UnsignedNumeric> for &UnsignedNumeric
impl Sub<&UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
- operator.Source§impl Sub<&UnsignedNumeric> for UnsignedNumeric
impl Sub<&UnsignedNumeric> for UnsignedNumeric
Source§impl Sub<UnsignedNumeric> for &UnsignedNumeric
impl Sub<UnsignedNumeric> for &UnsignedNumeric
Source§type Output = UnsignedNumeric
type Output = UnsignedNumeric
- operator.