glat

Types

pub type Rational {
  Rational(num: Int, den: Int)
}

Constructors

  • Rational(num: Int, den: Int)

    ⚠⚠ It is unsafe to raw construct this from unknown input, as these functions assume the rational they take in do not have a 0 or negative denominator. You should use new.

Constants

pub const one: Rational

1/1

pub const zero: Rational

0/1

Functions

pub fn absolute_value(rat: Rational) -> Rational

|num| / den

pub fn add(lhs: Rational, rhs: Rational) -> Rational
pub fn ceiling(rat: Rational) -> Rational

Rounds the rational upwards.

pub fn clamp(
  rat: Rational,
  min min_bound: Rational,
  max max_bound: Rational,
) -> Rational

Restricts a rational between a lower and upper bound.

pub fn compare(lhs: Rational, rhs: Rational) -> Order
pub fn digits(
  rat: Rational,
  base: Int,
) -> Result(#(List(Int), List(Int)), Nil)

Splits the numerator and denominator into their digit representations in the specified base. Returns an error if the base is less than 2.

pub fn divide(lhs: Rational, rhs: Rational) -> Rational

Also a new for rationals.

Returns zero if the second rational’s numerator is 0 because you are dividing the first rational by zero.

pub fn divide_check(
  lhs: Rational,
  rhs: Rational,
) -> Result(Rational, Nil)

Also a new for rationals.

Returns Error if the second rational’s numerator is 0, because you are dividing the first rational by zero.

pub fn flip(rat: Rational) -> Rational

Swap the numerator and denominator.

Returns zero if the numerator is 0.

pub fn flip_check(rat: Rational) -> Result(Rational, Nil)

Swap the numerator and denominator.

Returns an Error if the numerator is 0.

pub fn floor(rat: Rational) -> Rational

Rounds the rational upwards.

pub fn fraction(rat: Rational) -> Rational

Get the fractional part of a mixed fraction. Uses truncated division.

pub fn fraction_floor(rat: Rational) -> Rational

Get the fractional part of a mixed fraction. Uses floored division.

pub fn from_float(f: Float) -> Rational

Returns zero if the float is 0.0.

pub fn from_float_check(f: Float) -> Result(Rational, Nil)

Returns Error if the float is 0.0.

pub fn from_int(num: Int) -> Rational

num / 1.

pub fn int_power(base: Int, of exponent: Int) -> Rational

Returns zero if the numerator is 0 and the exponent is negative, because the negative exponent makes it 1/0.

pub fn int_power_check(
  base: Int,
  of exponent: Int,
) -> Result(Rational, Nil)

Returns Error if the numerator is 0 and the exponent is negative, because the negative exponent makes it 1/0.

pub fn is_negative(rat: Rational) -> Bool
pub fn is_positive(rat: Rational) -> Bool
pub fn map_both(rat: Rational, fun: fn(Int) -> Int) -> Rational
pub fn map_both_check(
  rat: Rational,
  fun: fn(Int) -> Int,
) -> Result(Rational, Nil)
pub fn map_den(rat: Rational, fun: fn(Int) -> Int) -> Rational
pub fn map_den_check(
  rat: Rational,
  fun: fn(Int) -> Int,
) -> Result(Rational, Nil)
pub fn map_num(rat: Rational, fun: fn(Int) -> Int) -> Rational
pub fn map_seperate(
  rat: Rational,
  fun1: fn(Int) -> Int,
  fun2: fn(Int) -> Int,
) -> Rational
pub fn map_seperate_check(
  rat: Rational,
  fun1: fn(Int) -> Int,
  fun2: fn(Int) -> Int,
) -> Result(Rational, Nil)
pub fn max(rat1: Rational, rat2: Rational) -> Rational

Returns the largest of two rationals.

pub fn min(rat1: Rational, rat2: Rational) -> Rational

Returns the smallest of two rationals.

pub fn modulo(rat: Rational) -> Int

This uses floor division. See remainder for truncated division.

pub fn multiply(lhs: Rational, rhs: Rational) -> Rational
pub fn negate(rat: Rational) -> Rational
pub fn new(num: Int, over den: Int) -> Rational

Returns zero if the denominator is 0.

pub fn new_check(
  num: Int,
  over den: Int,
) -> Result(Rational, Nil)

Returns Error if the denominator is 0.

pub fn parse(str: String) -> Result(Rational, Nil)

Only parses {num}/{den}.

Returns Error if the parsing fails.
Returns zero if the denominator is 0.

pub fn parse_check(str: String) -> Result(Rational, Nil)

Only parses {num}/{den}.

Returns Error if the parsing fails, or the denominator is 0.

pub fn power(rat: Rational, of exponent: Int) -> Rational

Returns zero if the numerator is 0 and the exponent is negative, because the negative exponent makes it 1/0.

pub fn power_check(
  rat: Rational,
  of exponent: Int,
) -> Result(Rational, Nil)

Returns Error if the numerator is 0 and the exponent is negative, because the negative exponent makes it 1/0.

pub fn product(rats: List(Rational)) -> Rational

Multiplies a list of rationals and returns the product.

pub fn random(over den: Int) -> Rational

This returns (0..=den)/den.
A negative denominator will negate the result.

Returns zero if den is 0.

pub fn random_check(over den: Int) -> Result(Rational, Nil)

This returns (0..=den)/den.
A negative denominator will negate the result.

Returns Error if the integer is 0.

pub fn reduce(rat: Rational) -> Rational

Simplify the rational.

pub fn remainder(rat: Rational) -> Int

This uses truncated division like the % operator does. See modulo for floor division.

pub fn round(rat: Rational) -> Rational

floor if it is less than 1/2, ceiling otherwise.

pub fn square_root(rat: Rational) -> Rational

TODO: make this function (or a new function) not have a float limitation.

Panics if the numerator and denominator are not in the bounds of a float.
Returns zero if the rational is negative.

pub fn square_root_check(rat: Rational) -> Result(Rational, Nil)

TODO: make this function (or a new function) not have a float limitation.

Panics if the numerator and denominator are not in the bounds of a float.
Returns Error if the rational is negative.

pub fn subtract(lhs: Rational, rhs: Rational) -> Rational
pub fn sum(rats: List(Rational)) -> Rational

Sums a list of rationals.

pub fn to_float(rat: Rational) -> Float

Panics if the numerator or denominator are not in range of a Float.

pub fn to_int(rat: Rational) -> Int

Result of num / den. Also getting the whole part of a mixed fraction.

pub fn to_int_floor(rat: Rational) -> Int

Result of int.floor_divide(num, den). Also getting the whole part of a mixed fraction.

pub fn to_string(rat: Rational) -> String

Join the numerator and denominator with a “/”.

pub fn undigits(
  tup: #(List(Int), List(Int)),
  base: Int,
) -> Result(Rational, Nil)

Joins two lists of digits into a rational.

Returns an error if the base is less than 2, or the list contains a digit greater than or equal to the specified base.
Returns zero if the second list of digits equal zero.

pub fn undigits_check(
  tup: #(List(Int), List(Int)),
  base: Int,
) -> Result(Rational, Nil)

Joins two lists of digits into a rational.

Returns Error if the base is less than 2, the list contains a digit greater than or equal to the specified base, or the second list of digits equal zero.

pub fn unwrap(res: Result(Rational, Nil)) -> Rational

Replace error with zero.

Search Document