CS220 Lecture 12
CS220 Lecture 12
• Fixed-point positional number system is based on a positive integer radix (base) r and an
implicit digit set {0, 1, ··· ,r − 1}. Each unsigned integer is represented by a digit vector of
length k + l, with k digits for the whole part and l digits for the fractional part.
• Redundant signed-digit number systems: digit set[−α, β], with α+β ≥ r for radix r.
For example, once can use the digit set [−7, 7], for r = 10 . (3 -1 5) = (3 0 -5)ten =
(1 -7 0 -5)
• In radix r, with the standard digit set [0,r − 1], the number of digits needed
to represent the natural numbers in [0, max] is
• k==
• If a computation generates a number that cannot fit within these bits (that
is greater than max), an overflow is said to have occurred
• Four possibilities have been tried: sign magnitude, two’s complement, one’s
complement, biased
• Sign magnitude is the simplest: reserve the most significant bit to represent
the sign of the integer but has ambiguous representation of zero (00…0 and
100…0) and addition requires extra logic to set the result’s sign
Signed Magnitude Representation
• The biased representation is based on adding a positive value bias to all numbers,
allowing us to represent the integers from –bias to max – bias using unsigned values
from 0 to max
• Signed integers in the range [−8, +7] can be encoded as unsigned values 0 through 15
by using a bias of 8
x + y + bias = (x + bias) + (y + bias) − bias
x − y + bias = (x + bias) − (y + bias) + bias
Multiplication and division becomes very complicated. For this reason, the practical use
of biased representation is limited to floating-point numbers.
Complement Representation
• If finding M − x requires subtraction and finding residues mod M implies division, then
complement representation becomes quite inefficient.
add/sub
a3 b3 a2 b2 a1 b1 a0 b0
XOR XOR XOR XOR
S3 S2 S1 S0
Overflow in integer operations
• Overflow occurs if the result of a computation cannot fit within the given
number of bits
• In usual binary representation, if there is a carry-out in the MSB position of
an addition, overflow is said to occur
• In two’s complement representation, overflow occurs if and only if the
carry in to the MSB is different from carry out from the MSB
• Let the carry in to the MSB be cn-1 and carry out from the MSB be cn
when adding two n-bit numbers
• Suppose that we are adding A (an-1an-2…a0) and B (bn-1bn-2…b0) both in two’s
complement representation and let the sum be S (sn-1sn-2…s0)
• In IEEE 754, zero has the all-0s representation, with positive or negative sign.
• Special codes are also needed for representing ±∞ and NaN (not-anumber).
• The NaN special value is useful for representing undefined results such as 0/0.
• When one of these special values appears as an operand in an arithmetic
operation, the result of the operation is specified according to defined rules.
• Ordinary number / +∞= ±0
• +∞ x ordinary number= ±∞
• NaN + ordinary number= NaN
• The special codes thus allow exceptions to be propagated to the end of a
computation rather than bringing it to a halt.
• IEEE 754 single-precision format
– Special numbers
• Encoding of zero (two possible
representations):
X 00000000 00000000000000000000000
0 • 11111111
Encoding of +infinity:
00000000000000000000000
• Encoding of –infinity:
1 11111111 00000000000000000000000
• Encoding of NaN (result of 0/0, sqrt(-n), 0*inf,
X 11111111
etc.): Anything non-zero
IEEE 754 - Subnormals
• Subnormals, or subnormal values, are defined as numbers without a
hidden 1 and with the smallest possible exponent.
• certain small values that are not representable as normalized
numbers, hence must be rounded to 0 if encountered in the course
of computations, can be represented more precisely as subnormals.
• For example, (0.0001) × 2−126 is a subnormal that does not have a
normalized representation in the IEEE single format.
Average error=0.125
Rouding- rtne
Rounding: RTNE
Average error is 0
Requires more hardware
Rounding modes
• IEEE 754 rounding modes
• Round to nearest (default behavior)
• Round to nearest even for halfway rounding
• After rounding, the least significant representable mantissa bit should be
even (see following single-precision examples)
• Example: 1.1111…1 (has 24 1s in mantissa) is rounded to 2.0 (in
decimal) i.e., 1.0 x 21 in binary
• Example: 1.111…101 (has 22 1s followed by a 0 and a 1 in mantissa) is
rounded to 1.111…10 (has 22 1s followed by a 0 in mantissa)
• Round toward zero
• Round toward +infinity
• Round toward –infinity
• A computer can choose one of the modes
Other Rounding Methods