Expand description

Fractions

This is the fraction module.

Examples

use lemonmath::fraction::Fraction;

// Create a fraction from a numerator and denominator
let x = Fraction::new(1, 2);
// Create a fraction from a float
let y = Fraction::from_float(10.2082);

// You can display fractions
println!("{}", x);
aseert_eq!(format!("{}", x), "1/2");

// You can add fractions
let z = x + y;
assert_eq!(z, Fraction::new(11, 2));

// You can subtract fractions
let z = x - y;
assert_eq!(z, Fraction::new(-9, 2));

// You can multiply fractions
let z = x * y;
assert_eq!(z, Fraction::new(10, 1));

// You can divide fractions
let z = x / y;
assert_eq!(z, Fraction::new(5, 1));

// You can add assign fractions
let mut x = Fraction::new(1, 2);
x += y;
assert_eq!(x, Fraction::new(11, 2));

// You can sub assign fractions
let mut x = Fraction::new(1, 2);
x -= y;
assert_eq!(x, Fraction::new(-9, 2));

// You can mul assign fractions
let mut x = Fraction::new(1, 2);
x *= y;
assert_eq!(x, Fraction::new(10, 1));

// You can div assign fractions
let mut x = Fraction::new(1, 2);
x /= y;
assert_eq!(x, Fraction::new(5, 1));

Structs

A struct to replace floats