(Thanks to
Ned Batchelder
for inspiration on some of these.)
Bits
Sign
=
=
Exponent
=
=
Fraction
=
=
Value
(-1)
× (1 +
/2
52) × 2 - 1023
= ×
2
Since the exponent is ,
the sign is , and the
fraction is 0, this represents the
floating point number
0.
Since the sign is ,
the exponent is 0x7ff =
2047, and the fraction is
0, this represents the floating point
number
∞.
Since the exponent is 0x7ff =
2047, and the fraction is non-zero,
this represents the floating point number
NaN (not a number).
Text and hex representation
On the left is the text representation the floating-point number as
typically presented to a user. The letter e does not
indicate the familiar constant approximately equal to
2.718281828459045, but is
short for exponent, and is part of
an old-school way of showing scientific notation without
superscripts. For example, both 1.234e30 and
1.234e+30 represent the value 1.234 ×
1030, while 1.234e-30
represents the value 1.234 × 10-30.
On the right is the hexadecimal (base-16) representation of the
floating-point number as stored in memory. This is explained in the
next help topic, Bits.
Bits
This shows how the floating-point number is represented in the
computer, in binary. (The hexadecimal representation is given above.)
There are three parts: the sign (1
bit), the exponent (11 bits), and the
fraction (52 bits).
Sign
This one bit represents the sign of the floating-point number. It the
bit is zero, the number is positive. If the bit is one, the number is
negative.
Exponent
These 11 bits give the exponent of the floating-point number, after
1023 is added.
For example, if 1025 is stored here, then the true exponent is 2. On
the other hand, if 1000 is stored here, then the true exponent is -23.
If the exponent is 0 and the fraction part is 0, then the floating
point number is ±0 (the sign being determined by the sign bit).
If the exponent is 0x7ff = 2047 and the
fraction part is 0, then the floating point number is ±∞ (the
sign being determined by the sign bit).
If the exponent is 0x7ff = 2047 and the
fraction part is nonzero, then the floating point number is
NaN (not a number).
Fraction
In scientific notation, non-zero numbers are written in decimal in the
form
where
and
for
Likewise, the idea behind floating-point numbers is to represent most
numbers (all except for ±0, ±∞, and NaN) in a binary
analogue to scientific notation,
where
and
for
(Here, the value
is written in binary.) Since
always, we don't bother storing it. The 52 binary digits
are the fraction bits for the floating-point number.
Value
Except in the cases of ±0, ±∞, or NaN, a floating-point
number is represented as
If we set
and
then
The values
, , and
are the sign, exponent, and fraction part of the float, respectively.