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


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)
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_ui (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 times op2.

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)
Set rop to (I.e., round the quotient towards

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)
Set rop to (I.e., round the quotient towards

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_sqrt (mpz_t rop, mpz_t op)
Set rop to

Function: void mpz_sqrtrem (mpz_t rop1, mpz_t rop2, mpz_t op)
Set rop1 to like mpz_sqrt. Set rop2 to (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.

Function: int mpz_probab_prime_p (mpz_t op, int reps)
If this function returns 0, op is definitely not prime. If it returns 1, then op is `probably' prime. The probability of a false positive is 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_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.

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

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: 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_mul_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 times 2 raised to op2. This operation can also be defined as a left shift, op2 steps.

Function: void mpz_tdiv_q_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 divided by 2 raised to op2. The quotient is rounded towards 0.

Function: void mpz_fdiv_q_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 divided by 2 raised to op2. The quotient is rounded towards -infinity.

Function: void mpz_tdiv_r_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
Divide op1 by (2 raised to op2) and put the remainder in rop. The sign of rop will have the same sign as op1, unless is becomes zero.

Function: void mpz_fdiv_r_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
Divide op1 by (2 raised to op2) and put the remainder in rop. The sign of rop will always be positive.

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


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