首先我们来看下 The Java? Language Specification 中官方对它的定义:
The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.
In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.
The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.
This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).
It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.
If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.
大体的意思就是说:Java中的%代表求余(注意:不是求模),在C和C++中,%仅支持整数操作,而在Java中,不仅接受整数操作,而且接受浮点数,此运算符计算的结果的符号与被除数的符号相同,如果除数为0,将抛出ArithmeticException。
例:
package deep;
public class Test {
public static void main(String[] args) {
int a = 5 % 3; // 2
int b = 5 / 3; // 1
System.out.println("5%3 produces " + a + " (note that 5/3 produces "
+ b + ")");
int c = 5 % (-3); // 2
int d = 5 / (-3); // -1
System.out.println("5%(-3) produces " + c
+ " (note that 5/(-3) produces " + d + ")");
int e = (-5) % 3; // -2
int f = (-5) / 3; // -1
System.out.println("(-5)%3 produces " + e
+ " (note that (-5)/3 produces " + f + ")");
int g = (-5) % (-3); // -2
int h = (-5) / (-3); // 1
System.out.println("(-5)%(-3) produces " + g
+ " (note that (-5)/(-3) produces " + h + ")");
}
}
运行结果:
5%3 produces 2 (note that 5/3 produces 1)
5%(-3) produces 2 (note that 5/(-3) produces -1)
(-5)%3 produces -2 (note that (-5)/3 produces -1)
(-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)
再看看官方对浮点数的提及:
The result of a floating-point remainder operation as computed by the % operator is not the same as that produced by the remainder operation defined by IEEE 754. The IEEE 754 remainder operation computes the remainder from a rounding division, not a truncating division, and so its behavior is not analogous to that of the usual integer remainder operator. Instead, the Java programming language defines % on floating-point operations to behave in a manner analogous to that of the integer remainder operator; this may be compared with the C library function fmod. The IEEE 754 remainder operation may be computed by the library routine Math.IEEEremainder.
The result of a floating-point remainder operation is determined by the rules of IEEE 754 arithmetic:
- If either operand is NaN, the result is NaN.
- If the result is not NaN, the sign of the result equals the sign of
the dividend.
- If the dividend is an infinity, or the divisor is a zero, or both,
the result is NaN.
- If the dividend is finite and the divisor is an infinity, the result
equals the dividend.
- If the dividend is a zero and the divisor is finite, the result
equals the dividend.
- In the remaining cases, where neither an infinity, nor a zero, nor
NaN is involved, the floating-point remainder r from the division of
a dividend n by a divisor d is defined by the mathematical relation r
= n - (d · q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is
as large as possible without exceeding the magnitude of the true
mathematical quotient of n and d.
Evaluation of a floating-point remainder operator % never throws a run-time exception, even if the right-hand operand is zero. Overflow, underflow, or loss of precision cannot occur.
例:
package deep;
public class Test {
public static void main(String[] args) {
double a = 5.0 % 3.0; // 2.0
System.out.println("5.0%3.0 produces " + a);
double b = 5.0 % (-3.0); // 2.0
System.out.println("5.0%(-3.0) produces " + b);
double c = (-5.0) % 3.0; // -2.0
System.out.println("(-5.0)%3.0 produces " + c);
double d = (-5.0) % (-3.0); // -2.0
System.out.println("(-5.0)%(-3.0) produces " + d);
}
}
运行结果:
5.0%3.0 produces 2.0
5.0%(-3.0) produces 2.0
(-5.0)%3.0 produces -2.0
(-5.0)%(-3.0) produces -2.0