Skip to main content

Mag

Struct Mag 

Source
pub struct Mag {
    pub log10: f64,
}
Expand description

Non-negative log-magnitude. Mag::ZERO == Mag { log10: -inf }, Mag::ONE == Mag { log10: 0.0 }. All other instances carry a finite log10.

§Save compatibility

Serializes as a plain f64 when the value fits inside one (log10 < 300, well clear of overflow); otherwise as {"log10": <f64>}. Deserialization accepts BOTH shapes — so existing V4 saves with "cuques": 1.234e10 and "MulFactor": 7.0 continue to load correctly, no schema bump required. New saves use the struct form only when the value exceeds the f64 finite range.

Fields§

§log10: f64

Implementations§

Source§

impl Mag

Source

pub const ZERO: Mag

Source

pub const ONE: Mag

Source

pub fn from_f64(v: f64) -> Mag

Construct from an f64. Non-finite / negative inputs collapse to ZERO — there is no honest log-magnitude for them and silently turning a NaN into a zero is friendlier than panicking on a codepath the player never asked for.

Source

pub fn to_f64(self) -> f64

Decode back to an f64. Clamps very-large log10 values to f64::MAX — losing precision in the high end is the price of asking for an f64; the Mag representation itself stays faithful. Use this at the display boundary or wherever a downstream API truly demands f64. Internal math should chain Mag ops.

Source

pub fn is_zero(self) -> bool

Source

pub fn mul(self, rhs: Mag) -> Mag

Source

pub fn div(self, rhs: Mag) -> Mag

Source

pub fn add(self, rhs: Mag) -> Mag

Source

pub fn try_sub(self, rhs: Mag) -> Option<Mag>

Saturating subtraction: returns Some(self - rhs) when self >= rhs, otherwise None. Cuque spending uses this on purchase — the caller already gated on affordable, so None means a logic bug, not a regular game state.

Source

pub fn saturating_sub(self, rhs: Mag) -> Mag

Saturating subtraction with floor at zero. Convenience for paths that don’t care about the underflow case (e.g. visual tween targets).

Source

pub fn pow_i(self, n: i32) -> Mag

Raise to an integer power. pow(0) == ONE, pow(n) = self * self * …. Cheap: just multiplies the log by n.

0^n for any n != 0 returns ZERO. Mathematically 0^negative is undefined, but returning ZERO is the conservative choice for this codebase: it propagates “no contribution” downstream rather than producing an Infinity, which would reintroduce the bug class the Mag type exists to kill.

Source

pub fn floor_u64(self) -> u64

Floored to_f64, useful for “how many of these can the player afford” math. Clamps to u64::MAX.

Trait Implementations§

Source§

impl Add for Mag

Source§

type Output = Mag

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Mag) -> Mag

Performs the + operation. Read more
Source§

impl Clone for Mag

Source§

fn clone(&self) -> Mag

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 Debug for Mag

Source§

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

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

impl Default for Mag

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Mag

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

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

impl Div for Mag

Source§

type Output = Mag

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Mag) -> Mag

Performs the / operation. Read more
Source§

impl From<f64> for Mag

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Mag

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Mag

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl Mul for Mag

Source§

type Output = Mag

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mag) -> Mag

Performs the * operation. Read more
Source§

impl PartialEq for Mag

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 Mag

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 Serialize for Mag

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

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

impl Copy for Mag

Auto Trait Implementations§

§

impl Freeze for Mag

§

impl RefUnwindSafe for Mag

§

impl Send for Mag

§

impl Sync for Mag

§

impl Unpin for Mag

§

impl UnsafeUnpin for Mag

§

impl UnwindSafe for Mag

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> 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> 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.
Source§

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