#[repr(C)]pub struct Fraction<I: Int> { /* private fields */ }
Expand description
Represents a fractional number.
§Type Contract
All combinations of numbers and signs are allowed, as long as denominator is not 0.
Some operations like new
will perform reduction on the value while others won’t for performance.
§Reductions
Only new
does full reduction, operators only do partial reduction for powers of 2.
In the context of bevy_stat_query
, use simple numbers like 1/3
over complicated
ones like 33/100
to avoid integer overflows.
Implementations§
Source§impl<I: Int> Fraction<I>
impl<I: Int> Fraction<I>
pub fn new(numer: I, denom: I) -> Self
pub fn numer(&self) -> I
pub fn denom(&self) -> I
pub fn pct(percent: I) -> Self
pub fn reduced_pow2(self) -> Self
pub fn reduce(&mut self)
pub fn reduced(self) -> Self
Sourcepub const fn new_raw(numer: I, denom: I) -> Self
pub const fn new_raw(numer: I, denom: I) -> Self
Create a unreduced fraction, does not breach the type contract as long as denom
is not 0.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if number is less than zero.
pub fn abs(&self) -> Self
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if number is less than zero.
pub fn floor(self) -> I
pub fn ceil(self) -> I
pub fn trunc(self) -> I
pub fn round(self) -> I
Sourcepub fn into_mixed_number(self) -> (I, Self)
pub fn into_mixed_number(self) -> (I, Self)
Returns an integer and fraction part of this value.
Sourcepub fn to_mixed_number(&mut self) -> I
pub fn to_mixed_number(&mut self) -> I
Truncate the integer part and return it.
Trait Implementations§
Source§impl<T: Int> AddAssign<T> for Fraction<T>
impl<T: Int> AddAssign<T> for Fraction<T>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
Performs the
+=
operation. Read moreSource§impl<T: Int> AddAssign for Fraction<T>
impl<T: Int> AddAssign for Fraction<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+=
operation. Read moreSource§impl<'de, I> Deserialize<'de> for Fraction<I>where
I: Deserialize<'de> + Int,
impl<'de, I> Deserialize<'de> for Fraction<I>where
I: Deserialize<'de> + Int,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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<T: Int> DivAssign<T> for Fraction<T>
impl<T: Int> DivAssign<T> for Fraction<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Performs the
/=
operation. Read moreSource§impl<T: Int> DivAssign for Fraction<T>
impl<T: Int> DivAssign for Fraction<T>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/=
operation. Read moreSource§impl<T: Int> MulAssign<T> for Fraction<T>
impl<T: Int> MulAssign<T> for Fraction<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Performs the
*=
operation. Read moreSource§impl<T: Int> MulAssign for Fraction<T>
impl<T: Int> MulAssign for Fraction<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*=
operation. Read moreSource§impl<I: Int> Ord for Fraction<I>
impl<I: Int> Ord for Fraction<I>
Source§impl<I: Int> PartialOrd for Fraction<I>
impl<I: Int> PartialOrd for Fraction<I>
Source§impl<T: Int> SubAssign<T> for Fraction<T>
impl<T: Int> SubAssign<T> for Fraction<T>
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
Performs the
-=
operation. Read moreSource§impl<T: Int> SubAssign for Fraction<T>
impl<T: Int> SubAssign for Fraction<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-=
operation. Read moreSource§impl<I> TypePath for Fraction<I>
impl<I> TypePath for Fraction<I>
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Returns the fully qualified path of the underlying type. Read more
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Returns a short, pretty-print enabled path to the type. Read more
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
impl<I: Copy + Int> Copy for Fraction<I>
impl<I: Int> Eq for Fraction<I>
Auto Trait Implementations§
impl<I> Freeze for Fraction<I>where
I: Freeze,
impl<I> RefUnwindSafe for Fraction<I>where
I: RefUnwindSafe,
impl<I> Send for Fraction<I>
impl<I> Sync for Fraction<I>
impl<I> Unpin for Fraction<I>where
I: Unpin,
impl<I> UnwindSafe for Fraction<I>where
I: UnwindSafe,
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
See
TypePath::type_path
.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
See
TypePath::type_ident
.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
See
TypePath::crate_name
.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using default()
.