Skip to main content

Fixed6

Struct Fixed6 

Source
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

Source

pub const ZERO: Self

Zero.

Source

pub const ONE: Self

One.

Source

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");
Source

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);
Source

pub const fn to_scaled(self) -> i128

The underlying scaled value.

§Example
use publet_algorithms::{Fixed6, SCALE};

assert_eq!(Fixed6::ONE.to_scaled(), SCALE);
Source

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);
Source

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");
Source

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);
Source

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);
Source

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());
Source

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");

Trait Implementations§

Source§

impl Add for Fixed6

Source§

type Output = Fixed6

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Fixed6

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Fixed6

Source§

impl Debug for Fixed6

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Fixed6

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Fixed6

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Render with exactly six decimal places, so that output is stable.

Source§

impl Eq for Fixed6

Source§

impl Hash for Fixed6

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Fixed6

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Fixed6

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Fixed6

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Fixed6

Source§

impl Sub for Fixed6

Source§

type Output = Fixed6

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl Sum for Fixed6

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.