title: For-loop in C++ using double breaking out one step early, boundary value not reached
body: <p>I have a simple C++ program compiled using gcc 4.2.4 on 32-bit Ubuntu 8.04. It has a <code>for</code>-loop in which a <code>double</code> variable is incremented from zero to one with a certain step size. When the step size is <code>0.1</code>, the behavior is what I expected. But when the step size is '0.05', the loop exits after <code>0.95</code>. Can anyone tell me why this is happening? The output follows the source code below.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      double rangeMin = 0.0;
      double rangeMax = 1.0;
      double stepSize = 0.1;
  
      for (double index = rangeMin; index &lt;= rangeMax; index+= stepSize)
      {
          cout &lt;&lt; index &lt;&lt; endl;
      }
      cout &lt;&lt; endl; 
  
      stepSize = 0.05;
      for (double index = rangeMin; index &lt;= rangeMax; index+= stepSize)
      {
          cout &lt;&lt; index &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>OUTPUT</p>
  
  <pre><code>sarva@savija-dev:~/code/scratch$ ./a.out 
  0
  0.1
  0.2
  0.3
  0.4
  0.5
  0.6
  0.7
  0.8
  0.9
  1
  
  0
  0.05
  0.1
  0.15
  0.2
  0.25
  0.3
  0.35
  0.4
  0.45
  0.5
  0.55
  0.6
  0.65
  0.7
  0.75
  0.8
  0.85
  0.9
  0.95
  sarva@savija-dev:~/code/scratch$
  </code></pre>