pub struct D96 { /* private fields */ }Expand description
96-bit fixed-point decimal with 12 decimal places of precision.
Uses 128-bit storage but only uses 96 bits of the mantissa for faster arithmetic. Range: ±39,614,081,257,132.168796771975167 Precision: 0.000000000001 (1 microGwei = 1000 wei)
Implementations§
Source§impl D96
impl D96
Sourcepub const BASIS_POINT: Self
pub const BASIS_POINT: Self
One basis point (0.0001) - 1 bps, common in interest rates
Sourcepub const HALF_BASIS_POINT: Self
pub const HALF_BASIS_POINT: Self
One half basis point (0.00005)
Sourcepub const QUARTER_BASIS_POINT: Self
pub const QUARTER_BASIS_POINT: Self
One quarter basis point (0.000025)
Sourcepub const THIRTY_SECOND: Self
pub const THIRTY_SECOND: Self
One thirty-second (1/32 = 0.03125) - US Treasury bond price tick
Sourcepub const SIXTY_FOURTH: Self
pub const SIXTY_FOURTH: Self
One sixty-fourth (1/64 = 0.015625) - US Treasury bond price tick
Sourcepub const HALF_THIRTY_SECOND: Self = Self::SIXTY_FOURTH
pub const HALF_THIRTY_SECOND: Self = Self::SIXTY_FOURTH
One half of a thirty-second (1/64) - alternative name
Sourcepub const QUARTER_THIRTY_SECOND: Self
pub const QUARTER_THIRTY_SECOND: Self
One quarter of a thirty-second (1/128 = 0.0078125)
Sourcepub const EIGHTH_THIRTY_SECOND: Self
pub const EIGHTH_THIRTY_SECOND: Self
One eighth of a thirty-second (1/256 = 0.00390625)
Sourcepub const HUNDRED_BPS: Self = Self::PERCENT
pub const HUNDRED_BPS: Self = Self::PERCENT
One hundred basis points (0.01) - same as PERCENT
Sourcepub const GWEI: Self
pub const GWEI: Self
One gwei (0.000000001) - 9 decimal places, common gas unit With 12 decimals, 1 gwei = 1000 units
Sourcepub const MICRO_GWEI: Self
pub const MICRO_GWEI: Self
One microGwei (0.000000000001) - minimum representable unit Equal to 1000 wei
Source§impl D96
impl D96
Sourcepub const fn from_raw(value: i128) -> Self
pub const fn from_raw(value: i128) -> Self
Creates a new D96 from a raw scaled value.
§Panics
Panics in debug mode if value exceeds 96-bit range
Sourcepub const fn from_raw_checked(value: i128) -> Option<Self>
pub const fn from_raw_checked(value: i128) -> Option<Self>
Creates a new D96 from a raw scaled value, checking bounds.
Sourcepub const fn new(integer: i128, fractional: i128) -> Self
pub const fn new(integer: i128, fractional: i128) -> Self
Creates a D96 from integer and fractional parts at compile time
Example: new(123, 450_000_000_000) → 123.45
The fractional part should always be positive.
For negative numbers, use a negative integer part:
new(-123, 450_000_000_000) → -123.45
§Panics
Panics if the value would overflow the 96-bit range.
Sourcepub const fn from_basis_points(bps: i128) -> Option<Self>
pub const fn from_basis_points(bps: i128) -> Option<Self>
Create from basis points (1 bp = 0.0001)
Example: from_basis_points(100) → 0.01 (1%)
Sourcepub const fn to_basis_points(self) -> i128
pub const fn to_basis_points(self) -> i128
Convert to basis points
Example: D96::from_str("0.01").unwrap().to_basis_points() → 100
Sourcepub fn with_scale(mantissa: i128, scale: u32) -> Self
pub fn with_scale(mantissa: i128, scale: u32) -> Self
Creates a D96 from a mantissa and scale (like rust_decimal).
The scale represents the number of decimal places.
For example: with_scale(12345, 2) = 123.45
§Panics
Panics if:
- The scale is greater than 12 (our max precision)
- The resulting value is out of bounds for D96
Sourcepub const fn try_with_scale(mantissa: i128, scale: u32) -> Option<Self>
pub const fn try_with_scale(mantissa: i128, scale: u32) -> Option<Self>
Creates a D96 from a mantissa and scale, returning None on error.
Like with_scale but returns None instead of panicking.
Sourcepub fn with_scale_lossy(mantissa: i128, scale: u32) -> Self
pub fn with_scale_lossy(mantissa: i128, scale: u32) -> Self
Creates a D96 from a mantissa and scale, rounding if necessary.
If the scale is greater than 12, the mantissa will be divided by 10^(scale-12) to fit our precision, rounding to the nearest even number (banker’s rounding).
§Panics
Panics if the resulting value (after rounding) is out of bounds for D96.
Sourcepub const fn try_with_scale_lossy(mantissa: i128, scale: u32) -> Option<Self>
pub const fn try_with_scale_lossy(mantissa: i128, scale: u32) -> Option<Self>
Creates a D96 from a mantissa and scale, rounding if necessary, returns None on error.
Like with_scale_lossy but returns None instead of panicking on overflow.
Source§impl D96
impl D96
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Returns None if overflow occurred.
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Saturating addition. Clamps on overflow.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Wrapping addition. Wraps on overflow.
Source§impl D96
impl D96
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Returns None if overflow occurred.
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction. Clamps on overflow.
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Wrapping subtraction. Wraps on overflow.
Source§impl D96
impl D96
Sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Optimized multiplication for 96-bit values
Since we guarantee 96-bit inputs, we can use optimized 192-bit arithmetic
Sourcepub fn saturating_mul(self, rhs: Self) -> Self
pub fn saturating_mul(self, rhs: Self) -> Self
Saturating multiplication
Sourcepub fn wrapping_mul(self, rhs: Self) -> Self
pub fn wrapping_mul(self, rhs: Self) -> Self
Wrapping multiplication
Sourcepub const fn mul_i128(self, rhs: i128) -> Option<Self>
pub const fn mul_i128(self, rhs: i128) -> Option<Self>
Multiply by an integer (faster than general multiplication) Useful for: quantity * price, shares * rate, etc.
Sourcepub fn mul_add(self, mul: Self, add: Self) -> Option<Self>
pub fn mul_add(self, mul: Self, add: Self) -> Option<Self>
Computes (self * mul) + add with only one rounding step More accurate and faster than separate mul + add
Sourcepub fn try_mul(self, rhs: Self) -> Result<Self>
pub fn try_mul(self, rhs: Self) -> Result<Self>
Checked multiplication. Returns an error if overflow occurred.
Sourcepub const fn try_mul_i128(self, rhs: i128) -> Result<Self>
pub const fn try_mul_i128(self, rhs: i128) -> Result<Self>
Multiply by an integer, returning an error on overflow
Source§impl D96
impl D96
Sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
pub fn checked_div(self, rhs: Self) -> Option<Self>
Checked division. Returns None if rhs is zero or overflow occurred.
Sourcepub fn wrapping_div(self, rhs: Self) -> Self
pub fn wrapping_div(self, rhs: Self) -> Self
Wrapping division. Wraps on overflow. Returns zero if rhs is zero.
Sourcepub fn saturating_div(self, rhs: Self) -> Self
pub fn saturating_div(self, rhs: Self) -> Self
Saturating division. Clamps on overflow. Returns zero if rhs is zero.
Source§impl D96
impl D96
Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Returns None if the result would overflow.
Sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Clamps on overflow.
Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping negation. Wraps on overflow.
Source§impl D96
impl D96
Sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value. Returns None if the result would overflow.
Sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value. Clamps on overflow.
Sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value. Wraps on overflow.
Source§impl D96
impl D96
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative.
Source§impl D96
impl D96
Source§impl D96
impl D96
Source§impl D96
impl D96
Sourcepub const fn from_i64(value: i64) -> Self
pub const fn from_i64(value: i64) -> Self
Creates a D96 from an i64 integer (always succeeds for reasonable values).
Sourcepub const fn from_u64(value: u64) -> Self
pub const fn from_u64(value: u64) -> Self
Creates a D96 from a u64 integer (always succeeds for reasonable values).
Sourcepub const fn to_i64(self) -> Option<i64>
pub const fn to_i64(self) -> Option<i64>
Converts to i64, truncating any fractional part. Returns None if the value doesn’t fit in i64.
Sourcepub const fn to_i128_round(self) -> i128
pub const fn to_i128_round(self) -> i128
Converts to i128, rounding to nearest (banker’s rounding on ties).
Sourcepub const fn try_from_i128(value: i128) -> Result<Self>
pub const fn try_from_i128(value: i128) -> Result<Self>
Creates a D96 from an i128, returning an error on overflow.
Sourcepub const fn try_from_u128(value: u128) -> Result<Self>
pub const fn try_from_u128(value: u128) -> Result<Self>
Creates a D96 from a u128, returning an error on overflow.
Source§impl D96
impl D96
Source§impl D96
impl D96
Sourcepub fn percent_of(self, percent: Self) -> Option<Self>
pub fn percent_of(self, percent: Self) -> Option<Self>
Calculate percentage: self * (percent / 100)
Sourcepub fn add_percent(self, percent: Self) -> Option<Self>
pub fn add_percent(self, percent: Self) -> Option<Self>
Add percentage: self * (1 + percent/100)
Source§impl D96
impl D96
Sourcepub fn from_str_exact(s: &str) -> Result<Self>
pub fn from_str_exact(s: &str) -> Result<Self>
Parses a decimal string into a D96.
Supports formats like: “123”, “123.45”, “-123.45”, “0.000000000001” Fast parsing using SWAR (SIMD Within A Register) for both integer and fractional parts
Sourcepub fn from_str_lossy(s: &str) -> Result<Self>
pub fn from_str_lossy(s: &str) -> Result<Self>
Parse a string, rounding to 12 decimal places if necessary
Unlike from_str_exact, this will succeed even if the input has more than
12 decimal places, rounding the excess digits using banker’s rounding.
Sourcepub fn from_fixed_point_str(s: &str, decimals: u8) -> Result<Self>
pub fn from_fixed_point_str(s: &str, decimals: u8) -> Result<Self>
Parse from fixed-point string (no decimal point)
Source§impl D96
impl D96
Sourcepub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self>
Parse from byte slice
Sourcepub const fn from_le_bytes(bytes: [u8; 16]) -> Self
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self
Read D96 from little-endian bytes
Sourcepub const fn from_be_bytes(bytes: [u8; 16]) -> Self
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self
Read D96 from big-endian bytes
Sourcepub const fn from_ne_bytes(bytes: [u8; 16]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self
Read D96 from native-endian bytes
Sourcepub const fn read_le_bytes(bytes: &[u8]) -> Self
pub const fn read_le_bytes(bytes: &[u8]) -> Self
Read D96 from a byte slice (little-endian)
Sourcepub const fn read_be_bytes(bytes: &[u8]) -> Self
pub const fn read_be_bytes(bytes: &[u8]) -> Self
Read D96 from a byte slice (big-endian)
Sourcepub const fn read_ne_bytes(bytes: &[u8]) -> Self
pub const fn read_ne_bytes(bytes: &[u8]) -> Self
Read D96 from a byte slice (native-endian)
Sourcepub const fn try_read_le_bytes(bytes: &[u8]) -> Option<Self>
pub const fn try_read_le_bytes(bytes: &[u8]) -> Option<Self>
Try to read D96 from a byte slice (little-endian), checking bounds
Sourcepub const fn try_read_be_bytes(bytes: &[u8]) -> Option<Self>
pub const fn try_read_be_bytes(bytes: &[u8]) -> Option<Self>
Try to read D96 from a byte slice (big-endian), checking bounds
Sourcepub const fn try_read_ne_bytes(bytes: &[u8]) -> Option<Self>
pub const fn try_read_ne_bytes(bytes: &[u8]) -> Option<Self>
Try to read D96 from a byte slice (native-endian), checking bounds
Sourcepub const fn to_be_bytes(self) -> [u8; 16]
pub const fn to_be_bytes(self) -> [u8; 16]
Returns the memory representation as a byte array in big-endian byte order.
Sourcepub const fn to_le_bytes(self) -> [u8; 16]
pub const fn to_le_bytes(self) -> [u8; 16]
Returns the memory representation as a byte array in little-endian byte order.
Sourcepub const fn to_ne_bytes(self) -> [u8; 16]
pub const fn to_ne_bytes(self) -> [u8; 16]
Returns the memory representation as a byte array in native byte order.
Sourcepub fn write_le_bytes(&self, buf: &mut [u8])
pub fn write_le_bytes(&self, buf: &mut [u8])
Writes the decimal as bytes in little-endian order.
Sourcepub fn write_be_bytes(&self, buf: &mut [u8])
pub fn write_be_bytes(&self, buf: &mut [u8])
Writes the decimal as bytes in big-endian order.
Sourcepub fn write_ne_bytes(&self, buf: &mut [u8])
pub fn write_ne_bytes(&self, buf: &mut [u8])
Writes the decimal as bytes in native-endian order.
Sourcepub fn try_write_le_bytes(&self, buf: &mut [u8]) -> Option<()>
pub fn try_write_le_bytes(&self, buf: &mut [u8]) -> Option<()>
Tries to write the decimal as bytes in little-endian order.
Sourcepub fn try_write_be_bytes(&self, buf: &mut [u8]) -> Option<()>
pub fn try_write_be_bytes(&self, buf: &mut [u8]) -> Option<()>
Tries to write the decimal as bytes in big-endian order.
Sourcepub fn try_write_ne_bytes(&self, buf: &mut [u8]) -> Option<()>
pub fn try_write_ne_bytes(&self, buf: &mut [u8]) -> Option<()>
Tries to write the decimal as bytes in native-endian order.
Trait Implementations§
Source§impl AddAssign for D96
impl AddAssign for D96
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl DivAssign for D96
impl DivAssign for D96
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl MulAssign for D96
impl MulAssign for D96
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl Ord for D96
impl Ord for D96
Source§impl PartialOrd for D96
impl PartialOrd for D96
Source§impl SubAssign for D96
impl SubAssign for D96
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more