Skip to main content

Wide

Struct Wide 

Source
pub struct Wide(/* private fields */);
Expand description

A product of two Fixed values, kept at full width: Q95.32 in an i128.

§Why this exists

Fixed::saturating_mul narrows its i128 product back to an i64, which is right for arithmetic that stays in the world and wrong for geometry that squares things twice. Ray-versus-sphere forms b² − 4ac where a and c are themselves squared lengths; at ordinary game scales a·c overflows a Fixed long before the ray does anything unusual, and the result saturates — deterministically, and to the wrong number.

So the narrowing is deferred. A product of two full-range Fixed values needs 126 bits and an i128 holds 127, which means a single wide_mul can never overflow, for any inputs at all. Sums of a few of them cannot either at any scale a world reaches.

§Contract

  • 32 fractional bits, being the sum of its operands’ sixteen. That is not an implementation detail: it is why Wide::sqrt needs no shift.
  • Ordering is exact, so comparing two products — which is most of what geometry does with them — never rounds at all.
  • Narrowing is explicit, and says whether it lost anything.

Implementations§

Source§

impl Wide

Source

pub const ZERO: Self

Zero.

Source

pub const fn to_bits(self) -> i128

The raw Q95.32 pattern.

There is no from_bits counterpart, deliberately. A Wide is an intermediate rather than state: it is produced by a multiply and consumed by a root, a comparison or a narrowing, and nothing serialises one. An unused constructor is a promise to keep working that nobody asked for.

Source

pub const fn signum(self) -> i32

−1, 0 or 1.

Source

pub fn sqrt(self) -> Fixed

The square root, as a Fixed.

No shift, and that is the point of 32 fractional bits. A Wide holding the real value v has raw pattern v · 2³², whose integer square root is √v · 2¹⁶ — exactly a Fixed’s raw pattern. Squaring and then rooting therefore loses nothing to scaling, where the narrow path had to shift left by sixteen first and could overflow doing it.

Floor-exact, by u128::isqrt’s own contract.

§Panics

If negative. See Wide::checked_sqrt.

Source

pub fn checked_sqrt(self) -> Option<Fixed>

The square root, or None if negative — which a discriminant is, routinely, and which is a miss rather than a mistake.

Source

pub const fn checked_narrow(self) -> Option<Fixed>

Back to a Fixed, or None if it will not fit.

Trait Implementations§

Source§

impl Add for Wide

Source§

type Output = Wide

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Wide

Source§

fn clone(&self) -> Wide

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 Wide

Source§

impl Debug for Wide

Source§

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

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

impl Default for Wide

Source§

fn default() -> Wide

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

impl Eq for Wide

Source§

impl Hash for Wide

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 Neg for Wide

Source§

type Output = Wide

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for Wide

Source§

fn cmp(&self, other: &Wide) -> 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§

impl PartialEq for Wide

Source§

fn eq(&self, other: &Wide) -> 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 Wide

Source§

fn partial_cmp(&self, other: &Wide) -> 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 Wide

Source§

impl Sub for Wide

Source§

type Output = Wide

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for Wide

§

impl RefUnwindSafe for Wide

§

impl Send for Wide

§

impl Sync for Wide

§

impl Unpin for Wide

§

impl UnsafeUnpin for Wide

§

impl UnwindSafe for Wide

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.