This library provides arbitrary precision 'Decimal' type and basic arithmetic operations on it.
A Decimal
is an arbitrary precision number with significand and exponent.
Integer
Significand
is an alias for an Integer
.
Basics.Int
Exponent
is an alias for Int
.
minExponent : Basics.Int
minExponent
specifies the default minimum exponent. It's currently -32, chosen arbitrarily.
Decimal
can be constructed from Int
, Float
, String
, or Integer
.
fromInt : Basics.Int -> Decimal
Construct Decimal
from Int
.
fromInt 89928271371 -- Decimal (Integer Positive [8271371,8992]) 0 : Decimal
fromInt 0 -- Decimal Integer.Zero 0 : Decimal
fromInt -899210300 -- Decimal (Integer Negative [8992103]) 2 : Decimal
Entering numbers that cannot be represented by Javascript number type will result in incorrect representation. Use fromString in that case.
fromInt 81927398127398127389172893712893712893798123 |> toString -- "-10497034289617408" : String
fromString "81927398127398127389172893712893712893798123" |> Maybe.map toString -- Just "81927398127398127389172893712893712893798123" : Maybe String
fromFloat : Basics.Float -> Decimal
Construct Decimal
from Float
.
fromFloat -8213.211 -- Decimal (Integer Negative [8213211]) -3 : Decimal
fromFloat 0.0000000000000000000000000000000003323232238 -- Decimal (Integer Positive [3]) -34 : Decimal
Entering numbers that cannot be represented by Javascript number type will result in incorrect representation. Use fromString in that case.
fromFloat -1727816283761287361287631287.123123123123 |> toString -- "-1727816283761287400000000000" : String
fromString "-1727816283761287361287631287.123123123123" |> Maybe.map toString -- Just "-1727816283761287361287631287.123123123123" : Maybe String
fromString : String -> Maybe Decimal
Construct Decimal
from String
.
fromString "0.0000000076128736" -- Just (Decimal (Integer Positive [6128736,7]) -16) : Maybe Decimal
fromString "-7637370.761287360000" -- Just (Decimal (Integer Negative [6128736,6373707,7]) -8) : Maybe Decimal
fromString "ajsdlkfj" -- Nothing : Maybe Decimal
fromInteger : Integer -> Decimal
Construct Decimal
from Integer
.
fromInteger (Integer.fromString "812371263726178361298371987329810000" |> Maybe.withDefault Integer.zero) -- Decimal (Integer Positive [8732981,2983719,6178361,7126372,8123]) 4 : Decimal
fromInteger Integer.zero -- Decimal Integer.Zero 0 : Decimal
fromInteger (Integer.negate Integer.zillion) -- Decimal (Integer Negative [1]) 12 : Decimal
significand : Decimal -> Integer
Get significand portion of Decimal
.
exponent : Decimal -> Exponent
Get exponent portion of Decimal
.
add : Decimal -> Decimal -> Decimal
Add two Decimal
s.
add (fromFloat 8172398.121) (fromFloat 0.0000001) |> toString -- "8172398.1210001" : String
add (fromFloat 0.000032) (fromFloat -0.000032) |> toString -- "0" : String
add (fromFloat -0.002) (fromFloat 0) |> toString -- "-0.002" : String
sub : Decimal -> Decimal -> Decimal
Subtract two Decimal
s.
sub (fromFloat 8172398.121) (fromFloat 0.0000001) |> toString -- "8172398.1209999" : String
sub (fromFloat 0.000032) (fromFloat -0.000032) |> toString -- "0.000064" : String
sub (fromFloat -0.002) (fromFloat 0) |> toString -- "-0.002" : String
mul : Decimal -> Decimal -> Decimal
Multiply two Decimal
s.
mul (fromFloat 8172398.121) (fromFloat 0.0000001) |> toString -- "0.8172398121" : String
mul (fromFloat 0.000032) (fromFloat -0.000032) |> toString -- "-0.000000001024" : String
mul (fromFloat -0.002) (fromFloat 0) |> toString -- "0" : String
mulToMinE : Basics.Int -> Decimal -> Decimal -> Decimal
Use this function when you want a multiplication with more fraction digits than what can be expressed with minExponent
.
div : Decimal -> Decimal -> Maybe Decimal
Divide two Decimal
s.
div (fromFloat 8172398.121) (fromFloat 0.0000001) |> Maybe.map toString -- Just "81723981210000" : Maybe String
div (fromFloat 0.000032) (fromFloat -0.000032) |> Maybe.map toString -- Just "-1" : Maybe String
div (fromFloat 0) (fromFloat -0.002) |> Maybe.map toString -- Just "0" : Maybe String
div (fromFloat -0.002) (fromFloat 0) |> Maybe.map toString -- Nothing : Maybe String
Maybe.andThen (div (fromInt 1)) (fromString "100000000000000000000000000000000000000") |> Maybe.map toString -- Nothing : Maybe String
divToMinE : Basics.Int -> Decimal -> Decimal -> Maybe Decimal
Use this function when you want a division with more fraction digits than what can be expressed with minExponent
.
Notation
type determines how to represent Decimal
as String
. Dec
represents decimal notation such as 0.003
, 1231.6161
, 335
, or 33500
. Sci
represents scientific notation such as 3e-3
, 1.2316161e3
, or 3.35e4
.
toString : Decimal -> String
Represent Decimal
as String
. Default notation is Dec
.
mul (fromFloat 8172387.1123) (fromFloat -3.3532) |> toString == "-27403648.46496436" : String
toStringIn : Notation -> Decimal -> String
Represent Decimal
as String
in desired notation.
negate : Decimal -> Decimal
Negate the sign of Decimal
.
negate (fromInt 1232139812) |> toString == "-1232139812" : String
negate (fromFloat -127.1232139812) |> toString == "127.1232139812" : String
negate (fromInt 0) |> toString == "0" : String
abs : Decimal -> Decimal
Get absolute value of Decimal
.
abs (fromInt 1232139812) |> toString == "1232139812" : String
abs (fromInt 0) |> toString == "0" : String
abs (fromFloat -127.1232139812) |> toString == "127.1232139812" : String
compare : Decimal -> Decimal -> Basics.Order
Compare two Decimal
s.
compare (fromFloat 23.223) (fromFloat 23.224) == LT : Order
compare (fromFloat 23.223) (fromFloat 23.22) == GT : Order
compare (fromFloat 23.223) (fromFloat -23.22) == GT : Order
compare (fromFloat 23.223) (fromFloat 23.223) == EQ : Order
lt : Decimal -> Decimal -> Basics.Bool
gt : Decimal -> Decimal -> Basics.Bool
lte : Decimal -> Decimal -> Basics.Bool
gte : Decimal -> Decimal -> Basics.Bool
eq : Decimal -> Decimal -> Basics.Bool
round : Decimal -> Decimal
round
to the nearest integer with HalfToEven
mode.
roundTo : Exponent -> Decimal -> Decimal
roundTo
given exponent with HalfToEven
mode.
floor : Decimal -> Decimal
floor
to the nearest integer with Down
mode.
ceiling : Decimal -> Decimal
ceiling
to the nearest integer with Up
mode.
truncate : Decimal -> Decimal
truncate
to the nearest integer with TowardsZero
mode.
roundWithContext : RoundingContext -> Decimal -> Decimal
Specify a RoundingContext
to customize the rounding strategy.
{ e : Exponent
, mode : RoundingMode
}
RoundingContext
type can be used to specify rounding strategy. Specify e
to designate the digit you would like to round to, and choose a mode
for rounding. e
of 0
will round to an integer; e
of -1
will round to the nearest tenths, e
of -2
to the nearest hundredths; e
of 1
will round to tens, e
of 2
to hundreds.
RoundingMode
type determines rounding strategy. Default strategy is HalfToEven
.
Value | Down | Up | TowardsZero | AwayFromZero | HalfToEven +1.8 | +1 | +2 | +1 | +2 | +2 +1.5 | +1 | +2 | +1 | +2 | +2 +1.2 | +1 | +2 | +1 | +2 | +1 +0.8 || 0 | +1 || 0 | +1 | +1 +0.5 || 0 | +1 || 0 | +1 || 0 +0.2 || 0 | +1 || 0 | +1 || 0 −0.2 | −1 || 0 || 0 | −1 || 0 −0.5 | −1 || 0 || 0 | −1 || 0 −0.8 | −1 || 0 || 0 | -1 | -1 −1.2 | −2 | −1 | −1 | −2 | −1 −1.5 | −2 | −1 | −1 | −2 | −2 −1.8 | −2 | −1 | −1 | −2 | −2
sqrt : Decimal -> Maybe Decimal
Calculate Square root using Babylonian method.
sqrtToMinE : Exponent -> Decimal -> Maybe Decimal
Use this function when you want a square root with more fraction digits than what can be expressed with minExponent
.