THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

The exact actions of the binary arithmetic operators depend on the types of operands involved. The following
sections look at the different rules for integer and floating-point arithmetic.


9.1.1. Integer Arithmetic


Integer arithmetic is modular two's-complement arithmeticthat is, if a value exceeds the range of its type (int
or long), it is reduced modulo the range. So integer arithmetic never overflows or underflows but only
wraps.


Integer division truncates toward zero (7/2 is 3 , and -7/2 is -3). For integer types, division and remainder
obey the rule


(x/y)*y + x%y == x


So 7%2 is 1, and -7%2 is 1. Dividing by zero or remainder by zero is invalid for integer arithmetic and
throws ArithmeticException.


Character arithmetic is integer arithmetic after the char is implicitly converted to intsee "Expression Type"
on page 215.


9.1.2. Floating-Point Arithmetic


Floating-point arithmetic can overflow to infinity (become too large for a double or float) or underflow
(become too small for a double or float). Underflow results in a loss of precision, possibly enough to
yield a zero value.[1] The result of an invalid expression, such as dividing infinity by infinity, is a NaN
valuefor "Not-a-Number."


[1] Detecting non-zero underflows is a non-trivial task that is beyond the scope of this book.

Arithmetic with finite operands performs as expected, within the limits of precision of double or float.
Signs of floating-point arithmetic results are also as expected. Multiplying two numbers having the same sign
results in a positive value; multiplying two numbers having opposite signs results in a negative value.


Adding two infinities results in the same infinity if their signs are the same, and NaN if their signs differ.
Subtracting infinities of the same sign produces NaN; subtracting infinities of opposite signs produces an
infinity of the same sign as the left operand. For example, ( - (- )) is. Arithmetic operations involving
any value that is NaN have a result that is also NaN. Overflows result in a value that is an infinity of the
proper sign. Underflows also result in values of the proper sign. Floating-point arithmetic has a negative zero
-0.0, which compares equal to +0.0. Although they compare equal, the two zeros can produce different
results. For example, the expression 1f/0f yields positive infinity and 1f/-0f yields negative infinity.


If the result of an underflow is -0.0 and if -0.0==0.0, how do you test for a negative zero? You must use
the zero in an expression where sign matters and then test the result. For example, if x has a zero value, the
expression 1/x will yield negative infinity if x is negative zero, or positive infinity if x is positive zero.


The rules for operations on infinities match normal mathematical expectations. Adding or subtracting any
number to or from either infinity results in that infinity. For example, (- + x) is - for any finite number x.


You can get an infinity value from the constants POSITIVE_INFINITY and NEGATIVE_INFINITY in
the wrapper classes Float and Double. For example, Double.NEGATIVE_INFINITY is the double
value of minus infinity.

Free download pdf