Java Reference
In-Depth Information
127, a 1
bit to the left of the binary point is assumed (i.e.
b
0
is fixed at 1 in the above
formula) thereby increasing the number of effective fractional bits by one. This
holds similarly for
double
numbers in the
For
float
numbers with exponent values in the range
−
126 to
+
−
−
1023 range. This provides
an effective significand of 24 bits for
float
,53bits for
double
. Such numbers
are referred to as
normalized
. This scheme for
float
provides at least six digits of
decimal precision while
double
provides at least 15 digits of decimal precision.
The special floating-point cases include the following:
Denormalized
-Ifthe bits in the exponent all equal 0 and the significand bits do not
all equal 0, then the exponent is treated as
−
126 for
float
and
−
1022 for
double
(i.e. the binary point moves left by 126 places and 1022 places, respectively) and the
implied bit to the left of the binary points is 0. These
denormalized
numbers allow for
smaller values (i.e. closer to zero) than normalized alone.
±
Zero
-Ifthe bits in the exponent and the significand all equal 0, then the floating-
point value is
1022 to
−
0or
+
0 depending on the sign bit. (See Appendix 3 for definitions of
0.)
±
Infinity
-Ifall the bits in the exponent equal 1 and all the bits in the significand
equal 0, then the floating-point value is plus or minus infinity according to the sign.
Not-a-Number (
NaN
)
-Ifall the bits in the exponent equal 1 and any of the bits in
the significand equal 1, then the floating-point value is Not-a-Number (
NaN
) and the
sign value is ignored. Not-a-Number occurs when an operation has no mathematical
meaning, such as 0.0/0.0, or when any operation is done with an existing Not-a-Number.
+
0 and
−
Overflows, underflows, and divide by zero in Java floating-point operations do
not lead to error states (Java
Exceptions
are discussed in Section 3.9). A division
by zero leads to the plus or minus infinity value unless the numerator is also zero,
in which case the Not-a-Number value results. You can test for Not-a-Number
values using methods from the floating-point wrapper classes (see Chapter 3)
such as
Double.isNaN (double x)
. Also, the Not-a-Number value can be
checked for with the test
if (x!
=
x) statement;
which always returns
true
for Not-a-Number values. Numerical comparisons
such as
if (x
<
y) statement;
always return
false
if either or both values are Not-a-Number.
Round-off takes the binary value nearest to the exact (or higher precision
intermediate) value. If two binary values are equally close, then the even value
(the one with its last bit equal to 0) is chosen.
In general, it is far safer to do floating-point calculations in
double
type. This
helps to reduce round-off errors that can reduce precision during intermediate
calculations. (You can always cast the final value to
float
if that is a more
convenient size for I/O or storage.) There can be some performance tradeoff,
Search WWH ::
Custom Search