nunntom/elm-bigrational/1.0.0

BigRational

Big rational numbers. Represent and perform arithmetic on non-integer values without floating point errors or native integer bounds.

Definition

The BigRational opaque type.

From

Make a rational from an integer.

Make a rational from a BigInt.

fromInts : Int -> 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.

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"

To

toBigInts : BigRational -> ( BigInt, BigInt )

Get the numerator and denominator of the rational as two BigInts

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 : 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 -> 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"

Arithmetic

Add two big rationals.

add (fromInts 1 2) (fromInts 1 4) == fromInts 3 4

Subtract one big rational from another.

sub (fromInt 10) (fromFloat 2.5) == fromInts 15 2

Multiply two big rationals.

mul (fromInts 5 2) (fromInts 3 2) == fromInts 15 4

Divide one big rational by another.

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 a rational. Flip the fraction upside down. 1/2 becomes 2/1.

BR.invert (BR.fromInts 1 2) == BR.fromInts 2 1

Negate a rational.

(fromInts 10 3 |> negate |> toString) == "-10/3"

Get the absolute value of a rational

abs (fromInts -10 3) == fromInts 10 3

Compare

Compare two rationals.

compare (fromInts 3 2) (fromInts 1 2) == GT

Is one rational lesser than another?

Is one rational greater than another?

Rounding

= ToEven
| ToOdd
| ToInfinity
| AwayFromZero

When rounding, if the number is exactly half-way between two whole numbers, which way to go?

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.