Struct figures::Fraction

source ·
#[repr(C)]
pub struct Fraction { /* private fields */ }
Expand description

A fraction type for predictable integer-based math.

Internally this type uses 32 bits of data to represent a fraction:

  • 1 bit of data for the positive/negative sign.
  • 15 bits of data for the numerator
  • 16 bits of data for the denominator

Many math operations are performed using temporary 32-bit values for the fraction, simplifing at the end of the operation. This prevents overflows, but does not prevent precision loss. We can see this by purposely buliding fractions that are hard to represent:

use figures::fraction;

assert_eq!(
    fraction!(1 / 32719) + fraction!(1 / 32749),
    fraction!(2 / 32749)
);

The above example adds fractions that have denominators using two largest primes that fit in 16 bits. The result should be 65,468/1,071,514,531, but that denominator is clearly too large. Because the fractions being added had prime numbers for their denominators, there is no way to reduce this fraction without losing information. For this particular example, a precision loss of ~2.8e-8 occurs.

However, in 2d graphics programming, it’s rare to be working with irrational numbers outside of angles represented in radians.

Implementations§

source§

impl Fraction

source

pub const MAX: Self = _

The maximum value representable by this type.

source

pub const MIN: Self = _

The minimum value representable by this type.

source

pub const ONE: Self = _

A fraction equivalent to 1.

source

pub const PI: Self = _

A fractional approximation of Pi, accurate to within 2.67e-7.

source

pub const ZERO: Self = _

A fraction equivalent to 0.

source

pub const fn new_whole(whole_number: i16) -> Self

Returns a new fraction for a whole number.

source

pub fn new(numerator: i16, denominator: i16) -> Self

Returns a new fraction using the components provided.

denominator will be limited to the absolute value of i16::MIN.

source

pub const fn numerator(&self) -> i16

Returns the numerator of the fraction.

source

pub const fn denominator(&self) -> i16

Returns the denominator of the fraction.

source

pub const fn is_positive(&self) -> bool

Returns true if the fraction is positive (greater than zero).

Note: Zero is neither negative nor positive.

source

pub const fn is_zero(&self) -> bool

Returns true if the fraction is zero.

source

pub const fn is_negative(&self) -> bool

Returns true if the fraction is negative (less than zero).

Note: Zero is neither negative nor positive.

source

pub fn into_compound(self) -> (i16, Fraction)

Simplifies the fraction into a compound number.

use figures::Fraction;

assert_eq!(
    Fraction::new(1, 3).into_compound(),
    (0, Fraction::new(1, 3))
);
assert_eq!(
    Fraction::new(4, 3).into_compound(),
    (1, Fraction::new(1, 3))
);
assert_eq!(
    Fraction::new(-4, 3).into_compound(),
    (-1, Fraction::new(-1, 3))
);

Adding the number and the fraction back together will result in the original fraction.

use figures::Fraction;

let improper = Fraction::new(-4, 3);
let (whole, fraction) = improper.into_compound();
assert_eq!(Fraction::from(whole) + fraction, improper);
source

pub fn round(self) -> i16

Rounds this fraction to the nearest whole number.

source

pub fn round_with_amount(self) -> (i16, Fraction)

Rounds this fraction to the nearest whole number, returning the fractional component that was rounded away. This fraction can be added to the whole number to reconstruct the original fraction.

use figures::Fraction;

let (whole, fraction) = Fraction::new(5, 3).round_with_amount();
assert_eq!(whole, 2);
assert_eq!(fraction, Fraction::new(-1, 3));
assert_eq!(Fraction::new_whole(whole) + fraction, Fraction::new(5, 3));

let (whole, fraction) = Fraction::new(-5, 3).round_with_amount();
assert_eq!(whole, -2);
assert_eq!(fraction, Fraction::new(1, 3));
assert_eq!(Fraction::new_whole(whole) + fraction, Fraction::new(-5, 3));
source

pub fn into_f32(self) -> f32

Returns this fraction as a floating point number.

source

pub const fn inverse(self) -> Self

Returns the inverse of this fraction.

source

pub const fn abs(self) -> Self

Returns the absolute value of this fraction.

source

pub fn atan(self) -> Angle

Returns the arctangent of this fraction.

This function is implemented using a lookup table and is an approximation.

source

pub fn atan2(self, other: Self) -> Angle

Returns the result of arctan(self/other) while correctly handling negative numbers.

Trait Implementations§

source§

impl Add<f32> for Fraction

§

type Output = Fraction

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<i16> for Fraction

§

type Output = Fraction

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add for Fraction

§

