For-loop in C++ using double breaking out one step early, boundary value not reached

后端 未结 10 634
难免孤独
难免孤独 2020-12-06 03:24

I have a simple C++ program compiled using gcc 4.2.4 on 32-bit Ubuntu 8.04. It has a for-loop in which a double variable is incremented from zero t

相关标签:
10条回答
  • 2020-12-06 04:06

    When using floating point values not every value is exactly representable, 0.95+0.05 > 1 because 0.95 is not exactly representable by a double value.

    See what Wikipedia has to say about floating point accuracy.

    If you look at the IEEE floating point converter you'll see that the value of 0.95 in 64 bit floating point (double) is 0-01111111110-1110011001100110011001100110011001100110011001100110 by entering this in a floating point calculator you get the value is 0.95000016 and adding 0.05 to that takes you over the 1.0 mark.

    This is why you should never use floating points in loops (or more generally compare the result of floating point calculation to an exact value).

    0 讨论(0)
  • 2020-12-06 04:12

    Probably the last index value would be like 1.00000001.

    0 讨论(0)
  • 2020-12-06 04:14

    As the previous answers , it is not accurate to use non-integers in for-loops , So I suggest to do as the following example so you keep the accuracy of integers and you can get the decimals you want:

    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std; 
    
    int main()
    {
    for (double y = 1; y!=10; y += 1)
        cout << static_cast<double>(y/10) << endl; 
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-06 04:16

    See this output: (floating point accuarcy)

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main(){
        double rangeMin = 0.0;
        double rangeMax = 1.0;
        double stepSize = 0.1;
        double index;
        for (index = rangeMin;  index <= rangeMax; index+=stepSize)
            {
                   cout << fixed << setprecision(16) <<  index << endl;
             }
      cout << endl;
      stepSize = 0.05;
      for (index = rangeMin; index<= rangeMax; index+= stepSize)
         {
             cout << index << endl;
                 }
    
       cout << "\n" << setprecision(16) << index << " "  << rangeMax;
       if(index==rangeMax)
          cout << "\nEQ";
       else
         cout << "\nNot EQ";
         return 0;
    }
    
    0.0000000000000000
    0.1000000000000000
    0.2000000000000000
    0.3000000000000000
    0.4000000000000000
    0.5000000000000000
    0.6000000000000000
    0.7000000000000000
    0.7999999999999999
    0.8999999999999999
    0.9999999999999999
    
    0.0000000000000000
    0.0500000000000000
    0.1000000000000000
    0.1500000000000000
    0.2000000000000000
    0.2500000000000000
    0.3000000000000000
    0.3500000000000000
    0.4000000000000000
    0.4500000000000000
    0.4999999999999999
    0.5499999999999999
    0.6000000000000000
    0.6500000000000000
    0.7000000000000001
    0.7500000000000001
    0.8000000000000002
    0.8500000000000002
    0.9000000000000002
    0.9500000000000003
    
    1.0000000000000002 1.0000000000000000
    Not EQ
    
    0 讨论(0)
提交回复
热议问题