0

I've written this simple piece of code to figure out a bug I faced in one part of a project.

Kindly find my code below:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
 float x = 0;
 int c=30;
 while(c--){
    cout<<x<<endl;
    x += 0.1;
    if(x>1.00){ 
        cout<<"reset"<<x<<endl;
        x=0;
     }
 }

}

However instead of printing a steady stream like 0 -> 0.1 -> 0.2 -> ..... -> 0.8 -> 0.9 -> 1 -> 0.1, it print below:-

enter image description here

X resets back to zero when at 1 and not when x>1. I'm guessing it might be because x is float here and so x might not be purely 1.000000(so on) but might be something like 1.00000002 or something.

My question is if this assumption is right and also what can i do to rectify this and get the printed stuff as i need. Thank you in advance!!

jeremy
  • 59
  • 7

0 Answers0