Module Tolk.Bigint
Arbitrary-precision integers.
Integers are exact: no operation wraps or rounds unless it says so. An integer that fits an int is held as one, so arithmetic on such integers allocates nothing while its result fits an int too.
Each integer has one representation: polymorphic equality and Hashtbl.hash apply to integers, and an integer that fits an int hashes as that int. Polymorphic comparison does not order them: use compare.
type tThe type for integers.
exception OverflowRaised when a conversion's result does not fit its type.
Constants
Conversions
of_float x is x rounded toward zero.
Raises Overflow if x is infinite or NaN.
of_string s is the integer s writes: an optional - or +, an optional 0x, 0o or 0b for base 16, 8 or 2 (base 10 otherwise), then digits of the base, where underscores may follow the first digit.
Raises Invalid_argument if s is anything else, or if it writes more than 4300 decimal digits: reading them takes time quadratic in their number.
to_float n is the float nearest to n, ties to even: an infinity if n is past the greatest float.
to_string n is n in decimal, with a - if negative.
Raises Invalid_argument if n has more than 4300 digits: writing them takes time quadratic in their number.
pp_print formats integers as to_string does, and raises as it does.
Predicates and comparisons
Arithmetic
rem a b is a - b * div a b: it has a's sign and is smaller than b in magnitude.
Raises Division_by_zero if b is zero.
fdiv a b is a / b rounded toward negative infinity.
Raises Division_by_zero if b is zero.
cdiv a b is a / b rounded toward positive infinity.
Raises Division_by_zero if b is zero.
divisible a b is true iff b divides a: a = b * q for an integer q. Only zero is divisible by zero.
gcd a b is the greatest common divisor of a and b, never negative: gcd a zero is abs a.
Bits
Bitwise operations read integers in two's complement, extended to infinitely many bits: a negative integer has infinitely many ones on its left.
shift_left n k is n * 2{^k}.
Raises Invalid_argument if k is negative.
shift_right n k is n / 2{^k} rounded toward negative infinity.
Raises Invalid_argument if k is negative.
extract n off len is the non-negative integer of bits off to off + len - 1 of n.
Raises Invalid_argument if off is negative or len is not positive.
signed_extract n off len is extract n off len read as a len-bit two's complement integer, in [-2{^len-1};2{^len-1}-1].
Raises Invalid_argument if off is negative or len is not positive.
numbits n is the number of bits of abs n: 0 for zero, and otherwise the k for which 2{^k-1} <= abs n < 2{^k}.
trailing_zeros n is the greatest k for which 2{^k} divides n, or max_int if n is zero.
popcount n is the number of one bits of n.
Raises Overflow if n is negative: it has infinitely many.
Operators
For local opens: Bigint.(a * b + one).
( lsl ) is shift_left.