Java Reference
In-Depth Information
A
PPENDIX
E
Special Floating-Point Values
Dividing an integer by zero is invalid and throws
ArithmeticException
, but dividing a
floating-point value by zero does not cause an exception. Floating-point arithmetic can over-
flow to infinity if the result of the operation is too large for a
double
or a
float
, or under-
flow to zero if the result is too small for a
double
or a
float
. Java provides the special
floating-point values
POSITIVE_INFINITY
,
NEGATIVE_INFINITY
, and
NaN
(Not a Num-
ber) to denote these results. These values are defined as special constants in the
Float
class
and the
Double
class.
If a positive floating-point number is divided by zero, the result is
POSITIVE_INFINITY
.
If a negative floating-point number is divided by zero, the result is
NEGATIVE_INFINITY
.
If a floating-point zero is divided by zero, the result is
NaN
, which means that the result is
undefined mathematically. The string representations of these three values are
Infinity
,
-Infinity
, and
NaN
. For example,
System.out.print(
1.0
/
0
);
// Print Infinity
System.out.print(
-1.0
/
0
);
// Print -Infinity
System.out.print(
0.0
/
0
);
// Print NaN
These special values can also be used as operands in computations. For example, a number
divided by
POSITIVE_INFINITY
yields a positive zero. Table E.1 summarizes various com-
binations of the
/
,
*
,
%
,
+
, and
-
operators.
T
ABLE
E.1
Special Floating-Point Values
x
y
x/y
x
*
y
x%y
x
+
y
x
-
y
Finite
NaN
Finite
Finite
{
0.0
{
infinity
{
0.0
Finite
{
infinity
{
0.0
{
0.0
x
{
infinity
infinity
{
0.0
{
0.0
NaN
{
0.0
NaN
{
0.0
{
0.0
Finite
NaN
{
infinity
{
infinity
{
0.0
{
infinity
{
infinity
{
infinity
{
infinity
NaN
{
0.0
NaN
{
infinity
infinity
{
0.0
{
infinity
{
0.0
NaN
{
0.0
{
infinity
{
0.0
NaN
Any
NaN
NaN
NaN
NaN
NaN
Any
NaN
NaN
NaN
NaN
NaN
NaN
Note
If one of the operands is NaN, the result is NaN.
1272
Search WWH ::
Custom Search