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
impl Fraction
Sourcepub fn new(numerator: i128, denominator: i128) -> Self
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);
Sourcepub fn from_float(value: f64) -> Self
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);
Sourcepub fn add_number(&self, other: Self) -> Self
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));
Sourcepub fn mul_number(&self, other: Self) -> Self
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));
Sourcepub fn div_number(&self, other: Self) -> Self
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));
Sourcepub fn sub_number(&self, other: Self) -> Self
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));
Trait Implementations§
Source§impl AddAssign for Fraction
impl AddAssign for Fraction
Source§fn add_assign(&mut self, other: Self)
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 DivAssign for Fraction
impl DivAssign for Fraction
Source§fn div_assign(&mut self, other: Self)
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
impl Mul for Fraction
Source§impl MulAssign for Fraction
impl MulAssign for Fraction
Source§fn mul_assign(&mut self, other: Self)
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 SubAssign for Fraction
impl SubAssign for Fraction
Source§fn sub_assign(&mut self, other: Self)
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));
impl Copy for Fraction
impl StructuralPartialEq for Fraction
Auto Trait Implementations§
impl Freeze for Fraction
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
Mutably borrows from an owned value. Read more