Skip to main content
Engineering LibreTexts

5.3: Representing floating-point numbers

  • Page ID
    40734
  • \( \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}}\)

    Floating-point numbers are represented using the binary version of scientific notation. In decimal notation, large numbers are written as the product of a coefficient and 10 raised to an exponent. For example, the speed of light in m/s is approximately \( 2.998 \cdot 10^8 \).

    Most computers use the IEEE standard for floating-point arithmetic. The C type float usually corresponds to the 32-bit IEEE standard; double usually corresponds to the 64-bit standard.

    In the 32-bit standard, the leftmost bit is the sign bit, \( s \). The next 8 bits are the exponent, \( q \), and the last 23 bits are the coefficient, \( c \). The value of a floating-point number is

    \[ (-1)^{s} \cdot 2^{q} \nonumber \]

    Well, that’s almost correct, but there’s one more wrinkle. Floating-point numbers are usually normalized so that there is one digit before the point. For example, in base 10, we prefer \( 2.998 \cdot 10^{8} \) rather than \( 2998 \cdot 10^{5} \)or any other equivalent expression. In base 2, a normalized number always has the digit 1 before the binary point. Since the digit in this location is always 1, we can save space by leaving it out of the representation.

    For example, the integer representation of \( 13_{10} \) is \( b1101 \). In floating point, that’s \( 1.101 \cdot 2^{3} \), so the exponent is 3 and the part of the coefficient that would be stored is 101 (followed by 20 zeros).

    Well, that’s almost correct, but there’s one more wrinkle. The exponent is stored with a “bias”. In the 32-bit standard, the bias is 127, so the exponent 3 would be stored as 130.

    To pack and unpack floating-point numbers in C, we can use a union and bitwise operations. Here’s an example:

        union {
            float f;
            unsigned int u;
        } p;
    
        p.f = -13.0;
        unsigned int sign = (p.u >> 31) & 1;
        unsigned int exp = (p.u >> 23) & 0xff;
    
        unsigned int coef_mask = (1 << 23) - 1;
        unsigned int coef = p.u & coef_mask;
    
        printf("%d\n", sign);
        printf("%d\n", exp);
        printf("0x%x\n", coef);
    

    This code is in float.c in the repository for this book (see Section 0.2).

    The union allows us to store a floating-point value using p.f and then read it as an unsigned integer using p.u.

    To get the sign bit, we shift the bits to the right 31 places and then use a 1-bit mask to select only the rightmost bit.

    To get the exponent, we shift the bits 23 places, then select the rightmost 8 bits (the hexadecimal value 0xff has eight 1’s).

    To get the coefficient, we need to extract the 23 rightmost bits and ignore the rest. We do that by making a mask with 1s in the 23 rightmost places and 0s on the left. The easiest way to do that is by shifting 1 to the left by 23 places and then subtracting 1.

    The output of this program is:

    1
    130
    0x500000
    

    As expected, the sign bit for a negative number is 1. The exponent is 130, including the bias. And the coefficient, which I printed in hexadecimal, is 101 followed by 20 zeros.

    As an exercise, try assembling or disassembling a double, which uses the 64-bit standard. See http://en.Wikipedia.org/wiki/IEEE_floating_point.


    This page titled 5.3: Representing floating-point numbers is shared under a CC BY-NC license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?