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.
Divide n by d, forming a quotient q and/or remainder r. For the2expfunctions, d=2^b. The rounding is in three styles, each suiting different applications.
cdivrounds q up towards +infinity, and r will have the opposite sign to d. Thecstands for “ceil”.fdivrounds q down towards −infinity, and r will have the same sign as d. Thefstands for “floor”.tdivrounds q towards zero, and r will have the same sign as n. Thetstands for “truncate”.In all cases q and r will satisfy n=q*d+r, and r will satisfy 0<=abs(r)<abs(d).
The
qfunctions calculate only the quotient, therfunctions only the remainder, and theqrfunctions calculate both. Note that forqrthe same variable cannot be passed for both q and r, or results will be unpredictable.For the
uivariants the return value is the remainder, and in fact returning the remainder is all thediv_uifunctions do. Fortdivandcdivthe remainder can be negative, so for those the return value is the absolute value of the remainder.For the
2expvariants 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_2expandmpz_tdiv_q_2expare simple bitwise right shifts. For negative n,mpz_fdiv_q_2expis effectively an arithmetic right shift treating n as twos complement the same as the bitwise logical functions do, whereasmpz_tdiv_q_2expeffectively treats n as sign and magnitude.
Set r to n
modd. The sign of the divisor is ignored; the result is always non-negative.
mpz_mod_uiis identical tompz_fdiv_r_uiabove, returning the remainder as well as setting r. Seempz_fdiv_uiabove if only the return value is wanted.
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.
Return non-zero if n is exactly divisible by d, or in the case of
mpz_divisible_2exp_pby 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.
Return non-zero if n is congruent to c modulo d, or in the case of
mpz_congruent_2exp_pmodulo 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.