Hi i asked this question on SO but no one seems to be bothered to answer the question with actual information.
Firstly just to say, i fully know computers are imprecise for floating numbers and doubles etc etc.
But what i would like to understand is why the imprecision fails for just a select few numbers in this code:
double o = 0.795660;
for (float i = 0; i < 4096; i++)
{
double a = i+0.795660;
double b = (i+1)+0.795660;
double resA = (int) System.Math.Floor((a - o) / 1.0);
double resB = (int) System.Math.Floor((b - o) / 1.0);
if (System.Math.Abs(resA - resB) < Mathf.Epsilon)
{
Debug.Log(i + " :: "+ (i+1));
}
}
Output:
1 :: 2 => 2^1
31 :: 32 => 2^5
4095 :: 4096 => 2^12
It can't be a coincidence these numbers fail when they are 2^n.
What is it about these particular numbers that the computer can't compute accurately but it managed to do so for the all the other numbers in the loop ?
My original question was here: https://stackoverflow.com/questions/59387659/fix-for-numbers-close-to-25-and-27-imprecision-problems
I just got the vague answer of "because imprecision" but no one explained why specific numbers failed in any educational detail.
I'm not a comp science student, i'm a self taught programmer, and though i understand computers are not good at representing floating numbers, i'd like to know why certain numbers fail more than others.
This was in C# i don't know if other languages will produce the same problem.