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


Integer Functions

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

GMP integers are stored in objects of type mpz_t.

Initialization 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 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.

[It turns out that it is not entirely true that this function ignores white-space. It does ignore it between digits, but not after a minus sign or within or after "0x". We are considering changing the definition of this function, making it fail when there is any white-space in the input, since that makes a lot of sense. Please tell us your opinion about this change. Do you really want it to accept "3 14" as meaning 314 as it does now?]

Function: void mpz_swap (mpz_t rop1, mpz_t rop2)
Swap the values rop1 and rop2 efficiently.

Combined Initialization and Assignment Functions

For convenience, GMP 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 GMP integers to standard C types. Functions for converting to GMP integers are described in section Assignment Functions and section Input and Output Functions.

Function: mp_limb_t mpz_getlimbn (mpz_t op, mp_size_t n)
Return limb #n from op. This function allows for very efficient decomposition of a number in its limbs.

The function mpz_size can be used to determine the useful range for n.

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 decompose an integer into unsigned longs.

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. To find out if the value will fit, use the function mpz_fits_slong_p.

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.

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.

A pointer to the result string is returned. This pointer will will either equal str, or if that is NULL, will point to the allocated storage.

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)
@ifnottex Set rop to op1 + 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_si (mpz_t rop, mpz_t op1, long int op2)
Function: void mpz_mul_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
@ifnottex Set rop to op1 times op2.

Function: void mpz_addmul_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
@ifnottex Add op1 times op2 to rop.

Function: void mpz_mul_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
@ifnottex Set rop to op1 times 2 raised to op2. This operation can also be defined as a left shift, op2 steps.

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.

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 lets the user 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 q, mpz_t n, mpz_t d)
Function: unsigned long int mpz_tdiv_q_ui (mpz_t q, mpz_t n, unsigned long int d)
Set q to [n/d], truncated towards 0.

The function mpz_tdiv_q_ui returns the absolute value of the true remainder.

