How to make doubles work properly? c#

一曲冷凌霜 提交于 2019-12-10 22:47:56

问题


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


回答1:


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);
}



回答2:


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));
}



回答3:


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




回答4:


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);
    }
}


来源:https://stackoverflow.com/questions/10693098/how-to-make-doubles-work-properly-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!