Module

Data.BigInt

This module defines a BigInt data type for arbitrary length integers.

#BaseDigits

type BaseDigits = { isNegative :: Boolean, value :: NonEmptyArray Int }

#fromString

fromString :: String -> Maybe BigInt

Parse a string into a BigInt, assuming a decimal representation. Returns Nothing if the parse fails.

Examples:

fromString "42"
fromString "857981209301293808359384092830482"
fromString "1e100"

#fromBase

fromBase :: Int -> String -> Maybe BigInt

Parse a string into a BigInt, assuming a representation in the given base. The letters "a-z" and "A-Z" will be interpreted as the digits 10 to 36. Returns Nothing if the parse fails.

fromBase 2 "100" == fromString "4"
fromBase 16 "ff" == fromString "255"

#fromInt

fromInt :: Int -> BigInt

Convert an integer to a BigInt.

#fromNumber

fromNumber :: Number -> Maybe BigInt

Convert a Number to a BigInt. The fractional part is truncated.

#toString

toString :: BigInt -> String

A decimal representation of the BigInt as a String.

#toNonEmptyString

toNonEmptyString :: BigInt -> NonEmptyString

A decimal representation of the BigInt as a NonEmptyString.

#toBase

toBase :: Int -> BigInt -> String

A base N representation of the BigInt as a String.

#toBase'

toBase' :: Int -> BigInt -> NonEmptyString

A base N representation of the BigInt as a NonEmptyString.

#digitsInBase

digitsInBase :: Int -> BigInt -> BaseDigits

A base N representation of the BigInt as an array of digits.

#abs

abs :: BigInt -> BigInt

The absolute value.

#even

even :: BigInt -> Boolean

Returns true if the number is even, false otherwise.

#odd

odd :: BigInt -> Boolean

Returns true if the number is odd, false otherwise.

#prime

prime :: BigInt -> Boolean

Returns true if the number is prime, false otherwise.

#pow

pow :: BigInt -> BigInt -> BigInt

Exponentiation for BigInt. If the exponent is less than 0, pow returns 0. Also, pow zero zero == one.

#not

not :: BigInt -> BigInt

Invert the bits.

#or

or :: BigInt -> BigInt -> BigInt

or the bits.

#xor

xor :: BigInt -> BigInt -> BigInt

Exlusive or the bits.

#and

and :: BigInt -> BigInt -> BigInt

and the bits.

#shl

shl :: BigInt -> Number -> BigInt

shift the bits left and zero fill.

#shr

shr :: BigInt -> Number -> BigInt

Shift the bits right and maintain pos/neg.

#quot

quot :: BigInt -> BigInt -> BigInt

Truncating integer division

#rem

rem :: BigInt -> BigInt -> BigInt

The remainder after truncating integer division

#toInt

#toNumber

toNumber :: BigInt -> Number

Converts a BigInt to a Number. Loses precision for numbers which are too large.

Modules