Java Reference
In-Depth Information
Rule #1
The operation never results in an error even if the right-hand operand is a floating-point zero.
Rule #2
The result is NaN if either operand is NaN . For example,
float f1;
double d1;
f1 = Float.NaN % 10.5F; // Assigns Float.NaN to f1
f1 = 20.0F % Float.NaN; // Assigns Float.NaN to f1
f1 = Float.NaN % Float.NaN; // Assigns Float.NaN to f1
// A compile-time error. The expression is of the type double.
// double to float assignment is not allowed
f1 = Float.NaN % Double.NaN;
d1 = Float.NaN % Double.NaN; // Assigns Double.NaN to d1
Rule #3
If the right-hand operand is zero, the result is NaN. For example,
float f1;
f1 = 15.0F % 0.0F; // Assigns Float.NaN to f1
Rule #4
If the left-hand operand is infinity, the result is NaN. For example,
float f1;
f1 = Float.POSITIVE_INFINITY % 2.1F; // Assigns Float.NaN to f1
Rule #5
If none of the above rules apply, the modulus operator returns the remainder of the division of the left-hand
operand and the right-hand operand. The sign of the result is the same as the sign of the left-hand operand. For example,
float f1;
double d1;
f1 = 15.5F % 6.5F; // Assigns 2.5F to f1
d1 = 5.5 % 15.65; // Assigns 5.5 to d1
d1 = 0.0 % 3.78; // Assigns 0.0 to d1
d1 = 85.0 % Double.POSITIVE_INFINITY; // Assigns 85.0 to d1
d1 = -85.0 % Double.POSITIVE_INFINITY; // Assigns -85.0 to d1
d1 = 85.0 % Double.NEGATIVE_INFINITY; // Assigns 85.0 to d1
d1 = -85.0 % Double.NEGATIVE_INFINITY; // Assigns -85.0 to d1
Search WWH ::




Custom Search