3.3: Floating-point Representation
- Page ID
- 19871
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The representation issues for floating-point numbers are more complex. There are a series of floating-point representations for various ranges of the value. For simplicity, we will look primarily at the IEEE 754 32-bit floating-point standard.
Representation
The IEEE 754 32-bit floating-point standard is defined as follows:
Where s is the sign (0 => positive and 1 => negative). More formally, this can be written as;
\[N = (-1)^{s} \times 1.F \times 2^{E - 127} \nonumber\]
When representing floating-point values, the first step is to convert floating-point value into binary. The following table provides a brief reminder of how binary handles fractional components:
\(2^3\) | \(2^2\) | \(2^1\) | \(2^0\) | \(2^{-1}\) | \(2^{-2}\) | \(2^{-3}\) | |||
... | 8 | 4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 | ... |
0 | 0 | 0 | 0 | . | 0 | 0 | 0 |
For example, \(100.101_2\) would be \(4.625_{10}\). For repeating decimals, calculating the binary value can be time consuming. However, there is a limit since computers have finite storage sizes (32-bits in this example).
The next step is to show the value in normalized scientific notation in binary. This means that the number should have a single, non-zero leading digit to the left of the decimal point. For example, \(8.125_{10}\) is \(1000.001_2\) (or 1000.0012 x 20) and in binary normalized scientific notation that would be written as \(1.000001 \times 2^3\) (since the decimal point was moved three places to the left). Of course, if the number was \(0.125_{10}\) the binary would be \(0.001_2\) (or \(0.001_2 \times 2^0\)) and the normalized scientific notation would be \(1.0 \times 2^{-3}\) (since the decimal point was moved three places to the right). The numbers after the leading 1, not including the leading 1, are stored left-justified in the fraction portion of the double-word.
The next step is to calculate the biased exponent, which is the exponent from the normalized scientific notation plus the bias. The bias for the IEEE 754 32-bit floating- point standard is \(127_{10}\). The result should be converted to a byte (8-bits) and stored in the biased exponent portion of the word.
Note, converting from the IEEE 754 32-bit floating-point representation to the decimal value is done in reverse, however leading 1 must be added back (as it is not stored in the word). Additionally, the bias is subtracted (instead of added).
32-bit Representation Examples
This section presents several examples of encoding and decoding floating-point representation for reference.
3.3.1.1.1 Example \(\to -7.75_{10}\)
For example, to find the IEEE 754 32-bit floating-point representation for \(-7.75_{10}\):
Example \(\PageIndex{1}\) -7.75
- determine sign -7.75 => 1 (since negative)
- convert to binary -7.75 = \( -0111.11_2\)
- normalized scientific notation = \(1.1111 \times 2^2\)
- compute biased exponent \(2_{10} + 127_{10} = 129_{10}\)
◦ and convert to binary =\(10000001_2\)
- write components in binary:
sign exponent mantissa
1 10000001 11110000000000000000000 - convert to hex (split into groups of 4)
11000000111110000000000000000000
1100 0000 1111 1000 0000 0000 0000 0000
C 0 F 8 0 0 0 0 - final result: \(C0F8\ 0000_{16}\)
3.3.1.1.2 Example \(\to -0.125_{10}\)
For example, to find the IEEE 754 32-bit floating-point representation for \(-0.125_{10}\):
Example \(\PageIndex{2}\) -0.125
- determine sign -0.125 => 1 (since negative)
- convert to binary -0.125 = \( -0.001_2\)
- normalized scientific notation = \(1.0 \times 2^{-3}\)
- compute biased exponent \(-3_{10} + 127_{10} = 124_{10}\)
◦ and convert to binary =\(01111100_2\)
- write components in binary:
sign exponent mantissa
1 01111100 00000000000000000000000 - convert to hex (split into groups of 4)
10111110000000000000000000000000
1011 1110 0000 0000 0000 0000 0000 0000
B E 0 0 0 0 0 0 - final result: \(BE00\ 0000_{16}\)
3.3.1.1.3 Example \(\to 41440000_{16}\)
For example, given the IEEE 754 32-bit floating-point representation \(41440000_{16}\) find the decimal value:
Example \(\PageIndex{3}\) \(41440000_{16}\)
- convert to binary
0100 0001 0100 0100 0000 0000 0000 00002 - split into components
0 10000010 100010000000000000000002
- determine exponent \(10000010_2 = 130_{10}\)
◦ and remove bias \(130_{10} - 127_{10} = 3_{10}\) - determine sign 0 => positive
- write result \(+1.10001 \times 2^3 = +1100.01 = +12.25\)
Representation
The IEEE 754 64-bit floating-point standard is defined as follows:
The representation process is the same, however the format allows for an 11-bit biased exponent (which support large and smaller values). The 11-bit biased exponent uses a bias of \(\pm 1023\).
Number (NaN)
When a value is interpreted as a floating-point value and it does not conform to the defined standard (either for 32-bit or 64-bit), then it cannot be used as a floating-point value. This might occur if an integer representation is treated as a floating-point representation or a floating-point arithmetic operation (add, subtract, multiply, or divide) results in a value that is too large or too small to be represented. The incorrect format or unrepresentable number is referred to as a NaN which is an abbreviation for not a number.