Go to the first, previous, next, last section, table of contents.


Integer Functions

This chapter describes the MP functions for performing integer arithmetic. These functions start with the prefix mpz_.

Arbitrary precision integers are stored in objects of type mpz_t.

Initialization and Assignment Functions

The functions for integer arithmetic assume that all integer objects are initialized. You do that by calling the function mpz_init.

Function: void mpz_init (mpz_t integer)
Initialize integer with limb space and set the initial numeric value to 0. Each variable should normally only be initialized once, or at least cleared out (using mpz_clear) between each initialization.

Here is an example of using mpz_init:

{
  mpz_t integ;
  mpz_init (integ);
  ...
  mpz_add (integ, ...);
  ...
  mpz_sub (integ, ...);

  /* Unless the program is about to exit, do ... */
  mpz_clear (integ);
}

As you can see, you can store new values any number of times, once an object is initialized.

Function: void mpz_clear (mpz_t integer)
Free the limb space occupied by integer. Make sure to call this function for all mpz_t variables when you are done with them.

Function: void * _mpz_realloc (mpz_t integer, mp_size_t new_alloc)
Change the limb space allocation to new_alloc limbs. This function is not normally called from user code, but it can be used to give memory back to the heap, or to increase the space of a variable to avoid repeated automatic re-allocation.

Function: void mpz_array_init (mpz_t integer_array[], size_t array_size, mp_size_t fixed_num_bits)
Allocate fixed limb space for all array_size integers in integer_array. The fixed allocation for each integer in the array is enough to store fixed_num_bits. If the fixed space will be insufficient for storing the result of a subsequent calculation, the result is unpredictable.

This function is useful for decreasing the working set for some algorithms that use large integer arrays.

There is no way to de-allocate the storage allocated by this function. Don't call mpz_clear!

Assignment Functions

These functions assign new values to already initialized integers (see section Initialization and Assignment Functions).

Function: void mpz_set (mpz_t rop, mpz_t op)
Function: void mpz_set_ui (mpz_t rop, unsigned long int op)
Function: void mpz_set_si (mpz_t rop, signed long int op)
Function: void mpz_set_d (mpz_t rop, double op)
Function: void mpz_set_q (mpz_t rop, mpq_t op)
Function: void mpz_set_f (mpz_t rop, mpf_t op)
Set the value of rop from op.

Function: int mpz_set_str (mpz_t rop, char *str, int base)
Set the value of rop from str, a '\0'-terminated C string in base base. White space is allowed in the string, and is simply ignored. The base may vary from 2 to 36. If base is 0, the actual base is determined from the leading characters: if the first two characters are `0x' or `0X', hexadecimal is assumed, otherwise if the first character is `0', octal is assumed, otherwise decimal is assumed.

This function returns 0 if the entire string up to the '\0' is a valid number in base base. Otherwise it returns -1.

Combined Initialization and Assignment Functions

For convenience, MP provides a parallel series of initialize-and-set functions which initialize the output and then store the value there. These functions' names have the form mpz_init_set...

Here is an example of using one:

{
  mpz_t pie;
  mpz_init_set_str (pie, "3141592653589793238462643383279502884", 10);
  ...
  mpz_sub (pie, ...);
  ...
  mpz_clear (pie);
}

Once the integer has been initialized by any of the mpz_init_set... functions, it can be used as the source or destination operand for the ordinary integer functions. Don't use an initialize-and-set function on a variable already initialized!

Function: void mpz_init_set (mpz_t rop, mpz_t op)
Function: void mpz_init_set_ui (mpz_t rop, unsigned long int op)
Function: void mpz_init_set_si (mpz_t rop, signed long int op)
Function: void mpz_init_set_d (mpz_t rop, double op)
Initialize rop with limb space and set the initial numeric value from op.

Function: int mpz_init_set_str (mpz_t rop, char *str, int base)
Initialize rop and set its value like mpz_set_str (see its documentation above for details).

If the string is a correct base base number, the function returns 0; if an error occurs it returns -1. rop is initialized even if an error occurs. (I.e., you have to call mpz_clear for it.)

Conversion Functions

This section describes functions for converting arbitrary precision integers to standard C types. Functions for converting to arbitrary precision integers are described in section Assignment Functions and section Input and Output Functions.

Function: unsigned long int mpz_get_ui (mpz_t op)
Return the least significant part from op. This function combined with
mpz_tdiv_q_2exp(..., op, CHAR_BIT*sizeof(unsigned long int)) can be used to extract the limbs of an integer.

