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


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.


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