This library provides a safe and simple API to deal with fractions.
The underlying opaque type of the Fraction module. Contains a numerator and a denominator.
invalidDenominator : Basics.Int
Zero is an invalid denominator value because it implies division by zero, which is mathematically undefined.
minimumSupportedInt : Basics.Int
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.
create : Basics.Int -> Basics.Int -> Maybe Fraction
Attempts to create a Fraction
.
create 1 2 == Just (Fraction 1 2)
create 1 0 == Nothing
Provides feedback based on the error for the user when creating a fraction.
createWithFeedback : Basics.Int -> Basics.Int -> Result FractionCreationError Fraction
Attempts to create a Fraction
.
create 1 2 == Ok (Fraction 1 2)
create 1 0 == Err (DenominatorError "Invalid denominator.")
createUnsafe : Basics.Int -> Basics.Int -> Fraction
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 3
fromTuple : ( Basics.Int, Basics.Int ) -> Maybe Fraction
Attempts to take a pair of ints and make a Fraction
.
getNumerator : Fraction -> Basics.Int
Gets the numerator.
getNumerator (Fraction 3 5) == 3
getDenominator : Fraction -> Basics.Int
Gets the denominator.
getDenominator (Fraction 3 5) == 5
isWholeNumber : Fraction -> Basics.Bool
Checks if a Fraction
is a whole number.
isWholeNumber (Fraction 5 3) == False
isWholeNumber (Fraction 8 1) == True
isWholeNumber (Fraction 8 2) == True
isZero : Fraction -> Basics.Bool
Checks if a Fraction
is equal to zero.
isZero (Fraction 43 32) == False
isZero (Fraction 0 3) == True
isOne : Fraction -> Basics.Bool
Checks if a Fraction
is equal to one.
isOne (Fraction 43 32) == False
isOne (Fraction 3 3) == True
isNegativeOne : Fraction -> Basics.Bool
Checks if a Fraction
is equal to negative one.
isNegativeOne (Fraction 43 32) == False
isNegativeOne (Fraction -3 3) == True
isNegativeOne (Fraction 3 -3) == True
reciprocal : Fraction -> Maybe Fraction
Attempts 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) == Nothing
simplify : Fraction -> Fraction
Puts a Fraction
in the simplest possible terms.
simplify (Fraction 5 15) == Fraction 1 3
simplify (Fraction 3 4) == Fraction 3 4
add : Fraction -> Fraction -> Fraction
Adds two Fraction
s to get their sum. Does no simplification of the result.
add (Fraction 2 3) (Fraction 1 2) == Fraction 7 6
subtract : Fraction -> Fraction -> Fraction
Subtracts two Fraction
s to get their difference. Does no simplification of the result.
subtract (Fraction 5 9) (Fraction 1 2) == Fraction 1 18
multiply : Fraction -> Fraction -> Fraction
Multiplies two Fraction
s to get their product. Does no simplification of the result.
multiply (Fraction 2 3) (Fraction 3 4) == Fraction 6 12
divide : Fraction -> Fraction -> Maybe Fraction
Divides two Fraction
s 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) == Nothing
compare : Fraction -> Fraction -> Basics.Order
Returns how fraction1
compares to fraction2
.
equal : Fraction -> Fraction -> Basics.Bool
Checks if fraction1
is equal to fraction2
exactlyEqual : Fraction -> Fraction -> Basics.Bool
Checks if fraction1
is exactly equal to fraction2
.
sort : List Fraction -> List Fraction
Sorts a List
of Fraction
s.
convertToSameDenominator : Fraction -> Fraction -> ( Fraction, Fraction )
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)
convertAllToSameDenominator : List Fraction -> List Fraction
Similar to convertToSameDenominator
, but is ideal for use cases involving 3+ Fraction
s.
toFloat : Fraction -> Basics.Float
Gets the floating point representation of the Fraction
.
Fraction.toFloat (Fraction 1 2) == 0.5
roundToNearestInt : Fraction -> Basics.Int
Rounds a Fraction
to the nearest integer.
toTuple : Fraction -> ( Basics.Int, Basics.Int )
Converts a Fraction
to a tuple pair.
toTuple (Fraction 5 8) == ( 5, 8 )