This library provides a safe and simple API to deal with fractions.
Zero is an invalid denominator value because it implies division by zero, which is mathematically undefined.
The minimum supported integer that should be used with the Fraction module.
This is because negate Random.minInt is 1 greater than Random.maxInt, so Fraction.simplify doesn't work correctly.
Attempts to create a Fraction.
create 1 2 == Just (Fraction 1 2)
create 1 0 == NothingWARNING: This should be used as a last resort. This is intended for use with integer literals, not with user input. Never trust user input. If an invalid denominator or an out of bounds integer is supplied, weird behavior could occur.
createUnsafe 4 3 == Fraction 4 3Gets the numerator.
getNumerator (Fraction 3 5) == 3Gets the denominator.
getDenominator (Fraction 3 5) == 5Checks if a fraction is a whole number. Simplifies the input, then checks if the denominator is 1.
isWholeNumber (Fraction 5 3) == False
isWholeNumber (Fraction 8 1) == True
isWholeNumber (Fraction 8 2) == TrueChecks if a fraction is equal to zero.
isZero (Fraction 43 32) == False
isZero (Fraction 0 3) == TrueChecks if a fraction is equal to one.
isOne (Fraction 43 32) == False
isOne (Fraction 3 3) == TrueChecks if a fraction is equal to negative one.
isNegativeOne (Fraction 43 32) == False
isNegativeOne (Fraction -3 3) == True
isNegativeOne (Fraction 3 -3) == TrueAttempts to take the reciprocal of a fraction. This returns a Maybe because the
numerator could be zero and then be swapped to the denominator, which is invalid.
reciprocal (Fraction 3 4) == Just (Fraction 4 3)
reciprocal (Fraction 0 4) == NothingPuts a Fraction in the simplest possible terms.
simplify (Fraction 5 15) == Fraction 1 3
simplify (Fraction 3 4) == Fraction 3 4Adds two fractions to get their sum. Does no simplification of the result.
add (Fraction 2 3) (Fraction 1 2) == Fraction 7 6Subtracts two fractions to get their difference. Does no simplification of the result.
subtract (Fraction 5 9) (Fraction 1 2) == Fraction 1 18Multiplies two fractions to get their product. Does no simplification of the result.
multiply (Fraction 2 3) (Fraction 3 4) == Fraction 6 12Divides two fractions to get their quotient. Does no simplification of the result.
divide (Fraction 2 3) (Fraction 3 4) == Just (Fraction 8 9)
divide (Fraction 0 2) (Fraction 4 7) == NothingConverts fraction1 and fraction2 to the same denominator.
Similar to convertToSameDenominator, but is ideal for use cases involving 3+ fractions.
Gets the floating point representation of the fraction.
fractionToFloat (Fraction 1 2) == 0.5Rounds a fraction to the nearest integer.
The underlying opaque type of the Fraction module. Contains a numerator and a denominator.