Analysis of information sources in references of the Wikipedia article "模除" in Chinese language version.
X modulo Y, i.e., X-Y*INT(X/Y).
The remainder function, i.e., X-Y*IP(X/Y).
(-(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:+10 / +3 == +3
+10 % +3 == +1
-10 / +3 == -3
-10 % +3 == -1
+10 / -3 == -3
+10 % -3 == +1
-10 / -3 == +3
-10 % -3 == -1
/
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. #include <stdio.h>
int main(void)
{
int x = -1,
y = +3;
if ((x%y > 0) ||
((x+y)%y == x%y))
printf("This is a C90 translator behaving differently than C99\n");
}
i div j
shall be an error if j
is zero; otherwise, the value of i div j
shall be such thatabs(i) - abs(j) < abs((i div j) * j) <= abs(i)
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.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
.i >= 0
and j > 0
does the relation (i div j) * j + i mod j = i
hold.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 |
If both operands are non-negative, then the remainder is non-negative. Results are undefined if one or both operands are negative.
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.
i div j
shall be an error if j
is zero; otherwise, the value of i div j
shall be such thatabs(i) - abs(j) < abs((i div j) * j) <= abs(i)
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.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
.i >= 0
and j > 0
does the relation (i div j) * j + i mod j = i
hold.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/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:+10 / +3 == +3
+10 % +3 == +1
-10 / +3 == -3
-10 % +3 == -1
+10 / -3 == -3
+10 % -3 == +1
-10 / -3 == +3
-10 % -3 == -1
/
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. #include <stdio.h>
int main(void)
{
int x = -1,
y = +3;
if ((x%y > 0) ||
((x+y)%y == x%y))
printf("This is a C90 translator behaving differently than C99\n");
}
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); |