pub struct Fixed6(/* private fields */);Expand description
A number scaled by 10^6, with truncating division.
Backed by i128 so that a multiplication of two scaled values cannot
overflow before the division that rescales it.
Implementations§
Source§impl Fixed6
impl Fixed6
Sourcepub const fn from_scaled(raw: i128) -> Self
pub const fn from_scaled(raw: i128) -> Self
Construct from a raw scaled value.
§Example
use publet_algorithms::Fixed6;
// 1_500_000 scaled by 10^6 is 1.5.
assert_eq!(Fixed6::from_scaled(1_500_000).to_string(), "1.500000");Sourcepub const fn from_integer(n: i64) -> Self
pub const fn from_integer(n: i64) -> Self
Construct from a whole number.
§Example
use publet_algorithms::Fixed6;
assert_eq!(Fixed6::from_integer(3).to_string(), "3.000000");
assert_eq!(Fixed6::from_integer(-2).to_scaled(), -2_000_000);Sourcepub const fn to_scaled(self) -> i128
pub const fn to_scaled(self) -> i128
The underlying scaled value.
§Example
use publet_algorithms::{Fixed6, SCALE};
assert_eq!(Fixed6::ONE.to_scaled(), SCALE);Sourcepub fn mul_div(self, numerator: Self, denominator: Self) -> Self
pub fn mul_div(self, numerator: Self, denominator: Self) -> Self
self * numerator / denominator, truncating toward zero.
The multiplication happens before the division and in i128, so
precision is not lost to an intermediate rounding that a different
implementation might place elsewhere.
Returns Self::ZERO when denominator is zero, which is the
arithmetic identity this layer wants: a key that confers no weight
at all confers none to anyone.
§Example
use publet_algorithms::Fixed6;
let two = Fixed6::from_integer(2);
let three = Fixed6::from_integer(3);
// 2 * 1 / 3, truncated: 0.666666, not 0.666667.
assert_eq!(two.mul_div(Fixed6::ONE, three).to_string(), "0.666666");
// A zero denominator gives zero rather than panicking.
assert_eq!(two.mul_div(Fixed6::ONE, Fixed6::ZERO), Fixed6::ZERO);Sourcepub fn times(self, other: Self) -> Self
pub fn times(self, other: Self) -> Self
self * other, truncating toward zero.
§Example
use publet_algorithms::Fixed6;
let half = Fixed6::from_scaled(500_000);
assert_eq!(half.times(half).to_string(), "0.250000");Sourcepub fn ratio(self, other: Self) -> Self
pub fn ratio(self, other: Self) -> Self
self / other, truncating toward zero.
Returns Self::ZERO when other is zero.
§Example
use publet_algorithms::Fixed6;
assert_eq!(Fixed6::ONE.ratio(Fixed6::from_integer(3)).to_string(), "0.333333");
assert_eq!(Fixed6::ONE.ratio(Fixed6::ZERO), Fixed6::ZERO);Sourcepub fn min(self, other: Self) -> Self
pub fn min(self, other: Self) -> Self
The smaller of two values.
§Example
use publet_algorithms::Fixed6;
assert_eq!(Fixed6::ONE.min(Fixed6::ZERO), Fixed6::ZERO);Sourcepub const fn is_zero(self) -> bool
pub const fn is_zero(self) -> bool
Whether this value is zero.
§Example
use publet_algorithms::Fixed6;
assert!(Fixed6::ZERO.is_zero());
// Truncation can reach zero: a millionth of a millionth.
let tiny = Fixed6::from_scaled(1);
assert!(tiny.times(tiny).is_zero());Sourcepub fn halve_fractional(self, numerator: u64, denominator: u64) -> Self
pub fn halve_fractional(self, numerator: u64, denominator: u64) -> Self
Halve repeatedly, for exponential decay.
Computes self * 2^(-numerator/denominator) by applying whole
halvings and then a linear interpolation of the remainder. Exact
exponentiation would need a transcendental function, which cannot be
made bit-identical across platforms; this approximation is fully
specified by integer operations and therefore can.
§Example
use publet_algorithms::Fixed6;
// One half-life: exactly half.
assert_eq!(Fixed6::ONE.halve_fractional(30, 30).to_string(), "0.500000");
// Half a half-life: interpolated between 1 and 0.5 (2^-0.5 is about 0.707).
assert_eq!(Fixed6::ONE.halve_fractional(15, 30).to_string(), "0.750000");