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 denominator(fr1: Fraction) -> Int
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 new2(
whole: Int,
numerator: Int,
denominator: Int,
) -> Result(Fraction, FractionError)
pub fn proper_numerator(fr1: Fraction) -> Int
pub fn proper_whole(fr1: Fraction) -> Int
pub fn to_proper_string(fr1: Fraction) -> String