Struct Fraction

Source
#[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 Fraction<u8>

Source

pub const fn const_new(numer: u8, denom: u8) -> Self

Source

pub const fn const_pct(percent: u8) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<u16>

Source

pub const fn const_new(numer: u16, denom: u16) -> Self

Source

pub const fn const_pct(percent: u16) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<u32>

Source

pub const fn const_new(numer: u32, denom: u32) -> Self

Source

pub const fn const_pct(percent: u32) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<u64>

Source

pub const fn const_new(numer: u64, denom: u64) -> Self

Source

pub const fn const_pct(percent: u64) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<u128>

Source

pub const fn const_new(numer: u128, denom: u128) -> Self

Source

pub const fn const_pct(percent: u128) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<usize>

Source

pub const fn const_new(numer: usize, denom: usize) -> Self

Source

pub const fn const_pct(percent: usize) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<i8>

Source

pub const fn const_new(numer: i8, denom: i8) -> Self

Source

pub const fn const_pct(percent: i8) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<i16>

Source

pub const fn const_new(numer: i16, denom: i16) -> Self

Source

pub const fn const_pct(percent: i16) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<i32>

Source

pub const fn const_new(numer: i32, denom: i32) -> Self

Source

pub const fn const_pct(percent: i32) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<i64>

Source

pub const fn const_new(numer: i64, denom: i64) -> Self

Source

pub const fn const_pct(percent: i64) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<i128>

Source

pub const fn const_new(numer: i128, denom: i128) -> Self

Source

pub const fn const_pct(percent: i128) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl Fraction<isize>

Source

pub const fn const_new(numer: isize, denom: isize) -> Self

Source

pub const fn const_pct(percent: isize) -> Self

Source

pub const fn const_reduce(self) -> Self

Source§

impl<I: Int> Fraction<I>

Source

pub fn new(numer: I, denom: I) -> Self

Source

pub fn numer(&self) -> I

Source

pub fn denom(&self) -> I

Source

pub fn pct(percent: I) -> Self

Source

pub fn reduced_pow2(self) -> Self

Source

pub fn reduce(&mut self)

Source

pub fn reduced(self) -> Self

Source

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.

Source

pub const fn from_int(value: I) -> Self

Create the fraction value / 1.

Source

pub fn is_positive(&self) -> bool

Returns true if number is less than zero.

Source

pub fn abs(&self) -> Self

Source

pub fn is_zero(&self) -> bool

Returns true if number is zero.

Source

pub fn is_negative(&self) -> bool

Returns true if number is less than zero.

Source

pub fn floor(self) -> I

Source

pub fn ceil(self) -> I

Source

pub fn trunc(self) -> I

Source

pub fn round(self) -> I

Source

pub fn fract(self) -> Self

Returns a truncated fraction part of this value.

Source

pub fn into_mixed_number(self) -> (I, Self)

Returns an integer and fraction part of this value.

Source

pub fn to_mixed_number(&mut self) -> I

Truncate the integer part and return it.

Trait Implementations§

Source§

impl<T: Int> Add<T> for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Int> Add for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Int> AddAssign<T> for Fraction<T>

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl<T: Int> AddAssign for Fraction<T>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<I: Clone + Int> Clone for Fraction<I>

Source§

fn clone(&self) -> Fraction<I>

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

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

Performs copy-assignment from source. Read more
Source§

impl<I: Debug + Int> Debug for Fraction<I>

Source§

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

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

impl<I: Int> Default for Fraction<I>

Source§

fn default() -> Self

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

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>,

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

impl<T: Int> Div<T> for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Int> Div for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Int> DivAssign<T> for Fraction<T>

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl<T: Int> DivAssign for Fraction<T>

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl<I: Int + Clone> Float for Fraction<I>

Source§

fn floor(self) -> Self

Source§

fn ceil(self) -> Self

Source§

fn trunc(self) -> Self

Source§

fn round(self) -> Self

Source§

impl<T: Int> From<T> for Fraction<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Int> Mul<T> for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Int> Mul for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Int> MulAssign<T> for Fraction<T>

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl<T: Int> MulAssign for Fraction<T>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<I: Int> NumCast<Fraction<I>> for I

Source§

fn cast(self) -> Fraction<I>

Source§

impl<I: Int> NumCast<I> for Fraction<I>

Source§

fn cast(self) -> I

Source§

impl<I: Int + Clone> Number for Fraction<I>

Source§

const ZERO: Self

Source§

const ONE: Self

Source§

const MIN_VALUE: Self

Source§

const MAX_VALUE: Self

Source§

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

Source§

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

Source§

impl<I: Int> Ord for Fraction<I>

Source§

fn cmp(&self, other: &Self) -> 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<I: Int> PartialEq for Fraction<I>

Source§

fn eq(&self, other: &Self) -> 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<I: Int> PartialOrd for Fraction<I>

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 · 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<I> Serialize for Fraction<I>
where I: Serialize + Int,

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<T: Int> Sub<T> for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Int> Sub for Fraction<T>

Source§

type Output = Fraction<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Int> SubAssign<T> for Fraction<T>

Source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
Source§

impl<T: Int> SubAssign for Fraction<T>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<I> TypePath for Fraction<I>
where Fraction<I>: Any + Send + Sync, I: TypePath + Int,

Source§

fn type_path() -> &'static str

Returns the fully qualified path of the underlying type. Read more
Source§

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>

Returns the name of the type, or None if it is anonymous. Read more
Source§

fn crate_name() -> Option<&'static str>

Returns the name of the crate the type is in, or None if it is anonymous. Read more
Source§

fn module_path() -> Option<&'static str>

Returns the path to the module the type is in, or None if it is anonymous. Read more
Source§

impl<I: Copy + Int> Copy for Fraction<I>

Source§

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> 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<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

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
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynEq for T
where T: Any + Eq,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Casts the type to dyn Any.
Source§

fn dyn_eq(&self, other: &(dyn DynEq + 'static)) -> bool

This method tests for self and other values to be equal. Read more
Source§

impl<T> DynamicTypePath for T
where T: TypePath,

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> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

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> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

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> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

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

Source§

impl<T> Shareable for T
where T: Clone + Debug + Send + Sync + 'static,