user27338077
user27338077

Reputation: 1

Simple while loop outputting unexpected values (beginner)

Learning c++, wrote a simple while loop for practice and when I run the program, the increment is not consistent for each iteration. After printing 0.83, the next printed increment is 0.839999 rather than 0.84.

This is the while loop I made:

#include <iostream>
using namespace std;

int main()
{
    float f1 = 0.00;
    float f2 = 1.00;

    while (f2 > f1)
    {
        cout << f1 << endl;
        f1 += .01;
    }
    system("pause");
}

This is the problematic part of the output I see when I run the program (all previous increments are correct):

0.83
0.839999
0.849999
0.859999
0.869999
0.879999
0.889999
0.899999
0.909999
0.919999
0.929999
0.939999
0.949999
0.959999
0.969999
0.979999
0.989999
0.999999

Upvotes: 0

Views: 46

Answers (0)

Related Questions