double c, d, e;
double a = (c - d) / e;
double b = Math.Floor(a);
Debug.WriteLine(a.ToString() + " " + b.ToString());
Code above outputs “3 2” at one configuration where all numbers are double. How is this possible? Is it because of fractional error resulting from double operations? However I think a.ToString() should give the whole number with its fractional part.
It’s just a matter of what
double.ToString()does. Here’s a short but complete program demonstrating the same thing:Output:
Now
double.ToString()is documented with:… and the general numeric format specifier docs state:
… where the table shows that the default precision for
doubleis 15. If you consider 2.9999999999999996 rounded to 15 significant digits, you end up with 3.In fact, the exact value of
ahere is:… which again, is 3 when regarded with 15 significant digits.