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: i128denominator: 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));

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

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));

The resulting type after applying the + operator.

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));

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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));

This displays the fraction as a string

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

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));

The resulting type after applying the / operator.

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 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));

The resulting type after applying the * operator.

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 method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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));

The resulting type after applying the - operator.

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.