You are here: JEE FAQ » Java FAQ

Java

JSP

Servlet

# What are results of the Remainder Operator in Java?

The remainder operator % returns the remainder from the division of the left operand by the right operand;the left-hand operand is the dividend and the right-hand operand is the divisor.
In Java, the remainder operator % can be used with integral operands as well as floating-point operands (\$15.17.3 Remainder Operator %).

For the integral type remainder operation whose operands are integers after binary numeric promotion, the remainder operator is defined by the following identity (if b is not 0): (a/b)*b + a%b = a

• If the divisor b evaluates to zero, then an ArithmeticException is thrown.
• If the dividend evaluates to zero, the result is zero.
• The sign of the result equals the sign of the dividend.
• The magnitude of the result |a%b| is always less than the magnitude of the divisor |b|.

The result of a floating-point remainder operation is determined by the rules
of IEEE 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. Printer-friendly version | Mail this to a friend    |   |