Java Reference
In-Depth Information
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 res-
ult 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 res-
ults. 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 ex-
pectations. 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.
Multiplying infinity by zero yields NaN. Multiplying infinity by a non-zero
finite number produces an infinity of the appropriate sign.
Floating-point division and remainder can produce infinities or NaN but
never throw an exception. This table shows the results of the various
combinations: