title: Double overflow?
body: <p>I have always wondered what happens in case a <code>double</code> reaches it's max value, so I decided to write this code: </p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;iostream&gt;
  #define UINT64_SIZE 18446744073709551615
  int main() {
      std::uint64_t i = UINT64_SIZE;
      double d1 = ((double)(i+1)) / UINT64_SIZE; 
      double d2 = (((double)(i)) / UINT64_SIZE)*16;
      double d3 = ((double)(i * 16)) / UINT64_SIZE;
  
      std::cout &lt;&lt; d1 &lt;&lt; " " &lt;&lt; d2 &lt;&lt; " " &lt;&lt; d3; 
  }
  </code></pre>
  
  <p>I was expecting something like this: </p>
  
  <pre><code>0 16 0 
  </code></pre>
  
  <p>But this is my output: </p>
  
  <pre><code>0 16 1 
  </code></pre>
  
  <p>What is going on here? Why are the values of <code>d3</code> and <code>d1</code> different?  </p>
  
  <p>EDIT: </p>
  
  <p>I decided to change my code to this to see the result: </p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;iostream&gt;
  #define UINT64_SIZE 18446744073709551615
  int main() {
      std::uint64_t i = UINT64_SIZE;
      double d1 = ((double)(i+1.0)) / UINT64_SIZE; //what? 
      double d2 = (((double)(i)) / UINT64_SIZE)*16;
      double d3 = ((double)(i * 16.0)) / UINT64_SIZE;
  
      std::cout &lt;&lt; d1 &lt;&lt; " " &lt;&lt; d2 &lt;&lt; " " &lt;&lt; d3; 
  }
  </code></pre>
  
  <p>The result I get now is this: </p>
  
  <pre><code>1 16 16 
  </code></pre>
  
  <p>However, shouldn't <code>d1</code> and <code>d3</code> still be the same value? </p>