Module Big_int
Operations on arbitrary-precision integers.
Big integers (type big_int) are signed integers of arbitrary size.
val zero_big_int : big_intThe big integer
0.
val unit_big_int : big_intThe big integer
1.
Arithmetic operations
val sqrt_big_int : big_int -> big_intsqrt_big_int areturns the integer square root ofa, that is, the largest big integerrsuch thatr * r <= a. RaiseInvalid_argumentifais negative.
val quomod_big_int : big_int -> big_int -> big_int * big_intEuclidean division of two big integers. The first part of the result is the quotient, the second part is the remainder. Writing
(q,r) = quomod_big_int a b, we havea = q * b + rand0 <= r < |b|. RaiseDivision_by_zeroif the divisor is zero.
val div_big_int : big_int -> big_int -> big_intEuclidean quotient of two big integers. This is the first result
qofquomod_big_int(see above).
val mod_big_int : big_int -> big_int -> big_intEuclidean modulus of two big integers. This is the second result
rofquomod_big_int(see above).
val power_int_positive_int : int -> int -> big_intval power_big_int_positive_int : big_int -> int -> big_intval power_int_positive_big_int : int -> big_int -> big_intval power_big_int_positive_big_int : big_int -> big_int -> big_intExponentiation functions. Return the big integer representing the first argument
araised to the powerb(the second argument). Depending on the function,aandbcan be either small integers or big integers. RaiseInvalid_argumentifbis negative.
Comparisons and tests
val sign_big_int : big_int -> intReturn
0if the given big integer is zero,1if it is positive, and-1if it is negative.
val compare_big_int : big_int -> big_int -> intcompare_big_int a breturns0ifaandbare equal,1ifais greater thanb, and-1ifais smaller thanb.
val eq_big_int : big_int -> big_int -> boolval le_big_int : big_int -> big_int -> boolval ge_big_int : big_int -> big_int -> boolval lt_big_int : big_int -> big_int -> boolval gt_big_int : big_int -> big_int -> boolUsual boolean comparisons between two big integers.
val num_digits_big_int : big_int -> intReturn the number of machine words used to store the given big integer.
val num_bits_big_int : big_int -> intReturn the number of significant bits in the absolute value of the given big integer.
num_bits_big_int areturns 0 ifais 0; otherwise it returns a positive integernsuch that2^(n-1) <= |a| < 2^n.- since
- 4.03.0
Conversions to and from strings
val string_of_big_int : big_int -> stringReturn the string representation of the given big integer, in decimal (base 10).
val big_int_of_string : string -> big_intConvert a string to a big integer, in decimal. The string consists of an optional
-or+sign, followed by one or several decimal digits.
val big_int_of_string_opt : string -> big_int optionConvert a string to a big integer, in decimal. The string consists of an optional
-or+sign, followed by one or several decimal digits. Other the function returnsNone.- since
- 4.05
Conversions to and from other numerical types
val big_int_of_int : int -> big_intConvert a small integer to a big integer.
val is_int_big_int : big_int -> boolTest whether the given big integer is small enough to be representable as a small integer (type
int) without loss of precision. On a 32-bit platform,is_int_big_int areturnstrueif and only ifais between 230 and 230-1. On a 64-bit platform,is_int_big_int areturnstrueif and only ifais between -262 and 262-1.
val int_of_big_int : big_int -> intConvert a big integer to a small integer (type
int). RaisesFailure "int_of_big_int"if the big integer is not representable as a small integer.
val int_of_big_int_opt : big_int -> int optionConvert a big integer to a small integer (type
int). ReturnNoneif the big integer is not representable as a small integer.- since
- 4.05
val big_int_of_int32 : int32 -> big_intConvert a 32-bit integer to a big integer.
val big_int_of_nativeint : nativeint -> big_intConvert a native integer to a big integer.
val big_int_of_int64 : int64 -> big_intConvert a 64-bit integer to a big integer.
val int32_of_big_int : big_int -> int32Convert a big integer to a 32-bit integer. Raises
Failureif the big integer is outside the range [-231, 231-1].
val int32_of_big_int_opt : big_int -> int32 optionConvert a big integer to a 32-bit integer. Return
Noneif the big integer is outside the range [-231, 231-1].- since
- 4.05
val nativeint_of_big_int : big_int -> nativeintConvert a big integer to a native integer. Raises
Failureif the big integer is outside the range[Nativeint.min_int, Nativeint.max_int].
val nativeint_of_big_int_opt : big_int -> nativeint optionConvert a big integer to a native integer. Return
Noneif the big integer is outside the range[Nativeint.min_int, Nativeint.max_int];- since
- 4.05
val int64_of_big_int : big_int -> int64Convert a big integer to a 64-bit integer. Raises
Failureif the big integer is outside the range [-263, 263-1].
val int64_of_big_int_opt : big_int -> int64 optionConvert a big integer to a 64-bit integer. Return
Noneif the big integer is outside the range [-263, 263-1].- since
- 4.05
val float_of_big_int : big_int -> floatReturns a floating-point number approximating the given big integer.
Bit-oriented operations
val and_big_int : big_int -> big_int -> big_intBitwise logical 'and'. The arguments must be positive or zero.
val or_big_int : big_int -> big_int -> big_intBitwise logical 'or'. The arguments must be positive or zero.
val xor_big_int : big_int -> big_int -> big_intBitwise logical 'exclusive or'. The arguments must be positive or zero.
val shift_left_big_int : big_int -> int -> big_intshift_left_big_int b nreturnsbshifted left bynbits. Equivalent to multiplication by 2^n.
val shift_right_big_int : big_int -> int -> big_intshift_right_big_int b nreturnsbshifted right bynbits. Equivalent to division by 2^n with the result being rounded towards minus infinity.