Java Reference
In-Depth Information
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 mag-
nitude is as large as possible without exceeding the magnitude of the true mathem-
atical 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 oc-
cur.
Example 15.17.3-2. Floating-Point Remainder Operator
Click here to view code image
class Test2 {
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);
}
}
This program produces the output:
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
Search WWH ::




Custom Search