DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK

# (gmp.info.gz) Integer Division Info Catalog (gmp.info.gz) Integer Arithmetic (gmp.info.gz) Integer Functions (gmp.info.gz) Integer Exponentiation
```
Division Functions
==================

Division is undefined if the divisor is zero.  Passing a zero divisor
to the division or modulo functions (including the modular powering
functions `mpz_powm' and `mpz_powm_ui'), will cause an intentional
division by zero.  This lets a program handle arithmetic exceptions in
these functions the same way as for normal C `int' arithmetic.

- Function: void mpz_cdiv_q (mpz_t Q, mpz_t N, mpz_t D)
- Function: void mpz_cdiv_r (mpz_t R, mpz_t N, mpz_t D)
- Function: void mpz_cdiv_qr (mpz_t Q, mpz_t R, mpz_t N, mpz_t D)
- Function: unsigned long int mpz_cdiv_q_ui (mpz_t Q, mpz_t N,
unsigned long int D)
- Function: unsigned long int mpz_cdiv_r_ui (mpz_t R, mpz_t N,
unsigned long int D)
- Function: unsigned long int mpz_cdiv_qr_ui (mpz_t Q, mpz_t R,
mpz_t N, unsigned long int D)
- Function: unsigned long int mpz_cdiv_ui (mpz_t N,
unsigned long int D)
- Function: void mpz_cdiv_q_2exp (mpz_t Q, mpz_t N,
unsigned long int B)
- Function: void mpz_cdiv_r_2exp (mpz_t R, mpz_t N,
unsigned long int B)

- Function: void mpz_fdiv_q (mpz_t Q, mpz_t N, mpz_t D)
- Function: void mpz_fdiv_r (mpz_t R, mpz_t N, mpz_t D)
- Function: void mpz_fdiv_qr (mpz_t Q, mpz_t R, mpz_t N, mpz_t D)
- Function: unsigned long int mpz_fdiv_q_ui (mpz_t Q, mpz_t N,
unsigned long int D)
- Function: unsigned long int mpz_fdiv_r_ui (mpz_t R, mpz_t N,
unsigned long int D)
- Function: unsigned long int mpz_fdiv_qr_ui (mpz_t Q, mpz_t R,
mpz_t N, unsigned long int D)
- Function: unsigned long int mpz_fdiv_ui (mpz_t N,
unsigned long int D)
- Function: void mpz_fdiv_q_2exp (mpz_t Q, mpz_t N,
unsigned long int B)
- Function: void mpz_fdiv_r_2exp (mpz_t R, mpz_t N,
unsigned long int B)

- Function: void mpz_tdiv_q (mpz_t Q, mpz_t N, mpz_t D)
- Function: void mpz_tdiv_r (mpz_t R, mpz_t N, mpz_t D)
- Function: void mpz_tdiv_qr (mpz_t Q, mpz_t R, mpz_t N, mpz_t D)
- Function: unsigned long int mpz_tdiv_q_ui (mpz_t Q, mpz_t N,
unsigned long int D)
- Function: unsigned long int mpz_tdiv_r_ui (mpz_t R, mpz_t N,
unsigned long int D)
- Function: unsigned long int mpz_tdiv_qr_ui (mpz_t Q, mpz_t R,
mpz_t N, unsigned long int D)
- Function: unsigned long int mpz_tdiv_ui (mpz_t N,
unsigned long int D)
- Function: void mpz_tdiv_q_2exp (mpz_t Q, mpz_t N,
unsigned long int B)
- Function: void mpz_tdiv_r_2exp (mpz_t R, mpz_t N,
unsigned long int B)

Divide N by D, forming a quotient Q and/or remainder R.  For the
`2exp' functions, D=2^B.  The rounding is in three styles, each
suiting different applications.

* `cdiv' rounds Q up towards +infinity, and R will have the
opposite sign to D.  The `c' stands for "ceil".

* `fdiv' rounds Q down towards -infinity, and R will have the
same sign as D.  The `f' stands for "floor".

* `tdiv' rounds Q towards zero, and R will have the same sign
as N.  The `t' stands for "truncate".

In all cases Q and R will satisfy N=Q*D+R, and R will satisfy
0<=abs(R)<abs(D).

The `q' functions calculate only the quotient, the `r' functions
only the remainder, and the `qr' functions calculate both.  Note
that for `qr' the same variable cannot be passed for both Q and R,
or results will be unpredictable.

For the `ui' variants the return value is the remainder, and in
fact returning the remainder is all the `div_ui' functions do.  For
`tdiv' and `cdiv' the remainder can be negative, so for those the
return value is the absolute value of the remainder.

For the `2exp' variants the divisor is 2^B.  These functions are
implemented as right shifts and bit masks, but of course they
round the same as the other functions.

For positive N both `mpz_fdiv_q_2exp' and `mpz_tdiv_q_2exp' are
simple bitwise right shifts.  For negative N, `mpz_fdiv_q_2exp' is
effectively an arithmetic right shift treating N as twos complement
the same as the bitwise logical functions do, whereas
`mpz_tdiv_q_2exp' effectively treats N as sign and magnitude.

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

`mpz_mod_ui' is identical to `mpz_fdiv_r_ui' above, returning the
remainder as well as setting R.  See `mpz_fdiv_ui' above if only
the return value is wanted.

- Function: void mpz_divexact (mpz_t Q, mpz_t N, mpz_t D)
- Function: void mpz_divexact_ui (mpz_t Q, mpz_t N, unsigned long D)
Set Q to N/D.  These functions produce correct results only when
it is known in advance that D divides N.

These routines are much faster than the other division functions,
and are the best choice when exact division is known to occur, for
example reducing a rational to lowest terms.

- Function: int mpz_divisible_p (mpz_t N, mpz_t D)
- Function: int mpz_divisible_ui_p (mpz_t N, unsigned long int D)
- Function: int mpz_divisible_2exp_p (mpz_t N, unsigned long int B)
Return non-zero if N is exactly divisible by D, or in the case of
`mpz_divisible_2exp_p' by 2^B.

N is divisible by D if there exists an integer Q satisfying N =
Q*D.  Unlike the other division functions, D=0 is accepted and
following the rule it can be seen that only 0 is considered
divisible by 0.

- Function: int mpz_congruent_p (mpz_t N, mpz_t C, mpz_t D)
- Function: int mpz_congruent_ui_p (mpz_t N, unsigned long int C,
unsigned long int D)
- Function: int mpz_congruent_2exp_p (mpz_t N, mpz_t C, unsigned long
int B)
Return non-zero if N is congruent to C modulo D, or in the case of
`mpz_congruent_2exp_p' modulo 2^B.

N is congruent to C mod D if there exists an integer Q satisfying
N = C + Q*D.  Unlike the other division functions, D=0 is accepted
and following the rule it can be seen that N and C are considered
congruent mod 0 only when exactly equal.

``` Info Catalog (gmp.info.gz) Integer Arithmetic (gmp.info.gz) Integer Functions (gmp.info.gz) Integer Exponentiation
automatically generated byinfo2html