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 simplify doesn't work correctly.
Attempts to create a Fraction.
create 1 2 == Just (Fraction 1 2)
create 1 0 == NothingProvides feedback based on the error for the user when creating a fraction.
Attempts to create a Fraction.
create 1 2 == Ok (Fraction 1 2)
create 1 0 == Err (DenominatorError "Invalid denominator.")WARNING: 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.
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) == TruePuts 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) == NothingChecks if fraction1 is exactly equal to fraction2.
Converts fraction1 and fraction2 to the same denominator.
Maybe.map2
(\fraction1 fraction2 -> convertToSameDenominator fraction1 fraction2)
(create 1 2)
(create 1 3) -- Just (Fraction 3 6, Fraction 2 6)Similar to convertToSameDenominator, but is ideal for use cases involving 3+ Fractions.
Gets the floating point representation of the Fraction.
Fraction.toFloat (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.