type Output = Fraction

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl AddAssign<f32> for Fraction

source§

fn add_assign(&mut self, rhs: f32)

Performs the += operation. Read more
source§

impl AddAssign<i16> for Fraction

source§

fn add_assign(&mut self, rhs: i16)

Performs the += operation. Read more
source§

impl AddAssign for Fraction

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl Clone for Fraction

source§

fn clone(&self) -> Fraction

Returns a copy 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 Fraction

source§

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

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

impl<'de> Deserialize<'de> for Fraction

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 Fraction

source§

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

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

impl Div<Fraction> for Lp

§

type Output = Lp

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<Fraction> for Px

§

type Output = Px

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<Fraction> for UPx

§

type Output = UPx

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<Fraction> for i32

§

type Output = i32

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<Fraction> for u32

§

type Output = u32

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<f32> for Fraction

§

type Output = Fraction

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<i16> for Fraction

§

type Output = Fraction

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div for Fraction

§

type Output = Fraction

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl DivAssign<f32> for Fraction

source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
source§

impl DivAssign<i16> for Fraction

source§

fn div_assign(&mut self, rhs: i16)

Performs the /= operation. Read more
source§

impl DivAssign for Fraction

source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
source§

impl From<Fraction> for Angle

source§

fn from(value: Fraction) -> Self

Converts to this type from the input type.
source§

impl From<Fraction> for f32

source§

fn from(value: Fraction) -> Self

Converts to this type from the input type.
source§

impl From<f32> for Fraction

source§

fn from(scale: f32) -> Self

Converts to this type from the input type.
source§

impl From<i16> for Fraction

source§

fn from(numerator: i16) -> Self

Converts to this type from the input type.
source§

impl Hash for Fraction

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<Fraction> for Lp

§

type Output = Lp

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<Fraction> for Px

§

type Output = Px

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<Fraction> for UPx

§

type Output = UPx

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<Fraction> for i32

§

type Output = i32

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<Fraction> for u32

§

type Output = u32

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<f32> for Fraction

§

type Output = Fraction

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<i16> for Fraction

§

type Output = Fraction

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul for Fraction

§

type Output = Fraction

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl MulAssign<f32> for Fraction

source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
source§

impl MulAssign<i16> for Fraction

source§

fn mul_assign(&mut self, rhs: i16)

Performs the *= operation. Read more
source§

impl MulAssign for Fraction

source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
source§

impl Neg for Fraction

§

type Output = Fraction

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl Ord for Fraction

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) -> Selfwhere Self: Sized,

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

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Fraction

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Fraction

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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Fraction

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<f32> for Fraction

§

type Output = Fraction

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<i16> for Fraction

§

type Output = Fraction

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub for Fraction

§

type Output = Fraction

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl SubAssign<f32> for Fraction

source§

fn sub_assign(&mut self, rhs: f32)

Performs the -= operation. Read more
source§

impl SubAssign<i16> for Fraction

source§

fn sub_assign(&mut self, rhs: i16)

Performs the -= operation. Read more
source§

impl SubAssign for Fraction

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl TryFrom<i128> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: i128) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i32> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: i32) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i64> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: i64) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<isize> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: isize) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u128> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: u128) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u16> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: u16) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u32> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: u32) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u64> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: u64) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<usize> for Fraction

§

type Error = TryFromIntError

The type returned in the event of a conversion error.
source§

fn try_from(value: usize) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for Fraction

source§

impl Eq for Fraction

source§

impl StructuralEq for Fraction

source§

impl StructuralPartialEq for Fraction

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<A> Cast for A

§

fn cast<To>(self) -> Towhere To: CastFrom<A>,

Casts self to the To type. This may be a lossy operation.
§

impl<A> CastFrom<A> for A

§

fn from_cast(from: A) -> A

Returns from as Self.
§

impl<A, B> CastInto<A> for Bwhere A: CastFrom<B>,

§

fn cast_into(self) -> A

Returns self as To.
§

impl<Q, K> Comparable<K> for Qwhere Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

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

Compare self to key and return their ordering.
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

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

§

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

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

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

§

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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<Unit> IntoComponents<Unit> for Unitwhere Unit: Copy,

source§

fn into_components(self) -> (Unit, Unit)

Extracts this type’s 2d vector components.
source§

fn to_vec<Type>(self) -> Typewhere Type: FromComponents<Unit>,

Converts this type to another type using FromComponents and IntoComponents.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

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

§

impl<T> WasmNotSend for Twhere T: Send,

§

impl<T> WasmNotSendSync for Twhere T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for Twhere T: Sync,