Module

JS.BigInt

#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.

#fromString

fromString :: String -> Maybe BigInt

Parse a string into a BigInt. Returns Nothing if the parse fails. Supports decimal, binary, octal, hexadecimal and exponentiation notations. See MDN for more examples.

Examples:

fromString "857981209301293808359384092830482"  -- 857981209301293808359384092830482
fromString "0b10000000000000000000000000000000" -- 2147483648
fromString "0B00000000011111111111111111111111" -- 8388607
fromString "0O755"                              -- 493
fromString "0o644"                              -- 420
fromString "0xFFFFFFFFFFFFFFFFF"                -- 295147905179352830000
fromString "0XA"                                -- 10
fromString "0e-5"                               -- 0
fromString "5e1"                                -- 50
fromString "175e-2"                             -- 1.75
fromString "1E3"                                -- 1000

#fromStringAs

fromStringAs :: Radix -> String -> Maybe BigInt

Like fromString, but the integer can be specified in a different base.

Example:

fromStringAs binary      "100" == Just 4
fromStringAs hexadecimal "ff"  == Just 255

#fromTLInt

fromTLInt :: forall i sym. ToString i sym => Reflectable sym String => Proxy i -> BigInt

Converts a type-level integer into a BigInt:

import Type.Proxy (Proxy(..))
foo = fromTLInt (Proxy :: Proxy 857981209301293808359384092830482)

#toInt

toInt :: BigInt -> Maybe Int

Convert a BigInt to an Int. If the BigInt is larger than 2_147_483_647 or smaller than -2_147_483_648 then Nothing is returned.

#toNumber

toNumber :: BigInt -> Number

Convert a BigInt to a Number. There may be a loss of precision. If the BigInt is larger than 9_007_199_254_740_991 then the result will be +Infinity. If the BigInt is smaller than -9_007_199_254_740_991 then the result will be -Infinity.

#asIntN

asIntN :: Int -> BigInt -> BigInt

Clamps a BigInt value to the given number of bits, and returns that value as a signed integer.

#asUintN

asUintN :: Int -> BigInt -> BigInt

Clamps a BigInt value to the given number of bits, and returns that value as an unsigned integer.

#toString

toString :: BigInt -> String

A decimal representation of the BigInt as a String.

#toStringAs

toStringAs :: Radix -> BigInt -> String

Like toString, but the integer can be specified in a different base.

#pow

pow :: BigInt -> BigInt -> BigInt

Raise a BigInt to the power of another BigInt.

pow (fromInt 2) (fromInt 3) -- 2^3

#not

not :: BigInt -> BigInt

Invert the bits.

#and

and :: BigInt -> BigInt -> BigInt

And the bits.

#or

or :: BigInt -> BigInt -> BigInt

Or the bits.

#shl

shl :: BigInt -> BigInt -> BigInt

The BigInt in the first argument shifted to the left by the number of bits specified in the second argument. Excess bits shifted off to the left are discarded, and zero bits are shifted in from the right.

#shr

shr :: BigInt -> BigInt -> BigInt

The BigInt in the first argument shifted to the right by the number of bits specified in the second argument. Excess bits shifted off to the right are discarded, and copies of the leftmost bit are shifted in from the left. This operation is also called "sign-propagating right shift" or "arithmetic right shift", because the sign of the resulting number is the same as the sign of the first operand.

#xor

xor :: BigInt -> BigInt -> BigInt

Exlusive or the bits.

#even

even :: BigInt -> Boolean

Returns whether an BigInt is an even number.

even (fromInt 0) == true
even (fromInt 1) == false

#odd

odd :: BigInt -> Boolean

The negation of even.

odd (fromInt 0) == false
odd (fromInt 1) == true

#parity

parity :: BigInt -> Parity

Returns whether an BigInt is Even or Odd.

parity (fromInt 0) == Even
parity (fromInt 1) == Odd

Re-exports from Data.Int

#Radix

newtype Radix

The number of unique digits (including zero) used to represent integers in a specific base.

#Parity

data Parity

A type for describing whether an integer is even or odd.

The Ord instance considers Even to be less than Odd.

The Semiring instance allows you to ask about the parity of the results of arithmetical operations, given only the parities of the inputs. For example, the sum of an odd number and an even number is odd, so Odd + Even == Odd. This also works for multiplication, eg. the product of two odd numbers is odd, and therefore Odd * Odd == Odd.

More generally, we have that

parity x + parity y == parity (x + y)
parity x * parity y == parity (x * y)

for any integers x, y. (A mathematician would say that parity is a ring homomorphism.)

After defining addition and multiplication on Parity in this way, the Semiring laws now force us to choose zero = Even and one = Odd. This Semiring instance actually turns out to be a Field.

Constructors

Instances

#octal

octal :: Radix

The base-8 system.

#hexadecimal

hexadecimal :: Radix

The base-16 system.

#decimal

decimal :: Radix

The base-10 system.

#binary

binary :: Radix

The base-2 system.

Modules