cycle
-
why in the cycle for (double i=-1; i<=1; i=i+0.1) the 0 is equal -1.38778e-016
for(double i=-1; i<=1;i=i+0.1)
cout<<i<<"\n";
result:
-1
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
-1.38778e-016
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1That is because floating point numbers are not exact. I don't have a good link handy, but if you google "floating point numbers" you'll find a number of articles on the subject. EDIT: Here's an article that talks about the issue: The trouble with rounding floating point numbers • The Register[^]
The difficult we do right away... ...the impossible takes slightly longer.
-
That is because floating point numbers are not exact. I don't have a good link handy, but if you google "floating point numbers" you'll find a number of articles on the subject. EDIT: Here's an article that talks about the issue: The trouble with rounding floating point numbers • The Register[^]
The difficult we do right away... ...the impossible takes slightly longer.
-
why in the cycle for (double i=-1; i<=1; i=i+0.1) the 0 is equal -1.38778e-016
for(double i=-1; i<=1;i=i+0.1)
cout<<i<<"\n";
result:
-1
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
-1.38778e-016
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1In C#, you can use this formula to account for the floats issues with zero; (Math.Abs(priorDegreePosition - degreePosition) < double.Epsilon) As the other writers mentioned, this number of -1.38778e-016, is pretty close to zero. If you were to move the decimal over the 16 place-holders, it would round to zero. Therefore, it sounds more of a formatting issue you are dealing with in your C++ example. In my statement above, C# has the smallest value for doubles/floats in the Epsilon.
Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach