Java Reference
In-Depth Information
may be permissible in certain regions of code for an implementation to use an element of
the double-extended-exponent value set instead.
Except for NaN, floating-point values are
ordered
; arranged from smallest to largest, they
are negative infinity, negative finite nonzero values, positive and negative zero, positive fi-
nite nonzero values, and positive infinity.
IEEE 754 allows multiple distinct NaN values for each of its single and double floating-
point formats. While each hardware architecture returns a particular bit pattern for NaN
when a new NaN is generated, a programmer can also create NaNs with different bit pat-
terns to encode, for example, retrospective diagnostic information.
For the most part, the Java SE platform treats NaN values of a given type as though col-
lapsed into a single canonical value, and hence this specification normally refers to an ar-
bitrary NaN as though to a canonical value.
However, version 1.3 of the Java SE platform introduced methods enabling the
programmer to distinguish between NaN values: the
Float.floatToRawIntBits
and
Double.doubleToRawLongBits
methods. The interested reader is referred to the specifica-
tions for the
Float
and
Double
classes for more information.
Positive zero and negative zero compare equal; thus the result of the expression
0.0==-0.0
is
true
and the result of
0.0>-0.0
is false. But other operations can distinguish positive and
negative zero; for example,
1.0/0.0
has the value positive infinity, while the value of
1.0/-0.0
is negative infinity.
NaN is
unordered
, so:
• The numerical comparison operators
<
,
<=
,
>
, and
>=
return
false
if either or both
operands are NaN (§
15.20.1
).
• The equality operator
==
returns
false
if either operand is NaN.
In particular,
(x<y) == !(x>=y)
will be
false
if
x
or
y
is NaN.
In particular,
x!=x
is
true
if and only if
x
is NaN.
4.2.4. Floating-Point Operations
The Java programming language provides a number of operators that act on floating-point
values:
• The comparison operators, which result in a value of type
boolean
: