where the value shall be zero if abs(i) < abs(j); otherwise, the sign of the value shall be positive if i and j have the same sign and negative if i and j have different signs.
A term of the form i mod j shall be an error if j is zero or negative; otherwise, the value of i mod j shall be that value of (i - (k * j)) for integral k such that 0 <= i mod j < j.
NOTE 2 — Only for i >= 0 and j > 0 does the relation (i div j) * j + i mod j = i hold.
Kathleen Jensen, Niklaus Wirth. PASCAL User Manual and Report - ISO Pascal Standard(PDF). 1991.
div
divide and truncate (i.e., value is not rounded)
mod
modulus: let Remainder = A - (A div B) * B; if Remainder < 0 then A mod B = Remainder + B
otherwise A mod B = Remainder
^A. van Wijngaarden, B. J. Mailloux, J. E. L. Peck, C. H. A. Koster, M. Sintzoff, C. H. Lindsey, L. G. L.T. Meertens and R. G. Fisker. Revised Report on the Algorithmic Language Algol 68. IFIP W.G. 2.1. 10.2.3.3. Operations on integral operands
……
m) OP «÷, %, OVER» = (L INT a, b) L INT:
IF b ≠ L 0
THEN L INT q := L 0, r := ABS a;
WHILE (r := r - ABS b) ≥ L 0 DO q := q + L 1 OD;
(a < L 0 AND b > L 0 OR a ≥ L 0 AND b < L 0 | -q | q)
FI;
n) OP «÷×, ÷*, %*, MOD» = (L INT a, b) L INT:
(L INT r = a - a ÷ b × b; r < 0 | r + ABS b | r);
^Jones, Derek M. The New C Standard: An Economic and Cultural Commentary(PDF). Addison-Wesley. 2003 [2018-07-11]. ISBN 9780201709179. (原始内容(PDF)存档于2018-07-11) (英语).
C99 reflects almost universal processor behavior (as does the Fortran Standard). This definition truncates toward zero and the expression (-(a/b) == (-a)/b) && (-(a/b) == a/(-b)) is always true. It also means that the absolute value of the result does not depend on the signs of the operands; for example:
When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than or equal to the algebraic quotient or the smallest integer greater than or equal to the algebraic quotient is implementation-defined, as is the sign of the result of the % operator.
If either operand is negative, the behavior may differ between C90 and C99, depending on the implementation-defined behavior of the C90 implementation.
#include<stdio.h>intmain(void){intx=-1,y=+3;if((x%y>0)||((x+y)%y==x%y))printf("This is a C90 translator behaving differently than C99\n");}
Quoting from the C9X Revision Proposal, WG14/N613, that proposed this change:
The origin of this practice seems to have been a desire to map C’s division directly to the “natural” behavior of the target instruction set, whatever it may be, without requiring extra code overhead that might be necessary to check for special cases and enforce a particular behavior. However, the argument that Fortran programmers are unpleasantly surprised by this aspect of C and that there would be negligible impact on code efficiency was accepted by WG14, who agreed to require Fortran-like behavior in C99.
^Knuth, Donald. E. The Art of Computer Programming. Addison-Wesley. 1972 (英语).
If is any real number, we write
= the greatest integer less than or equal to (the floor of );
= the least integer greater than or equal to (the ceiling of ).
……
The following formulas and examples are easily verified:
……
if and only if is an integer,
if and only if is not an integer;
;
.
……
If and are any real numbers, we define the following binary operation:
;.(1)
From this definition we can see that, when ,
.(2)
Consequently
a) if , then ;
b) if , then ;
c) the quantity is an integral multiple of .
We call the remainder when is divided by ; similarly, we call the quotient.
When and are integers, “” is therefore a familiar operation:
,,.(3)
We have if and only if is a multiple of , that is, if and only if is divisible by . The notation , read “ divides ,” means that is a positive integer and .
The “” operation is useful also when and take arbitrary real values. For example, with trigonometric functions we can write
.
The quantity is the fractional part of ; we have, by Eq. (1),
THEOREM. For any real numbers and with , there exists a unique pair of numbers satisfying the following conditions:
(a) ;
(b) ;
(c) .
For the particular case of integers, the conditions (a)-(c) correspond to part of the axioms for Euclidean rings [9].
With the notational conventions of the theorem, we define and . Clearly the correspondingly labeled conditions are satisfied.
Properties
where and is as defined earlier. ()
^ 6.06.1ISO/IEC 8652:2012 - Information technology — Programming languages — Ada. ISO, IEC. 2012. sec. 4.5.5 Multiplying Operators.
^ISO/IEC 14882:2003: Programming languages – C++. International Organization for Standardization (ISO), International Electrotechnical Commission (IEC). 2003. sec. 5.6.4. the binary % operator yields the remainder from the division of the first expression by the second. .... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined
^ISO/IEC 9899:1990: Programming languages – C. ISO, IEC. 1990. sec. 7.5.6.4. The fmod function returns the value x - i * y, for some integer i such that, if y is nonzero, the result has the same sign as x and magnitude less than the magnitude of y.
^ANSI. Programming Languages — Full BASIC. New York: American National Standards Institute. 28 January 1987. § 5.4.4. The remainder function, i.e., X-Y*IP(X/Y).
^GLSL Language Specification, Version 4.50.7(PDF). section 5.9 Expressions. If both operands are non-negative, then the remainder is non-negative. Results are undefined if one or both operands are negative.
^Operators. Microsoft. [2021-07-19]. The % operator is defined only in cases where either both sides are positive or both sides are negative. Unlike C, it also operates on floating-point data types, as well as integers.