title: Why does a cast from int to float round the value?
body: <p>I'm reading CS:APP, and regarding casts it says that when casting <strong>from int to float, the number cannot overflow, but it may be rounded.</strong></p>
  
  <p>It seemed odd to me as I didn't know what there was to round, so I've tried it out. I thought that this would only be the case for very large integers (near <code>INT_MAX</code>/<code>INT_MIN</code>), but rounding happens at values around a hundred million as well. (Not sure where exactly this happens first).</p>
  
  <p>Why does this happen? The range of <code>float</code> far exceeds that of <code>int</code>. One might say that floating point numbers cannot be represented exactly, but when converting from <code>int</code>to <code>double</code> there is no change in value. The advantage of <code>double</code>over <code>float</code> is that it has greater range and precision. But <code>float</code>still has enough range to "encapsulate" integers, and precision shouldn't really matter as integers have no decimal places (well, all 0), or am I thinking wrong?</p>
  
  <p>Here's some output that I got (here is the code: <a href="http://pastebin.com/K3E3A6Ni" rel="noreferrer">http://pastebin.com/K3E3A6Ni</a>):</p>
  
  <pre><code>FLT_MAX = 340282346638528859811704183484516925440.000000  
  INT_MAX     = 2147483647  
  (float)INT_MAX = 2147483648.000000  
  (double)INT_MAX = 2147483647.000000  
  INT_MIN     = -2147483648  
  (float)INT_MIN = -2147483648.000000  
  
  ====other values close to INT_MIN INT_MAX====  
  INT_MAX-1     = 2147483646  
  (float)INT_MAX-1 = 2147483648.000000  
  INT_MIN+1     = -2147483647  
  (float)INT_MIN+1 = -2147483648.000000  
  INT_MAX-2      = 2147483645  
  (float)INT_MAX-2  = 2147483648.000000  
  INT_MAX-10     = 2147483637  
  (float)INT_MAX-10 = 2147483648.000000  
  INT_MAX-100         = 2147483547  
  (float)INT_MAX-100  = 2147483520.000000  
  INT_MAX-1000         = 2147482647  
  (float)INT_MAX-1000 = 2147482624.000000  
  
  (float)1.234.567.809 = 1234567808.000000  
  (float)1.234.567.800 = 1234567808.000000  
  (float)1.000.000.005 = 1000000000.000000  
  (float)800.000.003   = 800000000.000000  
  (float)500.000.007   = 500000000.000000  
  (float)100.000.009   = 100000008.000000  
  </code></pre>