I have a very weird error with c++.
I have two values, max and singleStep. The number of steps is stepsInt and/or stepsDbl:
max = 100;
singleStep = 0.1;
// This means that I have 100/0.1 = 1000 numbers betwwen 0 and 100
double stepsDbl = max/singleStep;
int stepsInt = (int)(stepsDbl);
cout << stepsDbl << stepsInt;
You can expect an output like this:
1000 1000
However I’m getting this output:
1000 999
What is more, if I try a different value for singleStep, for instance 0.2, I get again wrong values
500 499
I don’t know what is happening but is pretty weird… If anyone has a solution for this problem I would appreciate that solution.
Thanks
Try this instead:
The “problem” you’re seeing is because of the way floating point numbers are stored internally.