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 Division Operator / 15.17.2<br />

❖ If the type of the multiplication expression is float, then either the float<br />

value set or the float-extended-exponent value set may be chosen, at the<br />

whim of the implementation.<br />

❖ If the type of the multiplication expression is double, then either the double<br />

value set or the double-extended-exponent value set may be chosen, at<br />

the whim of the implementation.<br />

Next, a value must be chosen from the chosen value set to represent the product.<br />

If the magnitude of the product is too large to represent, we say the operation<br />

overflows; the result is then an infinity of appropriate sign. Otherwise,<br />

the product is rounded to the nearest value in the chosen value set using IEEE<br />

754 round-to-nearest mode. <strong>The</strong> <strong>Java</strong> programming language requires support<br />

of gradual underflow as defined by IEEE 754 (§4.2.4).<br />

Despite the fact that overflow, underflow, or loss of information may occur,<br />

evaluation of a multiplication operator * never throws a run-time exception.<br />

15.17.2 Division Operator /<br />

Gallia est omnis divisa in partes tres.<br />

— Commentaries on the Gallic Wars<br />

<strong>The</strong> binary / operator performs division, producing the quotient of its operands.<br />

<strong>The</strong> left-hand operand is the dividend and the right-hand operand is the divisor.<br />

Integer division rounds toward 0. That is, the quotient produced for operands<br />

n and d that are integers after binary numeric promotion (§5.6.2) is an integer<br />

value q whose magnitude is as large as possible while satisfying d ⋅ q ≤ n ;<br />

moreover, q is positive when n ≥ d and n and d have the same sign, but q is negative<br />

when n ≥<br />

d and n and d have opposite signs. <strong>The</strong>re is one special case that<br />

does not satisfy this rule: if the dividend is the negative integer of largest possible<br />

magnitude for its type, and the divisor is -1, then integer overflow occurs and the<br />

result is equal to the dividend. Despite the overflow, no exception is thrown in this<br />

case. On the other hand, if the value of the divisor in an integer division is 0, then<br />

an ArithmeticException is thrown.<br />

<strong>The</strong> result of a floating-point division is determined by the specification of<br />

IEEE arithmetic:<br />

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

• If the result is not NaN, the sign of the result is positive if both operands have<br />

the same sign, negative if the operands have different signs.<br />

DRAFT<br />

493

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

Saved successfully!

Ooh no, something went wrong!