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
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);
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);
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));
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));
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));
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
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));
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));
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));
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));
Auto Trait Implementations
impl RefUnwindSafe for Fraction
impl UnwindSafe for Fraction
Blanket Implementations
Mutably borrows from an owned value. Read more