#[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
impl Fraction
sourcepub fn new(numerator: i16, denominator: i16) -> Self
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
.
sourcepub const fn denominator(&self) -> i16
pub const fn denominator(&self) -> i16
Returns the denominator of the fraction.
sourcepub const fn is_positive(&self) -> bool
pub const fn is_positive(&self) -> bool
Returns true if the fraction is positive (greater than zero).
Note: Zero is neither negative nor positive.
sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns true if the fraction is negative (less than zero).
Note: Zero is neither negative nor positive.
sourcepub fn into_compound(self) -> (i16, Fraction)
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);
sourcepub fn round_with_amount(self) -> (i16, Fraction)
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));
Trait Implementations§
source§impl AddAssign<f32> for Fraction
impl AddAssign<f32> for Fraction
source§fn add_assign(&mut self, rhs: f32)
fn add_assign(&mut self, rhs: f32)
+=
operation. Read moresource§impl AddAssign<i16> for Fraction
impl AddAssign<i16> for Fraction
source§fn add_assign(&mut self, rhs: i16)
fn add_assign(&mut self, rhs: i16)
+=
operation. Read moresource§impl AddAssign for Fraction
impl AddAssign for Fraction
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Fraction
impl<'de> Deserialize<'de> for Fraction
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>,
source§impl DivAssign<f32> for Fraction
impl DivAssign<f32> for Fraction
source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read moresource§impl DivAssign<i16> for Fraction
impl DivAssign<i16> for Fraction
source§fn div_assign(&mut self, rhs: i16)
fn div_assign(&mut self, rhs: i16)
/=
operation. Read moresource§impl DivAssign for Fraction
impl DivAssign for Fraction
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/=
operation. Read moresource§impl MulAssign<f32> for Fraction
impl MulAssign<f32> for Fraction
source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read moresource§impl MulAssign<i16> for Fraction
impl MulAssign<i16> for Fraction
source§fn mul_assign(&mut self, rhs: i16)
fn mul_assign(&mut self, rhs: i16)
*=
operation. Read moresource§impl MulAssign for Fraction
impl MulAssign for Fraction
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moresource§impl Ord for Fraction
impl Ord for Fraction
source§impl PartialEq for Fraction
impl PartialEq for Fraction
source§impl PartialOrd for Fraction
impl PartialOrd for Fraction
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<f32> for Fraction
impl SubAssign<f32> for Fraction
source§fn sub_assign(&mut self, rhs: f32)
fn sub_assign(&mut self, rhs: f32)
-=
operation. Read moresource§impl SubAssign<i16> for Fraction
impl SubAssign<i16> for Fraction
source§fn sub_assign(&mut self, rhs: i16)
fn sub_assign(&mut self, rhs: i16)
-=
operation. Read moresource§impl SubAssign for Fraction
impl SubAssign for Fraction
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read moreimpl Copy for Fraction
impl Eq for Fraction
impl StructuralEq for Fraction
impl StructuralPartialEq for Fraction
Auto Trait Implementations§
impl RefUnwindSafe for Fraction
impl Send for Fraction
impl Sync for Fraction
impl Unpin for Fraction
impl UnwindSafe for Fraction
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
§impl<A> Cast for A
impl<A> Cast for A
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<Unit> IntoComponents<Unit> for Unitwhere
Unit: Copy,
impl<Unit> IntoComponents<Unit> for Unitwhere Unit: Copy,
source§fn into_components(self) -> (Unit, Unit)
fn into_components(self) -> (Unit, Unit)
source§fn to_vec<Type>(self) -> Typewhere
Type: FromComponents<Unit>,
fn to_vec<Type>(self) -> Typewhere Type: FromComponents<Unit>,
FromComponents
and
IntoComponents
.