pub struct D64 { /* private fields */ }Expand description
64-bit fixed-point decimal with 8 decimal places of precision.
Range: ±92,233,720,368.54775807 Precision: 0.00000001
Implementations§
Source§impl D64
impl D64
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
Source§impl D64
impl D64
Sourcepub const fn from_raw(value: i64) -> Self
pub const fn from_raw(value: i64) -> Self
Creates a new D64 from a raw scaled value.
§Safety
The caller must ensure the value is properly scaled by 10^8.
Sourcepub const fn new(integer: i64, fractional: i64) -> Self
pub const fn new(integer: i64, fractional: i64) -> Self
Creates a D64 from integer and fractional parts at compile time
Example: new(123, 45_000_000) → 123.45
The fractional part should always be positive.
For negative numbers, use a negative integer part:
new(-123, 45_000_000) → -123.45
§Panics
Panics if the value would overflow i64 range.
Sourcepub const fn from_basis_points(bps: i64) -> Option<Self>
pub const fn from_basis_points(bps: i64) -> 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) -> i64
pub const fn to_basis_points(self) -> i64
Convert to basis points
Example: D64::from_str("0.01").unwrap().to_basis_points() → 100
Sourcepub fn with_scale(mantissa: i64, scale: u32) -> Self
pub fn with_scale(mantissa: i64, scale: u32) -> Self
Creates a D64 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 8 (our max precision)
- The resulting value is out of bounds for D64
Sourcepub const fn try_with_scale(mantissa: i64, scale: u32) -> Option<Self>
pub const fn try_with_scale(mantissa: i64, scale: u32) -> Option<Self>
Creates a D64 from a mantissa and scale, returning None on error.
Like with_scale but returns None instead of panicking.
Sourcepub fn with_scale_lossy(mantissa: i64, scale: u32) -> Self
pub fn with_scale_lossy(mantissa: i64, scale: u32) -> Self
Creates a D64 from a mantissa and scale, rounding if necessary.
If the scale is greater than 8, the mantissa will be divided by 10^(scale-8) 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 D64.
Sourcepub const fn try_with_scale_lossy(mantissa: i64, scale: u32) -> Option<Self>
pub const fn try_with_scale_lossy(mantissa: i64, scale: u32) -> Option<Self>
Creates a D64 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 D64
impl D64
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 D64
impl D64
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 D64
impl D64
Sourcepub const fn checked_mul(self, rhs: Self) -> Option<Self>
pub const fn checked_mul(self, rhs: Self) -> Option<Self>
Fast multiplication using reciprocal division
pub const fn saturating_mul(self, rhs: Self) -> Self
pub const fn wrapping_mul(self, rhs: Self) -> Self
Sourcepub const fn try_mul(self, rhs: Self) -> Result<Self>
pub const fn try_mul(self, rhs: Self) -> Result<Self>
Checked multiplication. Returns an error if overflow occurred.
Sourcepub const fn mul_i64(self, rhs: i64) -> Option<Self>
pub const fn mul_i64(self, rhs: i64) -> Option<Self>
Multiply by an integer (faster than general multiplication) Useful for: quantity * price, shares * rate, etc.
pub const fn try_mul_i64(self, rhs: i64) -> Result<Self>
Source§impl D64
impl D64
Sourcepub const fn checked_div(self, rhs: Self) -> Option<Self>
pub const fn checked_div(self, rhs: Self) -> Option<Self>
Checked division. Returns None if rhs is zero or overflow occurred.
Internally widens to i128 to maintain precision.
Sourcepub const fn saturating_div(self, rhs: Self) -> Self
pub const fn saturating_div(self, rhs: Self) -> Self
Saturating division. Clamps on overflow. Returns zero if rhs is zero.
Sourcepub const fn wrapping_div(self, rhs: Self) -> Self
pub const fn wrapping_div(self, rhs: Self) -> Self
Wrapping division. Wraps on overflow. Returns zero if rhs is zero.
Source§impl D64
impl D64
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 D64
impl D64
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 D64
impl D64
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 D64
impl D64
Source§impl D64
impl D64
Sourcepub const fn recip(self) -> Option<Self>
pub const fn recip(self) -> Option<Self>
Returns the reciprocal (multiplicative inverse) of self.
Returns None if self is zero.
Sourcepub const fn try_recip(self) -> Result<Self>
pub const fn try_recip(self) -> Result<Self>
Checked reciprocal. Returns an error if self is zero.
Sourcepub const fn powi(self, exp: i32) -> Option<Self>
pub const fn powi(self, exp: i32) -> Option<Self>
Raises self to an integer power.
Returns None if overflow occurs.
§Performance
Uses exponentiation by squaring for efficiency.
Sourcepub const fn try_powi(self, exp: i32) -> Result<Self>
pub const fn try_powi(self, exp: i32) -> Result<Self>
Checked integer power. Returns an error if overflow occurred.
Source§impl D64
impl D64
Sourcepub const fn to_i64_round(self) -> i64
pub const fn to_i64_round(self) -> i64
Converts to i64, rounding to nearest (banker’s rounding on ties).
Sourcepub const fn try_from_i64(value: i64) -> Result<Self>
pub const fn try_from_i64(value: i64) -> Result<Self>
Creates a D64 from an i64, returning an error on overflow.
Sourcepub const fn try_from_u64(value: u64) -> Result<Self>
pub const fn try_from_u64(value: u64) -> Result<Self>
Creates a D64 from a u64, returning an error on overflow.
Source§impl D64
impl D64
Source§impl D64
impl D64
Sourcepub fn percent_of(self, percent: Self) -> Option<Self>
pub fn percent_of(self, percent: Self) -> Option<Self>
Calculate percentage: self * (percent / 100) Example: 1000.percent_of(5) → 50 (5% of 1000)
Sourcepub fn add_percent(self, percent: Self) -> Option<Self>
pub fn add_percent(self, percent: Self) -> Option<Self>
Add percentage: self * (1 + percent/100) Example: 1000.add_percent(5) → 1050 (add 5%)
Source§impl D64
impl D64
Sourcepub fn from_str_exact(s: &str) -> Result<Self>
pub fn from_str_exact(s: &str) -> Result<Self>
Parses a decimal string into a D64.
Supports formats like: “123”, “123.45”, “-123.45”, “0.00000001”
Zero-allocation implementation using iterators.
§Errors
Returns DecimalError::InvalidFormat if the string is not a valid decimal.
Returns DecimalError::Overflow if the value is too large.
Returns DecimalError::PrecisionLoss if more than 8 decimal places are provided.
Sourcepub fn from_str_lossy(s: &str) -> Result<Self>
pub fn from_str_lossy(s: &str) -> Result<Self>
Parse a string, rounding to 8 decimal places if necessary
Unlike from_str_exact, this will succeed even if the input has more than
8 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 assuming a fixed number of decimals (no decimal point in string) E.g., parse_fixed_point_str(“12345”, 2) → 123.45
Source§impl D64
impl D64
Sourcepub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self>
Parse from byte slice (useful for binary protocols)
Sourcepub const fn from_be_bytes(bytes: [u8; 8]) -> Self
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self
Creates a D64 from its representation as a byte array in big endian.
Sourcepub const fn from_le_bytes(bytes: [u8; 8]) -> Self
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self
Creates a D64 from its representation as a byte array in little endian.
Sourcepub const fn from_ne_bytes(bytes: [u8; 8]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self
Creates a D64 from its representation as a byte array in native endian.
Sourcepub const fn to_be_bytes(self) -> [u8; 8]
pub const fn to_be_bytes(self) -> [u8; 8]
Returns the memory representation of this decimal as a byte array in big-endian byte order.
Sourcepub const fn to_le_bytes(self) -> [u8; 8]
pub const fn to_le_bytes(self) -> [u8; 8]
Returns the memory representation of this decimal as a byte array in little-endian byte order.
Sourcepub const fn to_ne_bytes(self) -> [u8; 8]
pub const fn to_ne_bytes(self) -> [u8; 8]
Returns the memory representation of this decimal 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 to the given buffer.
§Panics
Panics if buf.len() < 8.
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 to the given buffer.
§Panics
Panics if buf.len() < 8.
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 to the given buffer.
§Panics
Panics if buf.len() < 8.
Sourcepub fn read_le_bytes(buf: &[u8]) -> Self
pub fn read_le_bytes(buf: &[u8]) -> Self
Reads a decimal from bytes in little-endian order from the given buffer.
§Panics
Panics if buf.len() < 8.
Sourcepub fn read_be_bytes(buf: &[u8]) -> Self
pub fn read_be_bytes(buf: &[u8]) -> Self
Reads a decimal from bytes in big-endian order from the given buffer.
§Panics
Panics if buf.len() < 8.
Sourcepub fn read_ne_bytes(buf: &[u8]) -> Self
pub fn read_ne_bytes(buf: &[u8]) -> Self
Reads a decimal from bytes in native-endian order from the given buffer.
§Panics
Panics if buf.len() < 8.
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 to the given buffer.
Returns None if buf.len() < 8.
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 to the given buffer.
Returns None if buf.len() < 8.
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 to the given buffer.
Returns None if buf.len() < 8.
Sourcepub fn try_read_le_bytes(buf: &[u8]) -> Option<Self>
pub fn try_read_le_bytes(buf: &[u8]) -> Option<Self>
Tries to read a decimal from bytes in little-endian order from the given buffer.
Returns None if buf.len() < 8.
Sourcepub fn try_read_be_bytes(buf: &[u8]) -> Option<Self>
pub fn try_read_be_bytes(buf: &[u8]) -> Option<Self>
Tries to read a decimal from bytes in big-endian order from the given buffer.
Returns None if buf.len() < 8.
Sourcepub fn try_read_ne_bytes(buf: &[u8]) -> Option<Self>
pub fn try_read_ne_bytes(buf: &[u8]) -> Option<Self>
Tries to read a decimal from bytes in native-endian order from the given buffer.
Returns None if buf.len() < 8.
Trait Implementations§
Source§impl AddAssign for D64
impl AddAssign for D64
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl DivAssign for D64
impl DivAssign for D64
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl MulAssign for D64
impl MulAssign for D64
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl Ord for D64
impl Ord for D64
Source§impl PartialOrd for D64
impl PartialOrd for D64
Source§impl SubAssign for D64
impl SubAssign for D64
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more