2

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: enter image description here

I want to keep on adding 0.01 (as You can see on the screen, sometimes it happens to add 0.99999) Thanks

Patryk
  • 3,042
  • 11
  • 41
  • 83

4 Answers4

14

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.

decimal xd2 = 5m;

for (decimal xd = xd2; xd <= 6m; xd += 0.01m)
{
    Console.WriteLine(xd);
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 5
    It's worth noting that `decimal` is not fundamentally more "precise" than `double` (except for the greater number of bits it has at its disposal). It's simply stored in base 10, so numbers expressed in the familiar decimal point notation (as in your example) *can* be expressed exactly, while in the base-2 `float` and `double` types, they can't. `float` and `double` can exactly store numbers expressed in binary point notation; this is just not very useful, since no one uses that notation. – Will Vousden May 21 '12 at 22:04
5

No. That is how doubles work.... try using decimal instead

 int xd2 = 5;

 for (decimal xd = (decimal)xd2; xd <= 6; xd += 0.01M)
 {
     Console.WriteLine(xd);
 }

if you want to stick with doubles, but only care to two decimal places use...

int xd2 = 5;

for (double xd = (double)xd2; xd <= 6; xd += 0.01)
{
   Console.WriteLine(Math.Round(xd,2));
}
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
2

This is because double is float pointing and this arithmetic is not precise. You can use decimal instead, like this:

 static void Main(string[] args)
    {
        int xd2 = 5;

        for (decimal xd = (decimal)xd2; xd <= 6; xd += 0.01M)
        {
            Console.WriteLine(xd);
        }
        Console.ReadLine();
    }

See this article too: Double precision problems on .NET

Community
  • 1
  • 1
1

If possible you should always use absolute instead of iterative calculations to get rid of these kinds of rounding errors:

public static void Main(string[] args)
{
    int xd2 = 5;

    for (int i = 0; i < 100; ++i) {
        Console.WriteLine(xd2 + i * 0.01);
    }
}
aschoenebeck
  • 439
  • 7
  • 9