Exercises · Representation of numbers¶
Exercises · Chapter 1 · each solution opens with a click · Chapter 1: Representation of numbers · Exercises, volume 1 (PDF)
In the exercises of this chapter numbers are represented only with the definitions and observations of the chapter «Representation of numbers», summarized by the following formulas.
Natural numbers with \(k\) digits in base \(b\):
Integer numbers with \(k\) bits:
In two's complement, the representation of a negative \(n\) is also obtained by inverting the bits of \(|n|\) and adding \(1\); a sum overflows if and only if two operands with the same sign give a result with the opposite sign bit.
Real numbers in IEEE 754 single precision (\(1\) sign bit \(s\), \(8\) bits of biased exponent \(E\), \(23\) bits of fraction \(f\)), for \(1 \le E \le 254\):
Double precision: \(1 + 11 + 52\) bits, bias \(1023\).
Exercise 1.1: Representation of numbers (E1)
Consider the representation of natural numbers in the binary system (base \(b=2\)): representation intervals and minimum number of digits.
Questions
-
Compute the largest natural number that can be represented with \(k=6\) digits in binary and determine the associated representation interval.
-
For the following values of \(k\) (number of digits), determine the representation interval \([0, b^k-1]\) in base \(b=2\):
\[ k=4, \qquad k=8, \qquad k=10, \qquad k=16 \] -
Determine the minimum number of digits needed to represent each of the following numbers in binary, using the formula \(\lfloor \log_b n\rfloor +1\):
\[ n=64, \qquad n=128, \qquad n=256, \qquad n=512 \] -
Given \(k=12\) bits:
-
determine the representation interval;
-
verify that \(n=2{,}048\) can be represented with \(k=12\) bits;
-
compute the minimum number of bits needed to represent \(n=2{,}048\).
-
Solution
-
The largest natural number that can be represented with \(k=6\) digits in base \(b=2\) is:
\[ b^k - 1 = 2^6 - 1 = 64 - 1 = 63 \]This corresponds to the binary sequence \((111111)_2\). The associated representation interval is:
\[ [0, 2^6 - 1] = [0, 63] \] -
Representation intervals for the different values of \(k\):
\[\begin{align*} k=4: &\quad [0, 2^4-1] = [0, 16-1] = [0, 15]\\[1ex] k=8: &\quad [0, 2^8-1] = [0, 256-1] = [0, 255]\\[1ex] k=10: &\quad [0, 2^{10}-1] = [0, 1{,}024-1] = [0, 1{,}023]\\[1ex] k=16: &\quad [0, 2^{16}-1] = [0, 65{,}536-1] = [0, 65{,}535] \end{align*}\] -
The minimum number of digits for each number:
\[\begin{align*} n=64 = 2^6: &\quad \lfloor \log_2 64\rfloor +1 = \lfloor 6 \rfloor +1 = 6 + 1 = 7\\[1ex] n=128 = 2^7: &\quad \lfloor \log_2 128\rfloor +1 = \lfloor 7 \rfloor +1 = 7 + 1 = 8\\[1ex] n=256 = 2^8: &\quad \lfloor \log_2 256\rfloor +1 = \lfloor 8 \rfloor +1 = 8 + 1 = 9\\[1ex] n=512 = 2^9: &\quad \lfloor \log_2 512\rfloor +1 = \lfloor 9 \rfloor +1 = 9 + 1 = 10 \end{align*}\]For powers of \(2\), i.e., \(n=2^m\), we have \(\log_2 n = m\), and therefore the minimum number of bits is \(m+1\).
-
For \(k=12\) bits:
-
the representation interval is:
\[ [0, 2^{12}-1] = [0, 4{,}096-1] = [0, 4{,}095] \] -
since \(n=2{,}048 = 2^{11}\) and \(2{,}048 \le 4{,}095\), we have \(2{,}048 \in [0, 4{,}095]\): the number \(n=2{,}048\) can be represented with \(k=12\) bits;
-
the minimum number of bits needed to represent \(n=2{,}048 = 2^{11}\) is:
\[ \lfloor \log_2 2{,}048\rfloor +1 = \lfloor 11 \rfloor +1 = 12 \]
We need exactly \(12\) bits to represent \(2{,}048 = 2^{11}\), because its binary representation is one digit \(1\) followed by eleven digits \(0\):
\[ 2{,}048 = (100000000000)_2 \] -
Exercise 1.2: Representation of numbers (E2)
Consider the representation intervals and the minimum number of digits of natural numbers in the binary system (base \(b=2\)).
Questions
-
Complete the following table of representation intervals in binary:
\(k\) (digits) \(b\) (base) \(b^k-1\) representation interval 5 2 8 2 10 2 12 2 -
For each natural number \(n\) in the table below, compute the minimum number of digits needed to represent it in base \(b=2\), using the formula \(\lfloor \log_b n\rfloor +1\):
\(n\) \(b\) \(\log_b n\) \(\lfloor \log_b n\rfloor +1\) 7 2 15 2 31 2 100 2 255 2 -
Using the relationship between bytes and bits, complete the table:
bytes bits (\(k\)) \(2^k\) representation interval 1 8 \(2^8=256\) \([0,255]\) 2 4 -
Convert \((11011011)_2\) to base ten, writing the complete sum \(\sum_{i=0}^{k-1} d_i \cdot 2^i\).
Solution
-
Representation intervals in binary:
\(k\) (digits) \(b\) (base) \(b^k-1\) representation interval 5 2 \(2^5-1=31\) \([0,31]\) 8 2 \(2^8-1=255\) \([0,255]\) 10 2 \(2^{10}-1=1{,}023\) \([0,1{,}023]\) 12 2 \(2^{12}-1=4{,}095\) \([0,4{,}095]\) -
Minimum number of digits for each natural number:
\(n\) \(b\) \(\log_b n\) \(\lfloor \log_b n\rfloor +1\) 7 2 \(\log_2 7 \approx 2.807\) \(\lfloor 2.807 \rfloor +1 = 3\) 15 2 \(\log_2 15 \approx 3.907\) \(\lfloor 3.907 \rfloor +1 = 4\) 31 2 \(\log_2 31 \approx 4.954\) \(\lfloor 4.954 \rfloor +1 = 5\) 100 2 \(\log_2 100 \approx 6.644\) \(\lfloor 6.644 \rfloor +1 = 7\) 255 2 \(\log_2 255 \approx 7.994\) \(\lfloor 7.994 \rfloor +1 = 8\) For the numbers of the form \(2^k - 1\) we need exactly \(k\) bits: \(2^k-1\) is the largest natural number with \(k\) bits.
-
Since \(1\) byte \(= 8\) bits:
bytes bits (\(k\)) \(2^k\) representation interval 1 8 \(2^8=256\) \([0,255]\) 2 16 \(2^{16}=65{,}536\) \([0,65{,}535]\) 4 32 \(2^{32}=4{,}294{,}967{,}296\) \([0,4{,}294{,}967{,}295]\) -
The sequence has \(k=8\) digits: \(d_7=1\), \(d_6=1\), \(d_5=0\), \(d_4=1\), \(d_3=1\), \(d_2=0\), \(d_1=1\), \(d_0=1\). Hence:
\[\begin{align*} (11011011)_2 &= \sum_{i=0}^{7} d_i \cdot 2^i \\ &= d_0 \cdot 2^0 + d_1 \cdot 2^1 + d_2 \cdot 2^2 + d_3 \cdot 2^3 + d_4 \cdot 2^4 + d_5 \cdot 2^5 + d_6 \cdot 2^6 + d_7 \cdot 2^7 \\ &= 1 \cdot 1 + 1 \cdot 2 + 0 \cdot 4 + 1 \cdot 8 + 1 \cdot 16 + 0 \cdot 32 + 1 \cdot 64 + 1 \cdot 128 \\ &= 1 + 2 + 0 + 8 + 16 + 0 + 64 + 128 \\ &= 219 \end{align*}\]Therefore, \((11011011)_2 = (219)_{10}\).
Exercise 1.3: Representation of numbers (E3)
Consider the complete enumeration of the sequences of digits in binary for a given number of bits, and the binary representation of some natural numbers.
Questions
-
Complete the table of all the sequences of \(k=5\) digits in binary (base \(2\)) and their decimal values. Show the detailed calculation for at least the first three and the last three sequences:
Sequence of digits \(b\) \(k\) decimal representation \((00000)_2\) 2 5 \((00001)_2\) 2 5 \((00010)_2\) 2 5 \((00011)_2\) 2 5 \(\vdots\) \(\vdots\) \(\vdots\) \(\vdots\) \((11101)_2\) 2 5 \((11110)_2\) 2 5 \((11111)_2\) 2 5 -
How many different sequences are possible with \(k=5\) bits? Verify the answer with the formula \(b^k\), where \(b=2\).
-
Find the binary representation of the following decimal numbers, by determining which powers of \(2\) sum to the given value:
\[ n=21, \qquad n=50, \qquad n=63 \] -
For each sequence below, determine whether it represents a power of \(2\) and, if so, which power:
\[ (10000)_2, \qquad (10101)_2, \qquad (100000000)_2 \]
Solution
-
All the sequences with \(k=5\) bits:
Sequence of digits \(b\) \(k\) decimal representation \((00000)_2\) 2 5 \((0 \cdot 2^4 + 0 \cdot 2^3 + 0 \cdot 2^2 + 0 \cdot 2^1 + 0 \cdot 2^0)_{10} = (0)_{10}\) \((00001)_2\) 2 5 \((0 \cdot 2^4 + 0 \cdot 2^3 + 0 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0)_{10} = (1)_{10}\) \((00010)_2\) 2 5 \((0 \cdot 2^4 + 0 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 + 0 \cdot 2^0)_{10} = (2)_{10}\) \((00011)_2\) 2 5 \((3)_{10}\) \((00100)_2\) 2 5 \((4)_{10}\) \((00101)_2\) 2 5 \((5)_{10}\) \((00110)_2\) 2 5 \((6)_{10}\) \((00111)_2\) 2 5 \((7)_{10}\) \((01000)_2\) 2 5 \((8)_{10}\) \((01001)_2\) 2 5 \((9)_{10}\) \(\vdots\) \(\vdots\) \(\vdots\) \(\vdots\) \((11101)_2\) 2 5 \((1 \cdot 2^4 + 1 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0)_{10} = (29)_{10}\) \((11110)_2\) 2 5 \((1 \cdot 2^4 + 1 \cdot 2^3 + 1 \cdot 2^2 + 1 \cdot 2^1 + 0 \cdot 2^0)_{10} = (30)_{10}\) \((11111)_2\) 2 5 \((1 \cdot 2^4 + 1 \cdot 2^3 + 1 \cdot 2^2 + 1 \cdot 2^1 + 1 \cdot 2^0)_{10} = (31)_{10}\) -
The total number of different sequences with \(k=5\) bits in base \(b=2\) is:
\[ b^k = 2^5 = 32 \]Indeed, the sequences go from \((00000)_2 = 0\) to \((11111)_2 = 31\), which gives exactly \(32\) different values (from \(0\) to \(31\) inclusive).
-
Binary representation of the decimal numbers:
-
\(n=21\): we look for the powers of \(2\) that sum to \(21\), starting from the largest power not greater than \(21\):
\[ 21 = 16 + 5 = 16 + 4 + 1 = 2^4 + 2^2 + 2^0 \]Therefore \((21)_{10} = (10101)_2\).
-
\(n=50\):
\[ 50 = 32 + 18 = 32 + 16 + 2 = 2^5 + 2^4 + 2^1 \]Therefore \((50)_{10} = (110010)_2\).
-
\(n=63\):
\[ 63 = 32 + 31 = 32 + 16 + 8 + 4 + 2 + 1 = 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 \]Alternatively, \(63 = 2^6 - 1\) is the largest natural number with \(6\) bits, so all its \(6\) bits are \(1\). Therefore \((63)_{10} = (111111)_2\).
-
-
Powers of \(2\):
-
\((10000)_2 = 1 \cdot 2^4 + 0 \cdot 2^3 + 0 \cdot 2^2 + 0 \cdot 2^1 + 0 \cdot 2^0 = 2^4 = 16\): it is a power of \(2\), namely \(2^4\).
-
\((10101)_2 = 1 \cdot 2^4 + 0 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 16 + 4 + 1 = 21\): it is not a power of \(2\) (it is the sum of three different powers of \(2\)).
-
\((100000000)_2 = 1 \cdot 2^8 = 256\): it is a power of \(2\), namely \(2^8\).
A sequence in binary represents a power of \(2\) if and only if it has exactly one digit equal to \(1\) and all the other digits equal to \(0\).
-
Exercise 1.4: Representation of numbers (E4)
Consider the two's complement representation of integer numbers in binary.
Questions
-
Determine the representation interval of the integers in two's complement with the following numbers of bits:
\[ k=4, \qquad k=8, \qquad k=16, \qquad k=32 \] -
Complete the following table with all the two's complement representations with \(k=4\) bits:
Binary Decimal Sign bit \((0000)_2\) \((0001)_2\) \((0010)_2\) \((0011)_2\) \((0100)_2\) \((0101)_2\) \((0110)_2\) \((0111)_2\) \((1000)_2\) \((1001)_2\) \((1010)_2\) \((1011)_2\) \((1100)_2\) \((1101)_2\) \((1110)_2\) \((1111)_2\) -
Compute the two's complement representation of \(-5\) with \(8\) bits in three steps: (1) binary representation of \(5\), (2) inversion of all the bits, (3) addition of \(1\).
-
Verify that with \(k=5\) bits in two's complement we can represent the integer \(-16\) but not the integer \(16\).
Solution
-
Representation intervals, with the formula \([-2^{k-1}, 2^{k-1}-1]\):
\[\begin{align*} k=4: &\quad [-2^{4-1}, 2^{4-1}-1] = [-2^3, 2^3-1] = [-8, 7]\\[1ex] k=8: &\quad [-2^{8-1}, 2^{8-1}-1] = [-2^7, 2^7-1] = [-128, 127]\\[1ex] k=16: &\quad [-2^{16-1}, 2^{16-1}-1] = [-2^{15}, 2^{15}-1] = [-32{,}768,\, 32{,}767]\\[1ex] k=32: &\quad [-2^{32-1}, 2^{32-1}-1] = [-2^{31}, 2^{31}-1] = [-2{,}147{,}483{,}648,\, 2{,}147{,}483{,}647] \end{align*}\] -
All the two's complement representations with \(k=4\) bits: a sequence with sign bit \(1\) and natural value \(r\) represents \(r - 16\).
Binary Decimal Sign bit \((0000)_2\) \(0\) \(0\) (non-negative) \((0001)_2\) \(1\) \(0\) (non-negative) \((0010)_2\) \(2\) \(0\) (non-negative) \((0011)_2\) \(3\) \(0\) (non-negative) \((0100)_2\) \(4\) \(0\) (non-negative) \((0101)_2\) \(5\) \(0\) (non-negative) \((0110)_2\) \(6\) \(0\) (non-negative) \((0111)_2\) \(7\) \(0\) (non-negative) \((1000)_2\) \(8-16=-8\) \(1\) (negative) \((1001)_2\) \(9-16=-7\) \(1\) (negative) \((1010)_2\) \(10-16=-6\) \(1\) (negative) \((1011)_2\) \(11-16=-5\) \(1\) (negative) \((1100)_2\) \(12-16=-4\) \(1\) (negative) \((1101)_2\) \(13-16=-3\) \(1\) (negative) \((1110)_2\) \(14-16=-2\) \(1\) (negative) \((1111)_2\) \(15-16=-1\) \(1\) (negative) The representation interval with \(4\) bits is \([-8, 7]\).
-
Two's complement representation of \(-5\) with \(8\) bits:
-
Step 1: binary representation of \(5\) with \(8\) bits: \((00000101)_2\).
-
Step 2: inversion of all the bits: \((11111010)_2\).
-
Step 3: addition of \(1\) (the first row contains the carries; the carry above a column comes from the column on its right):
carry 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 1 sum 1 1 1 1 1 0 1 1
Therefore, the two's complement representation of \(-5\) with \(8\) bits is \((11111011)_2\). Check:
\[ 2^8 - 5 = 256 - 5 = 251 = (11111011)_2 \] -
-
With \(k=5\) bits the representation interval is:
\[ [-2^{5-1}, 2^{5-1}-1] = [-2^4, 2^4-1] = [-16, 15] \]-
Since \(-16 \in [-16, 15]\), the integer \(-16\) can be represented, and its representation is:
\[ 2^5 - 16 = 32 - 16 = 16 = (10000)_2 \] -
Since \(16 > 15\), we have \(16 \notin [-16, 15]\): the integer \(16\) cannot be represented with \(5\) bits in two's complement.
Two's complement represents one more negative integer than positive integers, because the interval \([-2^{k-1}, 2^{k-1}-1]\) is asymmetric.
-
Exercise 1.5: Representation of numbers (E5)
Consider the addition of integer numbers in two's complement, the detection of overflows, and the comparison between sign-magnitude and two's complement.
Questions
-
Perform the following additions in two's complement with \(k=5\) bits, showing the binary representations and the column addition, and verify the results:
\[ (-7) + 3, \qquad (-8) + 8, \qquad 5 + 6 \] -
Determine whether an overflow occurs in the following additions with \(k=5\) bits, and explain why:
\[ 7 + 7, \qquad (-8) + (-8), \qquad 7 + (-8), \qquad 9 + 8 \] -
Compare the sign-magnitude and the two's complement representations with \(k=4\) bits:
-
what is the representation interval of each?
-
how many representations does zero have in each system?
-
which system is better for arithmetic operations, and why?
-
-
With \(k=8\) bits in two's complement, compute the representations of \(-64\) and \(-128\), using the formula \(2^k + n\) for the negative integers \(n\).
Solution
In the column additions, the first row contains the carries: the carry written above a column comes from the column on its right, and the leftmost column receives the carry out of the most significant position, which is discarded.
-
With \(k=5\) bits the representation interval is \([-16, 15]\).
-
\((-7) + 3 = -4\). Representations: \(-7 \rightarrow 2^5 - 7 = 25 = (11001)_2\) and \(3 = (00011)_2\).
carry 0 0 0 1 1 1 1 0 0 1 0 0 0 1 1 sum 0 1 1 1 0 0 The result \((11100)_2\) has natural value \(28\), hence it represents \(28 - 32 = -4\).
-
\((-8) + 8 = 0\). Representations: \(-8 \rightarrow 2^5 - 8 = 24 = (11000)_2\) and \(8 = (01000)_2\).
carry 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 sum 1 0 0 0 0 0 The result is \((00000)_2 = 0\).
-
\(5 + 6 = 11\). Representations: \(5 = (00101)_2\) and \(6 = (00110)_2\).
carry 0 0 1 0 0 0 0 1 0 1 0 0 1 1 0 sum 0 0 1 0 1 1 The result is \((01011)_2 = 11\).
-
-
Overflow detection with \(k=5\) bits (interval \([-16, 15]\)):
-
\(7 + 7\) (expected \(14\)). Representations: \(7 = (00111)_2\).
carry 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 sum 0 0 1 1 1 0 Result: \((01110)_2 = 14\): both operands are positive and the result is positive: no overflow (\(14 \le 15\)).
-
\((-8) + (-8)\) (expected \(-16\)). Representations: \(-8 \rightarrow 2^5 - 8 = 24 = (11000)_2\).
carry 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 sum 1 1 0 0 0 0 Result: \((10000)_2 = -16\): both operands are negative and the result is negative: no overflow (\(-16 \ge -16\)). The carry out is discarded.
-
\(7 + (-8)\) (expected \(-1\)). Representations: \(7 = (00111)_2\) and \(-8 \rightarrow 2^5 - 8 = 24 = (11000)_2\).
carry 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 sum 0 1 1 1 1 1 Result: \((11111)_2 = -1\): the operands have different signs, so an overflow cannot occur.
-
\(9 + 8\) (expected \(17\)). Representations: \(9 = (01001)_2\) and \(8 = (01000)_2\).
carry 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 sum 0 1 0 0 0 1 Result: \((10001)_2 = -15\): both operands are positive, but the result has sign bit \(1\): overflow. Indeed \(9 + 8 = 17 > 15\) cannot be represented with \(5\) bits.
Rule: an overflow occurs only when two integers with the same sign are added and the result has the opposite sign bit.
-
-
Comparison of sign-magnitude and two's complement with \(k=4\) bits:
-
representation intervals:
\[\begin{align*} \text{sign-magnitude:} &\quad [-(2^{4-1}-1),\, 2^{4-1}-1] = [-7, 7]\\[1ex] \text{two's complement:} &\quad [-2^{4-1},\, 2^{4-1}-1] = [-8, 7] \end{align*}\]two's complement represents one more integer, \(-8\);
-
sign-magnitude has two representations of zero, \((0000)_2 = +0\) and \((1000)_2 = -0\); two's complement has only one, \((0000)_2 = 0\);
-
two's complement is better for arithmetic operations, because:
-
zero has a unique representation;
-
addition and subtraction are performed as for natural numbers (modulo \(2^k\)), with the same circuits used for unsigned numbers;
-
the sign bit needs no special handling, so the circuits are simpler and more efficient.
-
-
-
Representations with \(k=8\) bits:
\[\begin{align*} -64: &\quad 2^k + n = 2^8 + (-64) = 256 - 64 = 192 = (11000000)_2\\[1ex] -128: &\quad 2^k + n = 2^8 + (-128) = 256 - 128 = 128 = (10000000)_2 \end{align*}\]The integer \(-128 = -2^{8-1}\) is the smallest integer representable with \(8\) bits in two's complement.
Exercise 1.6: Representation of numbers (E6)
Consider the representation of real numbers in floating point, according to the IEEE 754 standard.
Questions
-
Write the real number \(13.25\) in binary and in normalized binary scientific notation, and give its \(32\) bits in IEEE 754 single precision.
-
Determine the real number represented by the following \(32\) bits in IEEE 754 single precision:
\[ 0~~01111100~~01000000000000000000000 \] -
Using the double precision format (\(1\) bit of sign, \(11\) bits of exponent with bias \(1023\), \(52\) bits of fraction), compute the largest finite number and the smallest positive normalized number. The values \(E = 0\) and \(E = 2^{11}-1 = 2{,}047\) of the biased exponent are reserved.
-
In double precision, adding ten times the number \(0.1\) to a variable initialized to \(0\) gives \(0.99999999999999989\) and not \(1\). Explain why.
Solution
-
Representation of \(13.25\):
-
Step 1 (binary): \(13.25 = 8 + 4 + 1 + 0.25 = 1 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 + 0 \cdot 2^{-1} + 1 \cdot 2^{-2}\), hence:
\[ 13.25 = (1101.01)_2 \] -
Step 2 (normalization): the point moves three positions to the left:
\[ (1101.01)_2 = (1.10101)_2 \cdot 2^3 \]hence \(s = 0\), \(e = 3\) and the fraction starts with \(10101\).
-
Step 3 (biased exponent): \(E = e + 127 = 130 = 128 + 2 = (10000010)_2\).
-
Step 4 (fraction): \(10101\) completed with zeros up to \(23\) bits.
The \(32\) bits are:
\[ \underbrace{0}_{s}~~\underbrace{10000010}_{E}~~\underbrace{10101000000000000000000}_{f} \] -
-
Reading the \(32\) bits:
-
Step 1 (sign): \(s = 0\), the number is positive.
-
Step 2 (exponent): \(E = (01111100)_2 = 64 + 32 + 16 + 8 + 4 = 124\), hence \(e = E - 127 = -3\).
-
Step 3 (significand): \((1.01)_2 = 1 + 0.25 = 1.25\).
-
Step 4 (value):
\[ x = (-1)^0 \cdot 1.25 \cdot 2^{-3} = \frac{1.25}{8} = 0.15625 \]
-
-
In double precision a normalized number is \((-1)^s \cdot (1.f_1 f_2 \dots f_{52})_2 \cdot 2^{E - 1023}\) with \(1 \le E \le 2{,}046\).
-
The largest finite number has \(E = 2{,}046\), i.e., \(e = 1{,}023\), and all the \(52\) fraction bits equal to \(1\), i.e., significand \(\sum_{i=0}^{52} 2^{-i} = 2 - 2^{-52}\):
\[ (2 - 2^{-52}) \cdot 2^{1023} \approx 1.7976931349 \times 10^{308} \] -
The smallest positive normalized number has \(E = 1\), i.e., \(e = -1{,}022\), and all the fraction bits equal to \(0\):
\[ 1 \cdot 2^{-1022} \approx 2.2250738585 \times 10^{-308} \]
-
-
The decimal number \(0.1\) has an infinite binary representation:
\[ 0.1 = (0.0\,0011\,0011\,0011\dots)_2 = (1.1001\,1001\,1001\dots)_2 \cdot 2^{-4} \]so its fraction does not fit in \(52\) bits and is rounded. The number actually stored is slightly different from \(0.1\):
\[ 0.1000000000000000055511151231\dots \]Moreover, the result of each addition is rounded again to the nearest number representable in double precision. The ten rounding errors accumulate, and the final result is not \(1\) but:
\[ 0.99999999999999989 \]For this reason, two floating-point numbers should not be compared with the equality operator without care.