Skip to main content

Fixed32

Struct Fixed32 

Source
#[repr(transparent)]
pub struct Fixed32(pub i32);
Expand description

Fixed-point number with 16.16 format (16 integer bits, 16 fractional bits).

Guarantees identical results across ALL platforms per Monniaux (2008): “Floating-point non-determinism is a primary source of divergence in cross-platform replay systems.”

§Examples

use jugar_web::trace::Fixed32;

let a = Fixed32::from_int(5);
let b = Fixed32::from_int(3);
assert_eq!((a + b).to_int(), 8);
assert_eq!((a - b).to_int(), 2);
assert_eq!((a * b).to_int(), 15);
assert_eq!((a / b).to_int(), 1); // Integer division

Tuple Fields§

§0: i32

Implementations§

Source§

impl Fixed32

Source

pub const FRAC_BITS: u32 = 16

Number of fractional bits (16.16 format)

Source

pub const SCALE: i32

Scale factor (2^16 = 65536)

Source

pub const ZERO: Self

Zero

Source

pub const ONE: Self

One (1.0 in fixed-point)

Source

pub const HALF: Self

Half (0.5 in fixed-point)

Source

pub const MIN: Self

Minimum value

Source

pub const MAX: Self

Maximum value

Source

pub const EPSILON: Self

Epsilon (smallest positive value)

Source

pub const PI: Self

Pi approximation (accurate to ~5 decimal places)

Source

pub const fn from_raw(raw: i32) -> Self

Create from raw fixed-point value (internal representation).

Source

pub const fn to_raw(self) -> i32

Get the raw fixed-point value.

Source

pub const fn from_int(n: i32) -> Self

Create from integer.

Source

pub const fn to_int(self) -> i32

Convert to integer (truncates fractional part).

Source

pub fn from_f32(f: f32) -> Self

Create from f32 (use only for constants, not runtime game logic).

§Warning

This conversion introduces platform-dependent rounding. Only use for initialization from constants; never in game update loops.

Source

pub fn to_f32(self) -> f32

Convert to f32 (for rendering only, not game logic).

§Warning

The result may differ slightly across platforms. Only use for rendering; never store or compare these values for game logic.

Source

pub const fn saturating_add(self, other: Self) -> Self

Saturating addition (clamps to MIN/MAX instead of overflowing).

Source

pub const fn saturating_sub(self, other: Self) -> Self

Saturating subtraction (clamps to MIN/MAX instead of overflowing).

Source

pub const fn mul(self, other: Self) -> Self

Fixed-point multiplication with proper scaling.

Uses i64 intermediate to prevent overflow.

Source

pub const fn saturating_mul(self, other: Self) -> Self

Saturating multiplication (clamps instead of overflowing).

Source

pub const fn div(self, other: Self) -> Self

Fixed-point division with proper scaling.

Uses i64 intermediate to prevent overflow.

§Panics

Panics if other is zero.

Source

pub const fn checked_div(self, other: Self) -> Option<Self>

Checked division (returns None if divisor is zero).

Source

pub const fn abs(self) -> Self

Absolute value.

Source

pub const fn signum(self) -> Self

Sign of the number (-1, 0, or 1).

Source

pub const fn is_negative(self) -> bool

Check if negative.

Source

pub const fn is_positive(self) -> bool

Check if positive.

Source

pub const fn is_zero(self) -> bool

Check if zero.

Source

pub const fn clamp(self, min: Self, max: Self) -> Self

Clamp value to range [min, max].

Source

pub const fn lerp(self, other: Self, t: Self) -> Self

Linear interpolation between self and other.

t should be between 0.0 and 1.0 (as Fixed32).

Source

pub const fn floor(self) -> Self

Floor to integer (round toward negative infinity).

Source

pub const fn ceil(self) -> Self

Ceiling to integer (round toward positive infinity).

Source

pub const fn round(self) -> Self

Round to nearest integer.

Source

pub const fn fract(self) -> Self

Get fractional part.

Source

pub const fn checked_mul(self, other: Self) -> Option<Self>

Checked multiplication - returns None on overflow (Regehr 2012).

Use this in game logic where overflow indicates a bug that should be caught early in development.

§Examples
use jugar_web::trace::Fixed32;

let a = Fixed32::from_int(100);
let b = Fixed32::from_int(50);
assert_eq!(a.checked_mul(b), Some(Fixed32::from_int(5000)));

// Overflow case
let big = Fixed32::MAX;
assert_eq!(big.checked_mul(Fixed32::from_int(2)), None);
Source

pub const fn strict_mul(self, other: Self) -> Self

Strict multiplication - panics on overflow in all builds (Regehr 2012).

Use this in game logic where overflow indicates a bug. This is the safest option for catching bugs early.

§Panics

Panics if the multiplication would overflow.

§Examples
use jugar_web::trace::Fixed32;

let a = Fixed32::from_int(100);
let b = Fixed32::from_int(50);
assert_eq!(a.strict_mul(b), Fixed32::from_int(5000));
Source

pub const fn checked_add(self, other: Self) -> Option<Self>

Checked addition - returns None on overflow.

Source

pub const fn strict_add(self, other: Self) -> Self

Strict addition - panics on overflow.

§Panics

Panics if the addition would overflow.

Source

pub const fn checked_sub(self, other: Self) -> Option<Self>

Checked subtraction - returns None on overflow.

Source

pub const fn strict_sub(self, other: Self) -> Self

Strict subtraction - panics on overflow.

§Panics

Panics if the subtraction would overflow.

Trait Implementations§

Source§

impl Add for Fixed32

Source§

type Output = Fixed32

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Fixed32

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Clone for Fixed32

Source§

fn clone(&self) -> Fixed32

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Fixed32

Source§

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

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

impl Default for Fixed32

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Fixed32

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Fixed32

Source§

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

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

impl Div for Fixed32

Source§

type Output = Fixed32

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl From<i32> for Fixed32

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl Hash for Fixed32

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 Mul for Fixed32

Source§

type Output = Fixed32

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Neg for Fixed32

Source§

type Output = Fixed32

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for Fixed32

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · 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 Fixed32

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Fixed32

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 Serialize for Fixed32

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Fixed32

Source§

type Output = Fixed32

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Fixed32

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl Copy for Fixed32

Source§

impl Eq for Fixed32

Source§

impl StructuralPartialEq for Fixed32

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,