Function: signed long int mpz_get_si (mpz_t op)
If op fits into a signed long int return the value of op. Otherwise return the least significant part of op, with the same sign as op.

If op is too large to fit in a signed long int, the returned result is probably not very useful.

Function: double mpz_get_d (mpz_t op)
Convert op to a double.

Function: char * mpz_get_str (char *str, int base, mpz_t op)
Convert op to a string of digits in base base. The base may vary from 2 to 36.

If str is NULL, space for the result string is allocated using the default allocation function, and a pointer to the string is returned.

If str is not NULL, it should point to a block of storage enough large for the result. To find out the right amount of space to provide for str, use mpz_sizeinbase (op, base) + 2. The two extra bytes are for a possible minus sign, and for the terminating null character.

Arithmetic Functions

Function: void mpz_add (mpz_t rop, mpz_t op1, mpz_t op2)
Function: void mpz_add_ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_sub (mpz_t rop, mpz_t op1, mpz_t op2)
Function: void mpz_sub_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 - op2.

Function: void mpz_mul (mpz_t rop, mpz_t op1, mpz_t op2)
Function: void mpz_mul_ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_mul_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_neg (mpz_t rop, mpz_t op)
Set rop to -op.

Function: void mpz_abs (mpz_t rop, mpz_t op)
Set rop to the absolute value of op.

Function: void mpz_fac_ui (mpz_t rop, unsigned long int op)
Set rop to op!, the factorial of op.

Division functions

Division is undefined if the divisor is zero, and passing a zero divisor to the divide or modulo functions, as well passing a zero mod argument to the mpz_powm and mpz_powm_ui functions, will make these functions intentionally divide by zero. This gives the user the possibility to handle arithmetic exceptions in these functions in the same manner as other arithmetic exceptions.

There are three main groups of division functions:

For each rounding mode, there are a couple of variants. Here `q' means that the quotient is computed, while `r' means that the remainder is computed. Functions that compute both the quotient and remainder have `qr' in the name.

Function: void mpz_tdiv_q (mpz_t rop, mpz_t op1, mpz_t op2)
Function: void mpz_tdiv_q_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to [op1/op2]. The quotient is truncated towards 0.

Function: void mpz_tdiv_r (mpz_t rop, mpz_t op1, mpz_t op2)
Function: void mpz_tdiv_r_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to (op1 - [op1/op2] * op2). Unless the remainder is zero, it has the same sign as the dividend.

Function: void mpz_tdiv_qr (mpz_t rop1, mpz_t rop2, mpz_t op1, mpz_t op2)
Function: void mpz_tdiv_qr_ui (mpz_t rop1, mpz_t rop2, mpz_t op1, unsigned long int op2)
Divide op1 by op2 and put the quotient in rop1 and the remainder in rop2. The quotient is rounded towards 0. Unless the remainder is zero, it has the same sign as the dividend.

If rop1 and rop2 are the same variable, the results are undefined.

Function: void mpz_fdiv_q (mpz_t rop1, mpz_t op1, mpz_t op2)
Function: void mpz_fdiv_q_ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_fdiv_r (mpz_t rop, mpz_t op1, mpz_t op2)
Function: unsigned long int mpz_fdiv_r_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Divide op1 by op2 and put the remainder in rop. Unless the remainder is zero, it has the same sign as the divisor.

For mpz_fdiv_r_ui the remainder is small enough to fit in an unsigned long int, and is therefore returned.

Function: void mpz_fdiv_qr (mpz_t rop1, mpz_t rop2, mpz_t op1, mpz_t op2)
Function: unsigned long int mpz_fdiv_qr_ui (mpz_t rop1, mpz_t rop2, mpz_t op1, unsigned long int op2)
Divide op1 by op2 and put the quotient in rop1 and the remainder in rop2. The quotient is rounded towards -infinity. Unless the remainder is zero, it has the same sign as the divisor.

For mpz_fdiv_qr_ui the remainder is small enough to fit in an unsigned long int, and is therefore returned.

If rop1 and rop2 are the same variable, the results are undefined.

Function: unsigned long int mpz_fdiv_ui (mpz_t op1, unsigned long int op2)
This function is similar to mpz_fdiv_r_ui, but the remainder is only returned; it is not stored anywhere.

Function: void mpz_cdiv_q (mpz_t rop1, mpz_t op1, mpz_t op2)
Function: void mpz_cdiv_q_ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_cdiv_r (mpz_t rop, mpz_t op1, mpz_t op2)
Function: unsigned long int mpz_cdiv_r_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Divide op1 by op2 and put the remainder in rop. Unless the remainder is zero, it has the opposite sign as the divisor.

