JoshuaHall/elm-fraction/2.0.2

NOTE: the latest version is 2.2.0

Fraction

This library provides a safe and simple API to deal with fractions.

Definition

The underlying opaque type of the Fraction module. Contains a numerator and a denominator.

Important Numbers

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.

Fraction Creation

create : Int -> Int -> Maybe Fraction

Attempts to create a Fraction.

create 1 2 == Just (Fraction 1 2)


create 1 0 == Nothing
createUnsafe : Int -> 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 : ( Int, Int ) -> Maybe Fraction

Attempts to take a pair of ints and make a Fraction.

Fraction Information

Gets the numerator.

getNumerator (Fraction 3 5) == 3

Gets the denominator.

getDenominator (Fraction 3 5) == 5

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 -> Bool

Checks if a Fraction is equal to zero.

isZero (Fraction 43 32) == False


isZero (Fraction 0 3) == True
isOne : Fraction -> Bool

Checks if a Fraction is equal to one.

isOne (Fraction 43 32) == False


isOne (Fraction 3 3) == True

Checks if a Fraction is equal to negative one.

isNegativeOne (Fraction 43 32) == False


isNegativeOne (Fraction -3 3) == True


isNegativeOne (Fraction 3 -3) == True

Manipulating Fractions

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

Puts a Fraction in the simplest possible terms.

simplify (Fraction 5 15) == Fraction 1 3


simplify (Fraction 3 4) == Fraction 3 4

Adds two Fractions to get their sum. Does no simplification of the result.

add (Fraction 2 3) (Fraction 1 2) == Fraction 7 6

Subtracts two Fractions to get their difference. Does no simplification of the result.

subtract (Fraction 5 9) (Fraction 1 2) == Fraction 1 18

Multiplies two Fractions to get their product. Does no simplification of the result.

multiply (Fraction 2 3) (Fraction 3 4) == Fraction 6 12

Divides 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) == Nothing

Comparing Fractions

compare : Fraction -> Fraction -> Order

Returns how fraction1 compares to fraction2.

equal : Fraction -> Fraction -> Bool

Checks if fraction1 is equal to fraction2

sort : List Fraction -> List Fraction

Sorts a List of Fractions.

Converting Fractions

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.

toFloat : Fraction -> Float

Gets the floating point representation of the Fraction.

Fraction.toFloat (Fraction 1 2) == 0.5

Rounds a Fraction to the nearest integer.

toTuple : Fraction -> ( Int, Int )

Converts a Fraction to a tuple pair.

toTuple (Fraction 5 8) == ( 5, 8 )