lemonmath::fraction

Struct Fraction

Source
pub struct Fraction {
    pub numerator: i128,
    pub denominator: i128,
}
Expand description

A struct to replace floats

§Examples

use lemonmath::fraction::Fraction;
 
let x = Fraction::from_float(10.2082);
 
println!("{}", x);

Fields§

§numerator: i128§denominator: i128

Implementations§

Source§

impl Fraction

Source

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

This creates a new Fraction from a numerator and denominator.

§Examples
use lemonmath::fraction::Fraction;
let x = Fraction::new(1, 2);
 
assert_eq!(x.numerator, 1);
assert_eq!(x.denominator, 2);
Source

pub fn from_float(value: f64) -> Self

This creates a new Fraction from a float

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::from_float(1.0);
 
assert_eq!(x.numerator, 1);
assert_eq!(x.denominator, 1);
Source

pub fn add_number(&self, other: Self) -> Self

This adds two fractions together

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x.add_number(y), Fraction::new(7, 6));
Source

pub fn mul_number(&self, other: Self) -> Self

This multiplies two fractions together

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x.mul_number(y), Fraction::new(1, 3));
Source

pub fn div_number(&self, other: Self) -> Self

This divides two fractions

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x.div_number(y), Fraction::new(3, 4));
Source

pub fn sub_number(&self, other: Self) -> Self

This subtracts two fractions

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x.sub_number(y), Fraction::new(-1, 6));
Source

pub fn reduce(&self) -> Self

This reduces the fraction to its lowest terms

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(2, 4);
 
assert_eq!(x.reduce(), Fraction::new(1, 2));

Trait Implementations§

Source§

impl Add for Fraction

Source§

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

This adds two fractions together

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x + y, Fraction::new(7, 6));
Source§

type Output = Fraction

The resulting type after applying the + operator.
Source§

impl AddAssign for Fraction

Source§

fn add_assign(&mut self, other: Self)

This adds two fractions together and then assigns the result to the original fraction

§Examples
use lemonmath::fraction::Fraction;
 
let mut x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
x += y;
assert_eq!(x, Fraction::new(7, 6));
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 Default for Fraction

Source§

fn default() -> Self

This returns a fraction with a numerator of 0 and a denominator of 1

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::default();
 
assert_eq!(x, Fraction::new(0, 1));
Source§

impl Display for Fraction

Source§

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

This displays the fraction as a string

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
 
assert_eq!(format!("{}", x), "1/2");
Source§

impl Div for Fraction

Source§

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

This divides two fractions

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x / y, Fraction::new(3, 4));
Source§

type Output = Fraction

The resulting type after applying the / operator.
Source§

impl DivAssign for Fraction

Source§

fn div_assign(&mut self, other: Self)

This divides two fractions and then assigns the result to the original fraction

§Examples
use lemonmath::fraction::Fraction;
 
let mut x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
x /= y;
assert_eq!(x, Fraction::new(3, 4));
Source§

impl Mul for Fraction

Source§

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

This multiplies two fractions together

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x * y, Fraction::new(1, 3));
Source§

type Output = Fraction

The resulting type after applying the * operator.
Source§

impl MulAssign for Fraction

Source§

fn mul_assign(&mut self, other: Self)

This multiplies two fractions and then assigns the result to the original fraction

§Examples
use lemonmath::fraction::Fraction;
     
let mut x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
x *= y;
assert_eq!(x, Fraction::new(1, 3));
Source§

impl PartialEq for Fraction

Source§

fn eq(&self, other: &Fraction) -> 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 Sub for Fraction

Source§

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

This subtracts two fractions

§Examples
use lemonmath::fraction::Fraction;
 
let x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
assert_eq!(x - y, Fraction::new(-1, 6));
Source§

type Output = Fraction

The resulting type after applying the - operator.
Source§

impl SubAssign for Fraction

Source§

fn sub_assign(&mut self, other: Self)

This subtracts two fractions and then assigns the result to the original fraction

§Examples
use lemonmath::fraction::Fraction;
 
let mut x = Fraction::new(1, 2);
let y = Fraction::new(2, 3);
 
x -= y;
assert_eq!(x, Fraction::new(-1, 6));
Source§

impl Copy for Fraction

Source§

impl StructuralPartialEq for Fraction

Auto Trait Implementations§

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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> ToString for T
where 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 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.