Big rational numbers. Represent and perform arithmetic on non-integer values without floating point errors or native integer bounds.
The BigRational opaque type.
fromInt : Basics.Int -> BigRational
Make a rational from an integer.
fromBigInt : BigInt -> BigRational
Make a rational from a BigInt.
fromInts : Basics.Int -> Basics.Int -> BigRational
Make a rational as the ratio of two integers. fromInts 1 2
is a half (1/2).
fromBigInts : BigInt -> BigInt -> BigRational
Make a rational as the ratio of two BigInts. fromBigInts 3 4
is 4 quarters.
fromFloat : Basics.Float -> BigRational
Make a rational out of a float.
fromFloat 1.5 == fromInts 3 2
fromString : String -> Maybe BigRational
Try to make a rational from a string. The string can in the format
"1/3"
or "5 3/4"
or "-4 3/25"
with a single space between
the integer part (if present) and the fraction.
fromString "5 10/100" == Just (fromFloat 5.1)
fromString "12/5" == Just (fromFloat 2.4)
fromString "-1/3" == Just (fromInts -1 3)
fromFloatString : String -> Maybe BigRational
Try to make a rational from a string representation of a float/decimal number.
fromFloatString "1.5" == Just (fromInts 3 2)
fromFloatString "5e-2" == fromString "100/5"
fromFloatString "1.5E7" == fromString "15000000"
toBigInts : BigRational -> ( BigInt, BigInt )
Get the numerator and denominator of the rational as two BigInts
toMixedFraction : BigRational -> ( BigInt, BigRational )
Turn a rational into a mixed fraction. That is, a BigInt, and a BigRational representing a proper fraction.
toMixedFraction (fromInts 25 2) == ( BigInt.fromInt 12, fromInts 1 2 )
toMixedParts : BigRational -> ( BigInt, BigInt, BigInt )
Turn a rational into a mixed fraction and get the individual parts as BigInts. This may be useful for displaying the number in your own way.
toDecimalString : Basics.Int -> BigRational -> String
Turn a rational into a decimal string with the specified number of decimal places.
(fromInts 10 3 |> toDecimalString 5) == "3.33333"
(fromInts 20 3 |> toDecimalString 10) == "6.6666666667"
(fromInts 22 7 |> toDecimalString 30) == "3.142857142857142857142857142857"
toFloat : BigRational -> Basics.Float
Turn a rational into a float approximation.
(fromInts 10 3 |> toFloat) == 3.3333333333333335
(fromInts 1 2000000 |> toFloat) == 5.0e-7
toString : BigRational -> String
Turn a rational into a string like "1/2"
or "5 3/4"
or "-3 5/8"
.
toString (fromFloat 2.25) == "2 1/4"
add : BigRational -> BigRational -> BigRational
Add two big rationals.
add (fromInts 1 2) (fromInts 1 4) == fromInts 3 4
sub : BigRational -> BigRational -> BigRational
Subtract one big rational from another.
sub (fromInt 10) (fromFloat 2.5) == fromInts 15 2
mul : BigRational -> BigRational -> BigRational
Multiply two big rationals.
mul (fromInts 5 2) (fromInts 3 2) == fromInts 15 4
div : BigRational -> BigRational -> BigRational
Divide one big rational by another.
pow : Basics.Int -> BigRational -> BigRational
Raise a big rational to the power of an integer.
(fromInts 5 2 |> pow 2 |> toString) == "6 1/4"
(fromInts 5 2 |> pow -2 |> toFloat) == 0.16
invert : BigRational -> BigRational
Invert a rational. Flip the fraction upside down. 1/2 becomes 2/1.
BR.invert (BR.fromInts 1 2) == BR.fromInts 2 1
negate : BigRational -> BigRational
Negate a rational.
(fromInts 10 3 |> negate |> toString) == "-10/3"
abs : BigRational -> BigRational
Get the absolute value of a rational
abs (fromInts -10 3) == fromInts 10 3
compare : BigRational -> BigRational -> Basics.Order
Compare two rationals.
compare (fromInts 3 2) (fromInts 1 2) == GT
lt : BigRational -> BigRational -> Basics.Bool
Is one rational lesser than another?
gt : BigRational -> BigRational -> Basics.Bool
Is one rational greater than another?
When rounding, if the number is exactly half-way between two whole numbers, which way to go?
round : MidpointRounding -> BigRational -> BigInt
Round a rational number to the nearest whole number.
round ToEven (fromInts 10 3) == BigInt.fromInt 3
round ToEven (fromFloat 1.5) == BigInt.fromInt 2
round ToEven (fromFloat 2.5) == BigInt.fromInt 2
round ToOdd (fromFloat 2.5) == BigInt.fromInt 3
round ToInfinity (fromFloat 2.5) == BigInt.fromInt 3
round AwayFromZero (fromFloat -2.5) == BigInt.fromInt -3
round ToInfinity (fromFloat -2.5) == BigInt.fromInt -2
floor : BigRational -> BigInt
Floor means always rounding down, away from infinity.
ceiling : BigRational -> BigInt
The opposite of floor. Round up.
truncate : BigRational -> BigInt
Truncate a rational, rounding toward zero.