10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPRESSIONS Remainder Operator % 15.17.3<br />

15.17.3 Remainder Operator %<br />

And on the pedestal these words appear:<br />

“My name is Ozymandias, king of kings:<br />

Look on my works, ye Mighty, and despair!”<br />

Nothing beside remains.<br />

—Ozymandias<br />

<strong>The</strong> binary % operator is said to yield the remainder of its operands from an<br />

implied division; the left-hand operand is the dividend and the right-hand operand<br />

is the divisor.<br />

In C and C++, the remainder operator accepts only integral operands, but in<br />

the <strong>Java</strong> programming language, it also accepts floating-point operands.<br />

<strong>The</strong> remainder operation for operands that are integers after binary numeric<br />

promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to<br />

a. This identity holds even in the special case that the dividend is the negative<br />

integer of largest possible magnitude for its type and the divisor is -1 (the remainder<br />

is 0). It follows from this rule that the result of the remainder operation can be<br />

negative only if the dividend is negative, and can be positive only if the dividend is<br />

positive; moreover, the magnitude of the result is always less than the magnitude<br />

of the divisor. If the value of the divisor for an integer remainder operator is 0,<br />

then an ArithmeticException is thrown.Examples:<br />

5%3 produces 2 (note that 5/3 produces 1)<br />

5%(-3) produces 2 (note that 5/(-3) produces -1)<br />

(-5)%3 produces -2 (note that (-5)/3 produces -1)<br />

(-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)<br />

<strong>The</strong> result of a floating-point remainder operation as computed by the % operator<br />

is not the same as that produced by the remainder operation defined by IEEE<br />

754. <strong>The</strong> IEEE 754 remainder operation computes the remainder from a rounding<br />

division, not a truncating division, and so its behavior is not analogous to that of<br />

the usual integer remainder operator. Instead, the <strong>Java</strong> programming language<br />

defines % on floating-point operations to behave in a manner analogous to that of<br />

the integer remainder operator; this may be compared with the C library function<br />

fmod. <strong>The</strong> IEEE 754 remainder operation may be computed by the library routine<br />

Math.IEEEremainder.<br />

<strong>The</strong> result of a floating-point remainder operation is determined by the rules<br />

of IEEE arithmetic:<br />

• If either operand is NaN, the result is NaN.<br />

• If the result is not NaN, the sign of the result equals the sign of the dividend.<br />

• If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.<br />

DRAFT<br />

495

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!