For mpz_cdiv_r_ui the negated remainder is small enough to fit in an unsigned long int, and it is therefore returned.

Function: void mpz_cdiv_qr (mpz_t rop1, mpz_t rop2, mpz_t op1, mpz_t op2)
Function: unsigned long int mpz_cdiv_qr_ui (mpz_t rop1, mpz_t rop2, mpz_t op1, unsigned long int op2)
Divide op1 by op2 and put the quotient in rop1 and the remainder in rop2. The quotient is rounded towards +infinity. Unless the remainder is zero, it has the opposite sign as the divisor.

For mpz_cdiv_qr_ui the negated remainder is small enough to fit in an unsigned long int, and it is therefore returned.

If rop1 and rop2 are the same variable, the results are undefined.

Function: unsigned long int mpz_cdiv_ui (mpz_t op1, unsigned long int op2)
Return the negated remainder, similar to mpz_cdiv_r_ui. (The difference is that this function doesn't store the remainder anywhere.)

Function: void mpz_mod (mpz_t rop, mpz_t op1, mpz_t op2)
Function: unsigned long int mpz_mod_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 mod op2. The sign of the divisor is ignored, and the result is always non-negative.

For mpz_mod_ui the remainder is small enough to fit in an unsigned long int, and is therefore returned.

Function: void mpz_divexact (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to op1/op2. This function produces correct results only when it is known in advance that op2 divides op1.

Since mpz_divexact is much faster than any of the other routines that produce the quotient (see section References Jebelean), it is the best choice for instances in which exact division is known to occur, such as reducing a rational to lowest terms.

Function: void mpz_tdiv_q_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_tdiv_r_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_fdiv_q_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)

Function: void mpz_fdiv_r_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)

This operation can also be defined as masking of the op2 least significant bits.

Exponentialization Functions

Function: void mpz_powm (mpz_t rop, mpz_t base, mpz_t exp, mpz_t mod)
Function: void mpz_powm_ui (mpz_t rop, mpz_t base, unsigned long int exp, mpz_t mod)
Set rop to (base raised to exp) mod mod. If exp is negative, the result is undefined.

Function: void mpz_pow_ui (mpz_t rop, mpz_t base, unsigned long int exp)
Function: void mpz_ui_pow_ui (mpz_t rop, unsigned long int base, unsigned long int exp)
Set rop to base raised to exp.

Square Root Functions

Function: void mpz_sqrt (mpz_t rop, mpz_t op)

Function: void mpz_sqrtrem (mpz_t rop1, mpz_t rop2, mpz_t op)
(i.e., zero if op is a perfect square).

If rop1 and rop2 are the same variable, the results are undefined.

Function: int mpz_perfect_square_p (mpz_t op)
Return non-zero if op is a perfect square, i.e., if the square root of op is an integer. Return zero otherwise.

Number Theoretic Functions

Function: int mpz_probab_prime_p (mpz_t op, int reps)
A reasonable value of reps is 25.

An implementation of the probabilistic primality test found in Seminumerical Algorithms (see section References Knuth).

Function: void mpz_gcd (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to the greatest common divisor of op1 and op2.

Function: unsigned long int mpz_gcd_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Compute the greatest common divisor of op1 and op2. If rop is not NULL, store the result there.

If the result is small enough to fit in an unsigned long int, it is returned. If the result does not fit, 0 is returned, and the result is equal to the argument op1. Note that the result will always fit if op2 is non-zero.

Function: void mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, mpz_t a, mpz_t b)
Compute g, s, and t, such that as + bt = g = gcd (a, b). If t is NULL, that argument is not computed.

Function: int mpz_invert (mpz_t rop, mpz_t op1, mpz_t op2)
Compute the inverse of op1 modulo op2 and put the result in rop. Return non-zero if an inverse exist, zero otherwise. When the function returns zero, do not assume anything about the value in rop.

Function: int mpz_jacobi (mpz_t op1, mpz_t op2)
Function: int mpz_legendre (mpz_t op1, mpz_t op2)
Compute the Jacobi and Legendre symbols, respectively.

Comparison Functions

Function: int mpz_cmp (mpz_t op1, mpz_t op2)

Macro: int mpz_cmp_ui (mpz_t op1, unsigned long int op2)
Macro: int mpz_cmp_si (mpz_t op1, signed long int op2)

These functions are actually implemented as macros. They evaluate their arguments multiple times.

Macro: int mpz_sgn (mpz_t op)

This function is actually implemented as a macro. It evaluates its arguments multiple times.

Logical and Bit Manipulation Functions

