glib/fraction

General Fraction functions Fraction type allow for accurate storage and manipulation of fractions

There is a current limitation of 9007199254740991 for the numerator and denominator. This is because this is the largest ‘safe’ integer in Javascript.

Types

pub type Fraction {
  Fraction(numerator: Int, denominator: Int)
}

Constructors

  • Fraction(numerator: Int, denominator: Int)
pub type FractionError {
  ZeroDenominator(a: String)
  Overflow(a: String)
  TooLarge(a: String)
  ConversionError(a: String)
  DivideByZero(a: String)
}

Constructors

  • ZeroDenominator(a: String)
  • Overflow(a: String)
  • TooLarge(a: String)
  • ConversionError(a: String)
  • DivideByZero(a: String)

Functions

pub fn abs(fr: Fraction) -> Result(Fraction, FractionError)
pub fn add(
  fr1: Fraction,
  fr2: Fraction,
) -> Result(Fraction, FractionError)
pub fn compare(fr1: Fraction, fr2: Fraction) -> Order
pub fn denominator(fr1: Fraction) -> Int
pub fn divide(
  fr1: Fraction,
  fr2: Fraction,
) -> Result(Fraction, FractionError)
pub fn equals(fr1: Fraction, fr2: Fraction) -> Bool
pub fn from_float(num: Float) -> Result(Fraction, FractionError)

Generates a Fraction from given float Returns error if number is larger than the max int value

Examples

let fr = from_float(0.75)
// -> Ok(Fraction(3, 4))
let fr = from_float(0.66666)
// -> Ok(Fraction(2, 3))
pub fn from_string(
  fraction: String,
) -> Result(Fraction, FractionError)
pub fn inverse(fr1: Fraction) -> Result(Fraction, FractionError)
pub fn multiply(
  fr1: Fraction,
  fr2: Fraction,
) -> Result(Fraction, FractionError)
pub fn negate(fr: Fraction) -> Result(Fraction, FractionError)
pub fn new(
  numerator: Int,
  denominator: Int,
) -> Result(Fraction, FractionError)
pub fn new2(
  whole: Int,
  numerator: Int,
  denominator: Int,
) -> Result(Fraction, FractionError)
pub fn numerator(fr1: Fraction) -> Int
pub fn power(
  fr1: Fraction,
  pow: Int,
) -> Result(Fraction, FractionError)
pub fn proper_numerator(fr1: Fraction) -> Int
pub fn proper_whole(fr1: Fraction) -> Int
pub fn reduce(fr: Fraction) -> Result(Fraction, FractionError)
pub fn sub(
  fr1: Fraction,
  fr2: Fraction,
) -> Result(Fraction, FractionError)
pub fn to_float(fr1: Fraction) -> Float
pub fn to_int(fr1: Fraction) -> Int
pub fn to_proper_string(fr1: Fraction) -> String
pub fn to_string(fr1: Fraction) -> String
Search Document