Here's the code:
static void Main(string[] args)
{
int xd2 = 5;
for (double xd = (double)xd2; xd <= 6; xd += 0.01)
{
Console.WriteLine(xd);
}
}
and here's the output:
I want to keep on adding 0.01 (as You can see on the screen, sometimes it happens to add 0.99999) Thanks
This is because double is float pointing and this arithmetic is not precise. You can use decimal instead, like this:
See this article too: Double precision problems on .NET
Use
decimal
if you want to keep this kind of accuracy.Floating point types cannot accurately represent certain values. I suggest reading What Every Computer Scientist Should Know About Floating-Point Arithmetic for a comprehensive explanation.
No. That is how doubles work.... try using decimal instead
if you want to stick with doubles, but only care to two decimal places use...
If possible you should always use absolute instead of iterative calculations to get rid of these kinds of rounding errors: