frac

This module has many functions to deal with simple fractions: fractions with an integer numerator and denominator.

If you need to…You can use…
create a fractionnew, from_int, approximate
get the numerator/denominatornumerator, denominator
simplify a fractionto_lowest_terms, to_mixed_numbers
combine fractions togetheradd, subtract, multiply, divide
turn a fraction into a floatto_float
compare fractionscompare

Types

A simple fraction with an integer numerator and denominator.

pub opaque type Fraction

Functions

pub fn add(one: Fraction, other: Fraction) -> Fraction

Adds two fractions together.

Note that the result is not reduced to its lowest terms, if you need that you can use the fraction.to_lowest_terms function.

Examples

let summed = sum(new(2, 4), new(3, 4))
assert to_lowest_terms(summed) == new(5, 4)
pub fn approximate(
  from float: Float,
  max_denominator limit: Int,
) -> Fraction

Approximates a float into a simple fraction whose denominator is at most max_denominator.

⚠️ This might result in a fraction that does not exactly yield the float number passed in as input. Precision only goes as far as 5 decimal digits.

Examples

assert approximate(0.333, 10) == new(1, 3)
assert approximate(0.2, 10) == new(1, 5)
pub fn compare(one: Fraction, with other: Fraction) -> Order

Compares two fractions.

Examples

assert compare(new(1, 2), new(3, 2)) == Lt
assert compare(new(1, 2), new(1, 2)) == Eq
assert compare(new(1, 2), new(1, 3)) == Gt
pub fn denominator(fraction: Fraction) -> Int

Returns the denominator of a fraction.

pub fn divide(one: Fraction, by other: Fraction) -> Fraction

Divides one fraction by another.

Note that the result is not reduced to its lowest terms, if you need that you can use the fraction.to_lowest_terms function.

Examples

assert divide(new(2, 4), new(1, 3)) == new(6, 4)
pub fn from_int(numerator: Int) -> Fraction

Creates a new fraction with the given nominator and denominator 1.

Examples

assert numerator(from_int(10)) == 10
assert denominator(from_int(10)) == 1
pub fn multiply(one: Fraction, by other: Fraction) -> Fraction

Multiplies one fraction by another.

Note that the result is not reduced to its lowest terms, if you need that you can use the fraction.to_lowest_terms function.

Examples

assert multiply(new(1, 2), by: new(4, 5)) == new(4, 10)
pub fn new(
  numerator numerator: Int,
  denominator denominator: Int,
) -> Fraction

Creates a new fraction with the given numerator and denominator.

Note that this function ensures that only the numerator can be negative, so new(1, -2) will be the same as new(-1, 2).

Examples

assert numerator(new(1, 2)) == 1
assert denominator(new(1, 2)) == 2
pub fn numerator(fraction: Fraction) -> Int

Returns the numerator of a fraction.

pub fn subtract(one: Fraction, other: Fraction) -> Fraction

Subtracts one fraction from another.

Note that the result is not reduced to its lowest terms, if you need that you can use the fraction.to_lowest_terms function.

Examples

let subtracted = subtract(new(2, 4), new(3, 4))
assert to_lowest_terms(subtracted) == new(-1, 4)
pub fn to_float(fraction: Fraction) -> Float

Returns the floating point number you get by dividing the numerator of a fraction by its denominator.

Note that if the denominator is 0 you’ll get 0 as the result.

Examples

assert to_float(Fraction(3, 2)) == 1.5
assert to_float(Fraction(2, 0)) == 0.0
pub fn to_lowest_terms(fraction: Fraction) -> Fraction

Reduces a fraction to its lowest terms.

Examples

assert to_lowest_terms(new(2, 4)) == new(1, 3)
assert to_lowest_terms(new(6, 4)) == new(3, 2)
pub fn to_mixed_numbers(fraction: Fraction) -> #(Int, Fraction)

Turns the fraction into a mixed number: that is an integer and a proper fraction that added together produce the original fraction.

Examples

assert to_mixed_numbers(Fraction(11, 4)) == #(2, Fraction(3, 4))
// 2 + 3/4 is equal to 11/4
Search Document