module Z:sig..end
This modules provides arbitrary-precision integers.
Small integers internally use a regular OCaml int.
When numbers grow too large, we switch transparently to GMP numbers
(mpn numbers fully allocated on the OCaml heap).
This interface is rather similar to that of Int32 and Int64,
with some additional functions provided natively by GMP
(GCD, square root, pop-count, etc.).
This file is part of the Zarith library http://forge.ocamlcore.org/projects/zarith . It is distributed under LGPL 2 licensing, with static linking exception. See the LICENSE file included in the distribution.
Copyright (c) 2010-2011 Antoine Miné, Abstraction project.
Abstraction is part of the LIENS (Laboratoire d'Informatique de l'ENS),
a joint laboratory by:
CNRS (Centre national de la recherche scientifique, France),
ENS (École normale supérieure, Paris, France),
INRIA Rocquencourt (Institut national de recherche en informatique, France).
ocaml interactive toplevel,
the magic commands are:
#load "zarith.cma";;
#install_printer Z.pp_print;;
Alternatively, using the new Zarith_top toplevel module, simply:
#require "zarith.top";;
type t
exception Overflow
val zero : tval one : tval minus_one : tval of_int : int -> tval of_int32 : int32 -> tval of_int64 : int64 -> tval of_nativeint : nativeint -> tval of_float : float -> tOverflow on infinity and NaN arguments.val of_string : string -> t- prefix indicates a negative number, while a +
prefix is ignored.
An optional prefix 0x, 0o, or 0b (following the optional -
or + prefix) indicates that the number is,
represented, in hexadecimal, octal, or binary, respectively.
Otherwise, base 10 is assumed.
(Unlike C, a lone 0 prefix does not denote octal.)
Raises an Invalid_argument exception if the string is not a
syntactically correct representation of an integer.val of_substring : string -> pos:int -> len:int -> tof_substring s ~pos ~len is the same as of_string (String.sub s
pos len)val of_string_base : int -> string -> t- or + prefix.
The base must be between 2 and 16.val of_substring_base : int -> string -> pos:int -> len:int -> tof_substring_base base s ~pos ~len is the same as of_string_base
base (String.sub s pos len)val succ : t -> tval pred : t -> tval abs : t -> tval neg : t -> tval add : t -> t -> tval sub : t -> t -> tval mul : t -> t -> tval div : t -> t -> tDivision_by_zero if the divisor (second argument) is 0.val rem : t -> t -> tDivision_by_zero.
The result of rem a b has the sign of a, and its absolute value is
strictly smaller than the absolute value of b.
The result satisfies the equality a = b * div a b + rem a b.val div_rem : t -> t -> t * tdiv_rem a b is equal to (div a b, rem a b).
Raises Division_by_zero if b = 0.val cdiv : t -> t -> tDivision_by_zero.val fdiv : t -> t -> tDivision_by_zero.val ediv_rem : t -> t -> t * tediv_rem a b returns a pair (q, r)
such that a = b * q + r and 0 <= r < |b|.
Raises Division_by_zero if b = 0.val ediv : t -> t -> tediv a b is equal to fst (ediv_rem a b).
The result satisfies 0 <= a - b * ediv a b < |b|.
Raises Division_by_zero if b = 0.val erem : t -> t -> terem a b is equal to snd (ediv_rem a b).
The result satisfies 0 <= erem a b < |b| and
a = b * ediv a b + erem a b. Raises Division_by_zero if b = 0.val divexact : t -> t -> tdivexact a b divides a by b, only producing correct result when the
division is exact, i.e., when b evenly divides a.
It should be faster than general division.
Can raise a Division_by_zero.val logand : t -> t -> tval logor : t -> t -> tval logxor : t -> t -> tval lognot : t -> tlognot a=-a-1 always hold.val shift_left : t -> int -> tval shift_right : t -> int -> tval shift_right_trunc : t -> int -> tval numbits : t -> intx is zero, numbits x returns 0. Otherwise,
numbits x returns a positive integer n such that
2^{n-1} <= |x| < 2^n. Note that numbits is defined
for negative arguments, and that numbits (-x) = numbits x.val trailing_zeros : t -> intx is zero, trailing_zeros x returns max_int.
Otherwise, trailing_zeros x returns a nonnegative integer n
which is the largest n such that 2^n divides x evenly.
Note that trailing_zeros is defined for negative arguments,
and that trailing_zeros (-x) = trailing_zeros x.val testbit : t -> int -> booltestbit x n return the value of bit number n in x:
true if the bit is 1, false if the bit is 0.
Bits are numbered from 0. Raise Invalid_argument if n
is negative.val popcount : t -> intOverflow for negative arguments, as those have an infinite
number of bits set.val hamdist : t -> t -> intOverflow if the arguments have different signs
(in which case the distance is infinite).Overflow exception is raised.val to_int : t -> intOverflow.val to_int32 : t -> int32Overflow.val to_int64 : t -> int64Overflow.val to_nativeint : t -> nativeintOverflow.val to_float : t -> floatval to_string : t -> stringval format : string -> t -> string
% [flags] [width] type
Where the type actually indicates the base:
i, d, u: decimalb: binaryo: octalx: lowercase hexadecimalX: uppercase hexadecimal
+: prefix positive numbers with a + sign-: left-justify (default is right justification)0: pad with zeroes (instead of spaces)#: alternate formatting (actually, simply output a literal-like prefix: 0x, 0b, 0o)printf, all numbers are signed (even hexadecimal ones),
there is no precision field, and characters that are not part of the format
are simply ignored (and not copied in the output).val fits_int : t -> boolint.val fits_int32 : t -> boolint32.val fits_int64 : t -> boolint64.val fits_nativeint : t -> boolnativeint.val print : t -> unitval output : Pervasives.out_channel -> t -> unit%a format printer in Printf.printf.val sprint : unit -> t -> string%a format printer in Printf.sprintf.val bprint : Buffer.t -> t -> unit%a format printer in Printf.bprintf.val pp_print : Format.formatter -> t -> unit%a format printer in Format.printf and as
argument to #install_printer in the top-level.val compare : t -> t -> intcompare x y returns 0 if x equals y,
-1 if x is smaller than y, and 1 if x is greater than y.
Note that Pervasive.compare can be used to compare reliably two integers
only on OCaml 3.12.1 and later versions.
val equal : t -> t -> boolval leq : t -> t -> boolval geq : t -> t -> boolval lt : t -> t -> boolval gt : t -> t -> boolval sign : t -> intval min : t -> t -> tval max : t -> t -> tval is_even : t -> boolval is_odd : t -> boolval hash : t -> inta = b, then hash a =
hash b.val gcd : t -> t -> tDivision_by_zero is either argument is null.val gcdext : t -> t -> t * t * tgcdext u v returns (g,s,t) where g is the greatest common divisor
and g=us+vt.
g is always positive.
Raises a Division_by_zero is either argument is null.
Note: the function is based on the GMP mpn_gcdext function. The exact choice of s and t such that g=us+vt is not specified, as it may vary from a version of GMP to another (it has changed notably in GMP 4.3.0 and 4.3.1).
val lcm : t -> t -> tDivision_by_zero is either argument is null.val powm : t -> t -> t -> tpowm base exp mod computes base^exp modulo mod.
Negative exp are supported, in which case (base^-1)^(-exp) modulo
mod is computed.
However, if exp is negative but base has no inverse modulo mod, then
a Division_by_zero is raised.val powm_sec : t -> t -> t -> tpowm_sec base exp mod computes base^exp modulo mod.
Unlike Z.powm, this function is designed to take the same time
and have the same cache access patterns for any two same-size
arguments. Used in cryptographic applications, it provides better
resistance to side-channel attacks than Z.powm.
The exponent exp must be positive, and the modulus mod
must be odd. Otherwise, Invalid_arg is raised.val invert : t -> t -> tinvert base mod returns the inverse of base modulo mod.
Raises a Division_by_zero if base is not invertible modulo mod.val probab_prime : t -> int -> intprobab_prime x r returns 0 if x is definitely composite,
1 if x is probably prime, and 2 if x is definitely prime.
The r argument controls how many Miller-Rabin probabilistic
primality tests are performed (5 to 10 is a reasonable value).val nextprime : t -> tval pow : t -> int -> tpow base exp raises base to the exp power.
exp must be non-negative.
Note that only exponents fitting in a machine integer are supported, as
larger exponents would surely make the result's size overflow the
address space.val sqrt : t -> tInvalid_argument on negative arguments.val sqrt_rem : t -> t * tInvalid_argument on negative arguments.val root : t -> int -> troot base n computes the n-th root of exp.
n must be non-negative.val perfect_power : t -> boola^b, with b>1val perfect_square : t -> boola^2.val log2 : t -> intx is positive, log2 x returns the largest n
such that 2^n <= x. If x is negative or zero, log2 x raise
the Invalid_argument exception.val log2up : t -> intx is positive, log2up x returns the smallest n
such that x <= 2^n. If x is negative or zero, log2up x raise
the Invalid_argument exception.val size : t -> intval extract : t -> int -> int -> textract a off len returns a non-negative number corresponding to bits
off to off+len-1 of b.
Negative a are considered in infinite-length 2's complement
representation.val signed_extract : t -> int -> int -> tsigned_extract a off len extracts bits off to off+len-1 of b,
as extract does, then sign-extends bit len-1 of the result
(that is, bit off + len - 1 of a). The result is between
- 2{^[len]-1} (included) and 2{^[len]-1} (excluded),
and equal to extract a off len modulo 2{^len}.val to_bits : t -> stringval of_bits : string -> tof_bits (to_bits x) = abs x.
However, we can have to_bits (of_bits s) <> s due to the presence of
trailing zeros in s.int operators are
redefined on t.
This makes it easy to typeset expressions.
Using OCaml 3.12's local open, you can simply write
Z.(~$2 + ~$5 * ~$10).
val (~-) : t -> tneg.val (~+) : t -> tval (+) : t -> t -> tadd.val (-) : t -> t -> tsub.val ( * ) : t -> t -> tmul.val (/) : t -> t -> tdiv.val (/>) : t -> t -> tcdiv.val (/<) : t -> t -> tfdiv.val (/|) : t -> t -> tdiv_exact.val (mod) : t -> t -> trem.val (land) : t -> t -> tlogand.val (lor) : t -> t -> tlogor.val (lxor) : t -> t -> tlogxor.val (~!) : t -> tlognot.val (lsl) : t -> int -> tshift_left.val (asr) : t -> int -> tshift_right.val (~$) : int -> tint of_int.val ( ** ) : t -> int -> tpow.val (=) : t -> t -> boolequal.val (<) : t -> t -> boollt.val (>) : t -> t -> boolgt.val (<=) : t -> t -> boolleq.val (>=) : t -> t -> boolgeq.val (<>) : t -> t -> boola <> b is equivalent to not (equal a b).val version : string"1.5").