How floating-point no is stored in memory?
Tags: c, tutorial, cpp, computerscience
Published: 2019-10-13 13:05:13

This is cross-post from my [blog](http://www.vishalchovatiya.com/how-floating-point-no-is-stored-memory/).

<!-- wp:paragraph -->
<p>This article is just a simplification of the IEEE 754 standard. Here, we will see how floating-point no stored in memory, floating-point exceptions/rounding, etc.&nbsp;But if you will want to find more authoritative sources then go for</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" target="_blank"><span style="color:#3919d8" class="color">What Every Computer Scientist Should Know About Floating-Point Arithmetic</span></a></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/IEEE_754-1985" target="_blank"><span style="color:#3919d8" class="color">https://en.wikipedia.org/wiki/IEEE_754-1985</span></a></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Floating_point" target="_blank"><span style="color:#3919d8" class="color">https://en.wikipedia.org/wiki/Floating_point</span></a><span style="color:#3919d8" class="color">.</span></li></ol>
<!-- /wp:list -->

>Floating-point numbers stored by encoding significand &amp; the exponent (along with a sign bit)

<!-- wp:list -->
<ul><li>Above line contains 2-3 abstract terms &amp; I think you will unable to understand the above line until you read further. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Floating point number memory layout</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">+-+--------+-----------------------+
| |        |                       |
+-+--------+-----------------------+
 ^    ^                ^
 |    |                |
 |    |                +-- significand(width- 23 bit) 
 |    |
 |    +------------------- exponent(width- 8 bit) 
 |
 +------------------------ sign bit(width- 1 bit)</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>A typical single-precision 32-bit floating-point memory layout has the following fields :</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>sign</li><li>exponent</li><li>significand(AKA mantissa)</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Sign</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The high-order bit indicates a sign.</li><li><code>0</code> indicates a positive value, <code>1</code> indicates negative.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Exponent</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The next 8 bits are used for the exponent which can be positive or negative, but instead of reserving another sign bit, they're encoded such that <code>1000 0000</code> represents <code>0</code>, so <code>0000 0000</code> represents <code>-128</code> and <code>1111 1111</code> represents <code>127</code>.</li><li>How does this encoding work? go to <a rel="noreferrer noopener" aria-label="exponent bias (opens in a new tab)" href="https://en.wikipedia.org/wiki/Exponent_bias" target="_blank"><span style="color:#1f15de" class="color">exponent bias</span></a> or see&nbsp;it in next point&nbsp;practically.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Significand</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The remaining 23-bits used for the significand(AKA mantissa). Each bit represents a negative power of 2 countings from the left, so:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">01101 = 0 * 2^-1 + 1 * 2^-2 + 1 * 2^-3 + 0 * 2^-4 + 1 * 2^-5 
      = 0.25 + 0.125 + 0.03125 
      = 0.40625</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>OK! We are done with basics. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Let's understand practically</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>So, we consider very famous float value <code>3.14</code>(PI) example.</li><li><strong>Sign</strong>: Zero here, as PI is positive!</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Exponent calculation</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>3</code> is easy: <code>0011</code> in binary</li><li>The rest, <code>0.14</code></li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0.14 x 2 = 0.28, 0

0.28 x 2 = 0.56, 00

0.56 x 2 = 1.12, 001

0.12 x 2 = 0.24, 0010

0.24 x 2 = 0.48, 00100

0.48 x 2 = 0.96, 001000

0.96 x 2 = 1.92, 0010001

0.92 x 2 = 1.84, 00100011

0.84 x 2 = 1.68, 001000111

And so on . . .</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>So,<code> 0.14 = 001000111...</code>If you don't know how to convert decimal no in binary then refer this <a rel="noreferrer noopener" aria-label="float to binary (opens in a new tab)" href="http://stackoverflow.com/questions/3954498/how-to-convert-float-number-to-binary" target="_blank"><span style="color:#1b44fb" class="color">float to binary</span></a>.</li><li>Add <code>3</code>, <code>&nbsp;11.001000111... with exp &nbsp;0 (3.14 * 2^0)</code></li><li>Now shift it (normalize it) and adjust the exponent accordingly <code>&nbsp;1.1001000111... with exp +1 (1.57 * 2^1)</code></li><li>Now you only have to add the bias of <code>127</code> to the exponent <code>1</code> and store it(i.e. <code>128</code> = <code>1000 0000</code>) <code>&nbsp;0 &nbsp; &nbsp; 1000 0000 &nbsp; &nbsp; 1100 1000 111...</code></li><li>Forget the top <code>1</code> of the mantissa (which is always supposed to be <code>1</code>, except for some special values, so it is not stored), and you get: <code>&nbsp;0 &nbsp; &nbsp; 1000 0000 &nbsp; &nbsp; 1001 0001 111...</code></li><li>So our value of <code>3.14</code> would be represented as something like:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    0 10000000 10010001111010111000011
    ^     ^               ^
    |     |               |
    |     |               +--- significand = 0.7853975
    |     |
    |     +------------------- exponent = 1
    |
    +------------------------- sign = 0 (positive)
</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>The number of bits in the exponent determines the range (the minimum and maximum values you can represent).</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Summing up significand</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>If you add up all the bits in the significand, they don't total <code>0.7853975</code>(which should be, according to 7 digit precision). They come out to <code>0.78539747</code>.</li><li>There aren't quite enough bits to store the value exactly. we can only store an approximation.</li><li>The number of bits in the significand determines the precision.</li><li>23-bits gives us roughly 6 decimal digits of precision. 64-bit floating-point types give roughly 12 to 15 digits of precision.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>&nbsp;<strong>Strange! But fact</strong> </p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Some values cannot represent exactly no matter how many bits you use. Just as values like 1/3 cannot represent in a finite number of decimal digits, values like 1/10 cannot represent in a finite number of bits.</li><li>Since values are approximate, calculations with them are also approximate, and rounding errors accumulate.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Let's see&nbsp;things working</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"c"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;stdio.h>
#include &lt;string.h>

/* Print binary stored in plain 32 bit block */ 
void intToBinary(unsigned int n)
{
        int c, k;
        for (c = 31; c >= 0; c--)
        {
                k = n >> c;
                if (k &amp; 1)  printf("1");
                else        printf("0");
        }
        printf("\n");
}

int main(void) 
{
        unsigned int m;
        float f = 3.14;

        /* See hex representation */
        printf("f = %a\n", f);  


        /* Copy memory representation of float to plain 32 bit block */
        memcpy(&amp;m, &amp;f, sizeof (m));     
        intToBinary(m);

        return 0;
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>This <a rel="noreferrer noopener" aria-label="C code (opens in a new tab)" href="http://www.vishalchovatiya.com/how-c-program-converted-into-assembly/" target="_blank"><span style="color:#1e1af0" class="color">C code</span></a> will print binary representation of float on the console.</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">f = 0x3.23d70cp+0
01000000010010001111010111000011</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>Where the decimal point is stored?</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The decimal point not explicitly stored anywhere.</li></ul>
<!-- /wp:list -->


<a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="http://www.vishalchovatiya.com/how-floating-point-no-is-stored-memory/" target="_blank"><span style="color:#3919d8" class="color">Click here to read more . . . !</span></a>

## Other related articles

- [21 new features of Modern C++ to use in your project](https://dev.to/visheshpatel/21-new-features-of-modern-c-to-use-in-your-project-3f87)
- [All about lambda function in C++](https://dev.to/visheshpatel/all-about-lambda-function-in-c-j4e)
- [C++ type casting with example for C developers](https://dev.to/visheshpatel/c-type-casting-with-example-for-c-developers-29b8)