Java Reference
In-Depth Information
For comparison purposes,
+0.0
and
-0.0
are considered equal. Therefore, the expression
0.0 == -0.0
always
returns
true
.
Why does IEEE define two zeros if they are considered equal? The sign of zero is used to determine the result
of an arithmetic expression involving multiplication and division. The result of
3.0 * 0.0
is a positive zero (
0.0
),
whereas the result of
3.0 * (-0.0)
is a negative zero (
-0.0
). For a floating-point number
num
with the values
±Infinity, the relation 1/(1/num) = num holds true only because of two signed zeros.
Signed Infinities
The IEEE floating-point format allows for two infinities: positive infinity and negative infinity. The sign bit represents
the sign of infinity. The maximum exponent value 128 (the biased exponent 255) for the single-precision format and
zero significand represents infinity. The maximum biased value 255 can be represented in 8-bit with all bits set to
1
as
11111111
. The binary representations of infinities in single-precision format are shown in Table
3-9
.
Table 3-9.
Binary Representations of Positive and Negative Infinities in the Single-Precision Format
Number
Sign
Exponent
Significand
+Infinity
0
11111111
00000000000000000000000
-Infinity
1
11111111
00000000000000000000000
NaN
NaN
stands for “Not-a-Number.”
NaN
is the result of arithmetic operations that do not have meaningful results, such as
dividing zero by zero, the square root of a negative number, adding -Infinity to +Infinity, etc.
NaN
is represented by maximum exponent value (128 for single-precision format) and non-zero significand. The
sign bit is not interpreted for
NaN
. What happens when
NaN
is one of the operands in an arithmetic expression? For
example, what is the result of
NaN + 100
? Should the execution of an arithmetic expression involving
NaNs
be stopped
or continued? There are two types of
NaNs
:
NaN
•
Quiet
NaN
A quiet
NaN
, when encountered as an operand in an arithmetic expression, quietly (i.e. without raising any trap
or exception) produces another quiet
NaN
as the result. In case of a quiet
NaN
, the expression
NaN + 100
will result
in another quiet
NaN
. The most significant bit in the significand is set to 1 for a quiet
NaN
. Table
3-10
shows a binary
representation of a quiet
NaN
. In the table, s and b indicate a 0 or 1 bit.
•
Signaling
Table 3-10.
A Binary Representation of a Quiet NaN
Number
Sign
Exponent
Significand
Quiet NaN
s
11111111
1bbbbbbbbbbbbbbbbbbbbbb
When a signaling
NaN
is encountered as an operand in an arithmetic expression, an invalid operation exception
is signaled and a quiet
NaN
is delivered as the result. Signaling
NaNs
are generally used to initialize the uninitialized
variables in a program, so when variables are not initialized before they are used, errors can be signaled. The most
significant bit of the significand is set to 0 for a signaling
NaN
. Table
3-11
shows a binary representation of a signaling
NaN
. In the table, s and b indicate a 0 or 1 bit.