Excess-k Encoding

Excess-k Encoding
Excess-k encoding is another signed integer encoding that is
important due to its use in representing floating point exponents.
• In excess-k (or “bias-k”) encoding, integer i is represented by
the unsigned encoding of i + k. For example, in excess-127:
3
−5
⇒
⇒
3 + 127 = 130
−5 + 127 = 122
=
=
10000010
01111010
• The advantage is that ordering is preserved by the encoding.
Comparison of Integer Encodings
Note: 1st column should be labeled "N Signed Magnitude", not "N binary"
Floating Point Encodings
Fixed-precision integer encodings have advantages and disadvantages:
Advantage
• Results are exact, as long as there is no overflow.
Disadvantages
• Limited “dynamic range”: can’t represent very large numbers
(e.g. 270) or very small numbers (e.g. 2−17)
• Can’t directly represent fractional values (e.g. amounts of
money would be represented in pennies, rather than dollars).
Floating point encodings provide complementary features.
IEEE 754 Floating Point Standard
The IEEE 754 standard specifies floating point representations
of numbers and arithmetic operations on these representations.
• Uses a kind of scientific notation:
±2exponent × fraction
• Can represent with three fields: sign bit (s), exponent (e),
and fraction (or mantissa) (f ).
• IEEE 754 defines several precisions, including single precision
(32 bits) and double precision (64 bits):
IEEE 754: Details
Sign Bit (s)
• Signed-magnitude representation
• 0 for positive, 1 for negative
Exponent (e)
• Exponent (base 2).
• Stored using “excess-k” encoding, where k = 127 for single
precision and k = 1023 in double precision.
• Permits floating point values to be compared as if they were
signed integers.
Fraction (f )
• Contains the digits to the right of the binary point.
• Normalized: The digit to the left of the point is always 1,
and is not represented.
• Gives us one bit of precision “for free”.
IEEE 754 to Decimal
Decimal value of a IEEE 754 floating point encoding is given by
the formula:
(−1)s × 2e−bias × (1 + f )
• s is the sign bit (0/1).
• e is the decimal value of the exponent field.
• bias is 127 for single-precision, 1023 for double-precision.
• f is the decimal value of the fraction field (regarded as a
binary fraction).
Example
What decimal value has the following IEEE 754 encoding?
10111110011000000000000000000000
• s = 1.
• e = (64 + 32 + 16 + 8 + 4) − 127 = −3.
• f = 0.5 + 0.25 = 0.75
Answer
−1.75 × 2−3 = −0.2187510.
Special Values
The smallest 000 . . . 0 and largest 111 . . . 1 exponents are reserved
for the encoding of special values:
• Zero (two encodings):
– +0.0: s = 0, e = 000 . . . 0, f = 000 . . . 0.
– −0.0: s = 1, e = 000 . . . 0, f = 000 . . . 0.
• Infinity (two encodings):
– +∞: s = 0, e = 111 . . . 1, f = 000 . . . 0.
– −∞: s = 1, e = 111 . . . 1, f = 000 . . . 0.
• NaN (not a number, multiple encodings):
– s =?, e = 111 . . . 1, f =??? . . .?.
– Can result from 0.0/0.0 and similar invalid operations.
Floating Point: Summary
Item
Bits in sign
Bits in exponent
Bits in fraction
Bits total
Exponent encoding
Exponent range
Decimal range
Single precision
1
8
23
32
excess 127
−126 to +127
approx. 10−38 to 1038
Double precision
1
11
52
64
excess 1023
−1022 to +1023
approx. 10−308 to 10308
Floating Point: Limitations
There are 232 different values that can be represented in singleprecision floating point.
• This is the same as the number of values that can be represented using 32-bit integer encodings.
• Many values (even integers) do not have floating-point representations:
– Example: 3355443110
– Example: 0.3355443110
Try assigning these to a float variable in Java and then
printing them out.
Caution: Results of floating point calculations are not exact.
Never use floating point when exact results are essential.