These functions behave as if two's complement arithmetic were used (although sign-magnitude is used by the actual implementation).

Function: void mpz_and (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to op1 logical-and op2.

Function: void mpz_ior (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to op1 inclusive-or op2.

Function: void mpz_com (mpz_t rop, mpz_t op)
Set rop to the one's complement of op.

Function: unsigned long int mpz_popcount (mpz_t op)
For non-negative numbers, return the population count of op. For negative numbers, return the largest possible value (MAX_ULONG).

Function: unsigned long int mpz_hamdist (mpz_t op1, mpz_t op2)
If op1 and op2 are both non-negative, return the hamming distance between the two operands. Otherwise, return the largest possible value (MAX_ULONG).

It is possible to extend this function to return a useful value when the operands are both negative, but the current implementation returns MAX_ULONG in this case. Do not depend on this behavior, since it will change in future versions of the library.

Function: unsigned long int mpz_scan0 (mpz_t op, unsigned long int starting_bit)
Scan op, starting with bit starting_bit, towards more significant bits, until the first clear bit is found. Return the index of the found bit.

Function: unsigned long int mpz_scan1 (mpz_t op, unsigned long int starting_bit)
Scan op, starting with bit starting_bit, towards more significant bits, until the first set bit is found. Return the index of the found bit.

Function: void mpz_setbit (mpz_t rop, unsigned long int bit_index)
Set bit bit_index in op1.

Function: void mpz_clrbit (mpz_t rop, unsigned long int bit_index)
Clear bit bit_index in op1.

Input and Output Functions

Functions that perform input from a stdio stream, and functions that output to a stdio stream. Passing a NULL pointer for a stream argument to any of these functions will make them read from stdin and write to stdout, respectively.

When using any of these functions, it is a good idea to include `stdio.h' before `gmp.h', since that will allow `gmp.h' to define prototypes for these functions.

Function: size_t mpz_out_str (FILE *stream, int base, mpz_t op)
Output op on stdio stream stream, as a string of digits in base base. The base may vary from 2 to 36.

Return the number of bytes written, or if an error occurred, return 0.

Function: size_t mpz_inp_str (mpz_t rop, FILE *stream, int base)
Input a possibly white-space preceded string in base base from stdio stream stream, and put the read integer in rop. The base may vary from 2 to 36. If base is 0, the actual base is determined from the leading characters: if the first two characters are `0x' or `0X', hexadecimal is assumed, otherwise if the first character is `0', octal is assumed, otherwise decimal is assumed.

Return the number of bytes read, or if an error occurred, return 0.

Function: size_t mpz_out_raw (FILE *stream, mpz_t op)
Output op on stdio stream stream, in raw binary format. The integer is written in a portable format, with 4 bytes of size information, and that many bytes of limbs. Both the size and the limbs are written in decreasing significance order (i.e., in big-endian).

The output can be read with mpz_inp_raw.

Return the number of bytes written, or if an error occurred, return 0.

The output of this can not be read by mpz_inp_raw from GMP 1, because of changes necessary for compatibility between 32-bit and 64-bit machines.

Function: size_t mpz_inp_raw (mpz_t rop, FILE *stream)
Input from stdio stream stream in the format written by mpz_out_raw, and put the result in rop. Return the number of bytes read, or if an error occurred, return 0.

This routine can read the output from mpz_out_raw also from GMP 1, in spite of changes necessary for compatibility between 32-bit and 64-bit machines.

Miscellaneous Functions

Function: void mpz_random (mpz_t rop, mp_size_t max_size)
Generate a random integer of at most max_size limbs. The generated random number doesn't satisfy any particular requirements of randomness. Negative random numbers are generated when max_size is negative.

Function: void mpz_random2 (mpz_t rop, mp_size_t max_size)
Generate a random integer of at most max_size limbs, with long strings of zeros and ones in the binary representation. Useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger corner-case bugs. Negative random numbers are generated when max_size is negative.

Function: size_t mpz_size (mpz_t op)
Return the size of op measured in number of limbs. If op is zero, the returned value will be zero.

This function is obsolete. It will disappear from future MP releases.

Function: size_t mpz_sizeinbase (mpz_t op, int base)
Return the size of op measured in number of digits in base base. The base may vary from 2 to 36. The returned value will be exact or 1 too big. If base is a power of 2, the returned value will always be exact.

This function is useful in order to allocate the right amount of space before converting op to a string. The right amount of allocation is normally two more than the value returned by mpz_sizeinbase (one extra for a minus sign and one for the terminating '\0').


Go to the first, previous, next, last section, table of contents.