pub struct FixedPoint {
pub value: U256,
}Expand description
A fixed-point number with 18 decimal places of precision.
This type represents decimal numbers using integer arithmetic, scaled by 10^18. All arithmetic operations maintain the scale factor automatically.
§Examples
// Create 5.5
let x = FixedPoint::from_fraction(5, 1, 2)?; // 5 + 1/2
// Create from float (testing only)
let y = FixedPoint::from_f64(2.5)?;
// Arithmetic
let sum = x.add(&y)?; // 8.0Fields§
§value: U256The raw U256 value, scaled by 10^18
Implementations§
Source§impl FixedPoint
impl FixedPoint
Sourcepub fn from_scaled(value: U256) -> Self
pub fn from_scaled(value: U256) -> Self
Sourcepub fn from_scaled_u128(value: u128) -> Self
pub fn from_scaled_u128(value: u128) -> Self
Creates a fixed-point number from a raw scaled u128 value.
This function assumes the input is already scaled by 10^18.
§Arguments
value- The pre-scaled u128 value
Sourcepub fn to_u64(&self) -> Result<u64>
pub fn to_u64(&self) -> Result<u64>
Converts the fixed-point number to a u64 integer.
This operation truncates any decimal places. For example, 5.7 becomes 5, and 5.3 becomes 5.
§Returns
The integer part as u64, or an error if the value is too large.
§Errors
MathError::Overflow- If the value exceeds u64::MAX
§Examples
let x = FixedPoint::from_fraction(5, 7, 10)?; // 5.7
assert_eq!(x.to_u64()?, 5);Sourcepub fn to_f64(&self) -> Result<f64>
pub fn to_f64(&self) -> Result<f64>
Converts the fixed-point number to an f64 float.
This is primarily intended for debugging and testing purposes. Precision may be lost for very large or very precise numbers.
§Returns
The floating-point representation, or an error if overflow occurs.
§Errors
MathError::Overflow- If the value is too large for f64 conversion
§Examples
let x = FixedPoint::from_int(5);
assert!((x.to_f64()? - 5.0).abs() < 1e-10);Sourcepub fn from_f64(val: f64) -> Result<Self>
pub fn from_f64(val: f64) -> Result<Self>
Creates a fixed-point number from an f64 float.
This is intended for testing and convenience. For production code, prefer using integer-based constructors for deterministic behavior.
§Arguments
val- The floating-point value (must be non-negative and finite)
§Returns
A new FixedPoint, or an error if the input is invalid.
§Errors
MathError::InvalidInput- If val is negative, infinite, or NaNMathError::Overflow- If val is too large to represent
§Examples
let x = FixedPoint::from_f64(3.14159)?;Sourcepub fn from_fraction(
whole: u64,
numerator: u64,
denominator: u64,
) -> Result<Self>
pub fn from_fraction( whole: u64, numerator: u64, denominator: u64, ) -> Result<Self>
Creates a fixed-point number from fractional components.
Computes: whole + (numerator / denominator)
§Arguments
whole- The integer partnumerator- Numerator of the fractional partdenominator- Denominator of the fractional part (must be non-zero)
§Returns
The resulting fixed-point number.
§Errors
MathError::DivisionByZero- If denominator is zero
§Examples
// Create 5.5 (5 + 1/2)
let x = FixedPoint::from_fraction(5, 1, 2)?;Sourcepub fn from_ratio(numerator: u64, denominator: u64) -> Result<Self>
pub fn from_ratio(numerator: u64, denominator: u64) -> Result<Self>
Sourcepub fn from_percent(percent: u64) -> Result<Self>
pub fn from_percent(percent: u64) -> Result<Self>
Sourcepub fn div(&self, other: &Self) -> Result<Self>
pub fn div(&self, other: &Self) -> Result<Self>
Divides one fixed-point number by another.
§Arguments
other- The divisor (must be non-zero)
§Returns
The quotient.
§Errors
MathError::DivisionByZero- If other is zeroMathError::Overflow- If the result exceeds U256::MAX
§Examples
let x = FixedPoint::from_int(10);
let y = FixedPoint::from_int(4);
let quotient = x.div(&y)?; // 2.5Sourcepub fn pow2_fast(exponent: &Self) -> Result<Self>
pub fn pow2_fast(exponent: &Self) -> Result<Self>
Optimized power function for base 2 with fractional exponents.
Computes 2^exponent using range reduction and Taylor series. This is more efficient than the general power function.
§Arguments
exponent- The exponent (can be fractional)
§Returns
2 raised to the given power.
§Errors
MathError::Overflow- If the result is too large
§Examples
let exp = FixedPoint::from_int(3);
let result = FixedPoint::pow2_fast(&exp)?; // 2^3 = 8Sourcepub fn pow(&self, exponent: &Self) -> Result<Self>
pub fn pow(&self, exponent: &Self) -> Result<Self>
Raises this number to the given power.
Supports both integer and fractional exponents. Uses optimized algorithms based on the base and exponent values.
§Arguments
exponent- The power to raise to (can be fractional)
§Returns
self^exponent
§Errors
MathError::InvalidInput- If self is zero and exponent is non-positiveMathError::Overflow- If the result is too large
§Examples
let base = FixedPoint::from_int(5);
let exp = FixedPoint::from_int(2);
let result = base.pow(&exp)?; // 25
// Fractional exponent (square root)
let half = FixedPoint::from_ratio(1, 2)?;
let sqrt_25 = FixedPoint::from_int(25).pow(&half)?; // 5Sourcepub fn debug_value(&self) -> (u64, u64, u64, u64)
pub fn debug_value(&self) -> (u64, u64, u64, u64)
Returns the raw internal U256 representation for debugging.
The U256 is stored as 4 u64 limbs.
§Returns
A tuple of (limb0, limb1, limb2, limb3)
Trait Implementations§
Source§impl Clone for FixedPoint
impl Clone for FixedPoint
Source§fn clone(&self) -> FixedPoint
fn clone(&self) -> FixedPoint
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more