Natural number
Invariant: numbers <= 0xffffffffffffffff use the NS constructor
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05
Moduleghc-bignum-1.3Haskell2010
Natural number
Invariant: numbers <= 0xffffffffffffffff use the NS constructor
Check Natural invariants
Check Natural invariants
Zero Natural
One Natural
Test Zero Natural
Test One Natural
Indicate if the value is a power of two and which one
Create a Natural from a BigNat# (respect the invariants)
Convert a Natural into a BigNat#
Create a Natural from a Word#
Convert two Word# (most-significant first) into a Natural
Create a Natural from a Word
Convert the lower bits of a Natural into a Word#
Convert the lower bits of a Natural into a Word
Convert a Natural into a Word# clamping to (maxBound :: Word#).
Convert a Natural into a Word# clamping to (maxBound :: Word).
Create a Natural from a list of Word
Write a Natural in base-256 representation and return the number of bytes written.
The endianness is selected with the Bool# parameter: most significant
byte first (big-endian) if 1# or least significant byte first
(little-endian) if 0#.
Read a Natural in base-256 representation from a ByteArray#.
The size is given in bytes.
The endianness is selected with the Bool# parameter: most significant
byte first (big-endian) if 1# or least significant byte first
(little-endian) if 0#.
Null higher limbs are automatically trimed.
Encode (# Natural mantissa, Int# exponent #) into a Double#
Encode (# Natural mantissa, Int# exponent #) into a Float#
TODO: Not sure if it's worth to write Float optimized versions here
Addr#Write a Natural to addr in base-256 representation and return the
number of bytes written.
The endianness is selected with the Bool# parameter: write most significant
byte first (big-endian) if 1# or least significant byte first
(little-endian) if 0#.
Write a Natural to addr in base-256 representation and return the
number of bytes written.
The endianness is selected with the Bool# parameter: write most significant
byte first (big-endian) if 1# or least significant byte first
(little-endian) if 0#.
Read a Natural in base-256 representation from an Addr#.
The size is given in bytes.
The endianness is selected with the Bool# parameter: most significant
byte first (big-endian) if 1# or least significant byte first
(little-endian) if 0#.
Null higher limbs are automatically trimed.
Read a Natural in base-256 representation from an Addr#.
The size is given in bytes.
The endianness is selected with the Bool# parameter: most significant
byte first (big-endian) if 1# or least significant byte first
(little-endian) if 0#.
Null higher limbs are automatically trimed.
Equality test for Natural
Equality test for Natural
Inequality test for Natural
Inequality test for Natural
Greater or equal test for Natural
Greater or equal test for Natural
Lower or equal test for Natural
Lower or equal test for Natural
Greater test for Natural
Greater test for Natural
Lower test for Natural
Lower test for Natural
Compare two Natural
PopCount for Natural
PopCount for Natural
Right shift for Natural
Right shift for Natural
Left shift
Left shift
Add two naturals
Sub two naturals
Sub two naturals
Throw an Underflow exception if x < y
Sub two naturals
Unsafe: don't check that x >= y Undefined results if it happens
Multiplication
Square a Natural
Signum for Natural
Negate for Natural
Return division quotient and remainder
Division by zero is handled by BigNat
Return division quotient and remainder
Return division quotient
Return division remainder
Compute greatest common divisor.
Compute least common multiple.
Base 2 logarithm
Base 2 logarithm
Logarithm for an arbitrary base
Logarithm for an arbitrary base
Logarithm for an arbitrary base
Logarithm for an arbitrary base
"naturalPowMod b e m" computes base b raised to
exponent e modulo m.
Compute the number of digits of the Natural in the given base.
base must be > 1