This library provides arbitrary precision Integer type and basic arithmetic operations on it.
Integer can be constructed from Int or String. Several convenience constructors for commonly used numbers are also provided.
Construct Integer from Int.
fromInt 83748314374831 -- Integer Positive [4374831,8374831] : Integer
fromInt 0 -- Zero : Integer
fromInt -93939193 -- Integer Negative [3939193,9] : Integer
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 StringConstruct Integer from String.
fromString "71289371892739812798734895701347589273948523984573" -- Just (Integer Positive [3984573,7394852,3475892,4895701,1279873,8927398,1289371,7]) : Maybe Integer
fromString "jiaosdjf" -- Nothing : Maybe IntegerAdd two Integers.
add ten million == Integer Positive [1000010] : IntegerSubtract two Integers.
sub million trillion == Integer Negative [9000000,99999] : IntegerMultiply two Integers.
mul (fromInt -873812381) (fromInt 78738732) == Integer Negative [5840892,287888,688] : IntegerDivide two Integers and get both quotient and remainder.
divmod (fromInt -873812381) (fromInt 78738732) == Just (Integer Negative [11],Integer Negative [7686329]) : Maybe ( Integer, Integer )
divmod zero (fromInt 871) == Just Zero : Maybe Integer
divmod (fromInt 871) zero == Nothing : Maybe IntegerDivide two Integers and get quotient.
div (fromInt -873812381) (fromInt 78738732) == Just (Integer Negative [11]) : Maybe Integer
div zero (fromInt 871) == Just Zero : Maybe Integer
div (fromInt 871) zero == Nothing : Maybe IntegerDivide two Integers and get remainder.
remainderBy (fromInt -873812381) (fromInt 78738732) == Just (Integer Negative [7686329]) : Maybe Integer
remainderBy zero (fromInt 871) == Just Zero : Maybe Integer
remainderBy (fromInt 871) zero == Nothing : Maybe IntegerRepresent Integer as String.
mul (fromInt 8172387) (fromInt -332)
|> toString == "-2713232484" : StringNegate the sign of Integer.
negate (fromInt -332) == Integer Positive [332] : Integer
negate (fromInt 8872) == Integer Negative [8872] : Integer
negate zero == Zero : IntegerGet absolute value of Integer.
negate (fromInt -332) == Integer Positive [332] : Integer
negate (fromInt 8872) == Integer Positive [8872] : Integer
negate zero == Zero : IntegerCompare two Integers.
compare one zero == GT
compare one one == EQ
compare (negate million) one == LTCount the number of decimal digits.
mul (fromInt 8172387) (fromInt -332) |> countDigits == 10 : Int
An
Integeris an arbitrary-precision number where its 'digits' of base 10 ^ 7 are represented as a linked list in little endian order. This base is chosen to simplify multiplication, toString, and debugging in Elm debugger.