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: f64Implementations§
Source§impl Mag
impl Mag
pub const ZERO: Mag
pub const ONE: Mag
Sourcepub fn from_f64(v: f64) -> Mag
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.
Sourcepub fn to_f64(self) -> f64
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.
pub fn is_zero(self) -> bool
pub fn mul(self, rhs: Mag) -> Mag
pub fn div(self, rhs: Mag) -> Mag
pub fn add(self, rhs: Mag) -> Mag
Sourcepub fn try_sub(self, rhs: Mag) -> Option<Mag>
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.
Sourcepub fn saturating_sub(self, rhs: Mag) -> Mag
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).
Sourcepub fn pow_i(self, n: i32) -> Mag
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.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Mag
impl<'de> Deserialize<'de> for Mag
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Source§impl PartialOrd for Mag
impl PartialOrd for Mag
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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