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_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.

Examples

assert sum(new(2, 4), new(3, 4)) == 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.

Examples

assert divide(new(2, 4), new(1, 3)) == new(3, 2)
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.

Examples

assert multiply(new(1, 2), by: new(4, 5)) == new(2, 5)
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.

Examples

assert subtract(new(2, 4), new(3, 4)) == 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(new(3, 2)) == 1.5
assert to_float(new(2, 0)) == 0.0
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(new(11, 4)) == #(2, new(3, 4))
// 2 + 3/4 is equal to 11/4
Search Document