Function: void mpz_tdiv_r (mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_tdiv_r_ui (mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Set r to (n - [n/d] * d), where the quotient is truncated towards 0. Unless r becomes zero, it will get the same sign as n.

The function mpz_tdiv_r_ui returns the absolute value of the remainder.

Function: void mpz_tdiv_qr (mpz_t q, mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_tdiv_qr_ui (mpz_t q, mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Set q to [n/d], truncated towards 0. Set r to (n - [n/d] * d). Unless r becomes zero, it will get the same sign as n. If q and r are the same variable, the results are undefined.

The function mpz_tdiv_qr_ui returns the absolute value of the remainder.

Function: unsigned long int mpz_tdiv_ui (mpz_t n, unsigned long int d)
Like mpz_tdiv_r_ui, but the remainder is not stored anywhere; its absolute value is just returned.

Function: void mpz_fdiv_q (mpz_t q, mpz_t n, mpz_t d)
Function: unsigned long int mpz_fdiv_q_ui (mpz_t q, mpz_t n, unsigned long int d)
@ifnottex Set q to n/d, rounded towards -infinity.

The function mpz_fdiv_q_ui returns the remainder.

Function: void mpz_fdiv_r (mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_fdiv_r_ui (mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Set r to (n - n/d * d), where the quotient is rounded towards -infinity. Unless r becomes zero, it will get the same sign as d.

The function mpz_fdiv_r_ui returns the remainder.

Function: void mpz_fdiv_qr (mpz_t q, mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_fdiv_qr_ui (mpz_t q, mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Set q to n/d, rounded towards -infinity. Set r to (n - n/d * d). Unless r becomes zero, it will get the same sign as d. If q and r are the same variable, the results are undefined.

The function mpz_fdiv_qr_ui returns the remainder.

Function: unsigned long int mpz_fdiv_ui (mpz_t n, unsigned long int d)
Like mpz_fdiv_r_ui, but the remainder is not stored anywhere; it is just returned.

Function: void mpz_cdiv_q (mpz_t q, mpz_t n, mpz_t d)
Function: unsigned long int mpz_cdiv_q_ui (mpz_t q, mpz_t n, unsigned long int d)
@ifnottex Set q to n/d, rounded towards +infinity.

The function mpz_cdiv_q_ui returns the negated remainder.

Function: void mpz_cdiv_r (mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_cdiv_r_ui (mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Set r to (n - n/d * d), where the quotient is rounded towards +infinity. Unless r becomes zero, it will get the opposite sign as d.

The function mpz_cdiv_r_ui returns the negated remainder.

Function: void mpz_cdiv_qr (mpz_t q, mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_cdiv_qr_ui (mpz_t q, mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Set q to n/d, rounded towards +infinity. Set r to (n - n/d * d). Unless r becomes zero, it will get the opposite sign as d. If q and r are the same variable, the results are undefined.

The function mpz_cdiv_qr_ui returns the negated remainder.

Function: unsigned long int mpz_cdiv_ui (mpz_t n, unsigned long int d)
Like mpz_tdiv_r_ui, but the remainder is not stored anywhere; its negated value is just returned.

Function: void mpz_mod (mpz_t r, mpz_t n, mpz_t d)
Function: unsigned long int mpz_mod_ui (mpz_t r, mpz_t n, unsigned long int d)
Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative.

The function mpz_mod_ui returns the remainder.

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

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 q, mpz_t n, unsigned long int d)
@ifnottex Set q to n divided by 2 raised to d. The quotient is truncated towards 0.

Function: void mpz_tdiv_r_2exp (mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Divide n by (2 raised to d), rounding the quotient towards 0, and put the remainder in r. Unless it is zero, r will have the same sign as n.

Function: void mpz_fdiv_q_2exp (mpz_t q, mpz_t n, unsigned long int d)
@ifnottex Set q to n divided by 2 raised to d, rounded towards -infinity. This operation can also be defined as arithmetic right shift d bit positions.

Function: void mpz_fdiv_r_2exp (mpz_t r, mpz_t n, unsigned long int d)
@ifnottex Divide n by (2 raised to d), rounding the quotient towards -infinity, and put the remainder in r. The sign of r will always be positive. This operation can also be defined as masking of the d least significant bits.

Exponentiation 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)
@ifnottex 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)
@ifnottex Set rop to base raised to exp. The case of 0^0 yields 1.

Root Extraction Functions

Function: int mpz_root (mpz_t rop, mpz_t op, unsigned long int n)
@ifnottex Set rop to the truncated integer part of the nth root of op. Return non-zero if the computation was exact, i.e., if op is rop to the nth power.

Function: void mpz_sqrt (mpz_t rop, mpz_t op)
@ifnottex Set rop to the truncated integer part of the square root of op.

Function: void mpz_sqrtrem (mpz_t rop1, mpz_t rop2, mpz_t op)
@ifnottex Set rop1 to the truncated integer part of the square root of op, like mpz_sqrt. Set rop2 to op-rop1*rop1, (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_power_p (mpz_t op)
@ifnottex Return non-zero if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op equals a raised to b. Return zero otherwise.

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 n, int reps)
If this function returns 0, n is definitely not prime. If it returns 1, then n is `probably' prime. If it returns 2, then n is surely prime. Reasonable values of reps vary from 5 to 10; a higher value lowers the probability for a non-prime to pass as a `probable' prime.

The function uses Miller-Rabin's probabilistic test.

Function: int mpz_nextprime (mpz_t rop, mpz_t op)
Set rop to the next prime greater than op.

This function uses a probabilistic algorithm to identify primes, but for for practical purposes it's adequate, since the chance of a composite passing will be extremely small.

Function: void mpz_gcd (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to the greatest common divisor of op1 and op2. The result is always positive even if either of or both input operands are negative.

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: void mpz_lcm (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to the least common multiple of op1 and op2.

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 exists, zero otherwise. When the function returns zero, rop is undefined.

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. op2 should be odd and must be positive.

Function: int mpz_si_kronecker (long a, mpz_t b);
Function: int mpz_ui_kronecker (unsigned long a, mpz_t b);
Function: int mpz_kronecker_si (mpz_t a, long b);
Function: int mpz_kronecker_ui (mpz_t a, unsigned long b);
@ifnottex Calculate the value of the Kronecker/Jacobi symbol (a/b), with the Kronecker extension (a/2)=(2/a) when a odd, or (a/2)=0 when a even. All values of a and b give a well-defined result. See Henri Cohen, section 1.4.2, for more information (see section References). See also the example program `demos/qcn.c' which uses mpz_kronecker_ui.

Function: unsigned long int mpz_remove (mpz_t rop, mpz_t op, mpz_t f)
Remove all occurrences of the factor f from op and store the result in rop. Return the multiplicity of f in op.

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

Function: void mpz_bin_ui (mpz_t rop, mpz_t n, unsigned long int k)
Function: void mpz_bin_uiui (mpz_t rop, unsigned long int n, unsigned long int k)
Compute the binomial coefficient @ifnottex n over k and store the result in rop. Negative values of n are supported by mpz_bin_ui, using the identity @ifnottex bin(-n,k) = (-1)^k * bin(n+k-1,k) (see Knuth volume 1 section 1.2.6 part G).

Function: void mpz_fib_ui (mpz_t rop, unsigned long int n)
Compute the nth Fibonacci number and store the result in rop.

Comparison Functions

Function: int mpz_cmp (mpz_t op1, mpz_t op2)
@ifnottex Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < 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)
@ifnottex Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2.

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

Function: int mpz_cmpabs (mpz_t op1, mpz_t op2)
Function: int mpz_cmpabs_ui (mpz_t op1, unsigned long int op2)
@ifnottex Compare the absolute values of op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2.

Macro: int mpz_sgn (mpz_t op)
@ifnottex Return +1 if op > 0, 0 if op = 0, and -1 if op < 0.

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_xor (mpz_t rop, mpz_t op1, mpz_t op2)
Set rop to op1 exclusive-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 a future release.

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 rop.

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

Function: int mpz_tstbit (mpz_t op, unsigned long int bit_index)
Check bit bit_index in op and return 0 or 1 accordingly.

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.

Random Number Functions

The random number functions of GMP come in two groups; older function that rely on a global state, and newer functions that accept a state parameter that is read and modified. Please see the section Random Number Functions for more information on how to use and not to use random number functions.

Function: void mpz_urandomb (mpz_t rop, gmp_randstate_t state,
unsigned long int n) Generate a uniformly distributed random integer in the range @ifnottex 0 to 2^n - 1, inclusive.

The variable state must be initialized by calling one of the gmp_randinit functions (section Random State Initialization) before invoking this function.

Function: void mpz_urandomm (mpz_t rop, gmp_randstate_t state,
mpz_t n) Generate a uniform random integer in the range 0 to @ifnottex n - 1, inclusive.

The variable state must be initialized by calling one of the gmp_randinit functions (section Random State Initialization) before invoking this function.

Function: void mpz_rrandomb (mpz_t rop, gmp_randstate_t state, unsigned long int n)
Generate a random integer 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. The random number will be in the range @ifnottex 0 to 2^n - 1, inclusive.

The variable state must be initialized by calling one of the gmp_randinit functions (section Random State Initialization) before invoking this function.

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.

This function is obsolete. Use mpz_urandomb or mpz_urandomm instead.

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.

This function is obsolete. Use mpz_rrandomb instead.

Miscellaneous Functions

Function: int mpz_fits_ulong_p (mpz_t op)
Function: int mpz_fits_slong_p (mpz_t op)
Function: int mpz_fits_uint_p (mpz_t op)
Function: int mpz_fits_sint_p (mpz_t op)
Function: int mpz_fits_ushort_p (mpz_t op)
Function: int mpz_fits_sshort_p (mpz_t op)
Return non-zero iff the value of op fits in an unsigned long int, signed long int, unsigned int, signed int, unsigned short int, or signed short int, respectively. Otherwise, return zero.

Macro: int mpz_odd_p (mpz_t op)
Macro: int mpz_even_p (mpz_t op)
Determine whether op is odd or even, respectively. Return non-zero if yes, zero if no. These macros evaluate their arguments more than once.

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.

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.