C# float double精度损的的问题

来源:互联网 发布:买电动车上淘宝 编辑:程序博客网 时间:2024/05/18 15:08

在工作中我发现了一个C#浮点数的精度问题,以下的程序运行结果并未得到我预期的结果:

view sourceprint?
01namespace FloatTest
02{
03    classProgram
04    {
05        staticvoid Main(string[] args)
06        {
07            doublea = 0.0001;
08            floatb = 0.1F;
09
10            intc = (int)((a * 1000) / b);
11
12            Console.WriteLine("c = {0}", c);
13
14            Console.ReadLine();
15        }
16    }
17}

我期望的结果是得到1,结果程序返回的结果为c = 0

这让我想到了可能是因为浮点数采用IEEE754的表示方法,在运算中b会转换成double,可能是在转换中算法的问题导致精度丢失,为了证实该问题,我做了下面的实验:

view sourceprint?
01namespace FloatTest
02{
03    classProgram
04    {
05        staticvoid Main(string[] args)
06        {
07            doublea = 0.0001;
08            floatb = 0.1F;
09
10            intc = (int)((a * 1000) / b);
11
12            Console.WriteLine("a = {0}", a);
13            Console.WriteLine("b = {0}", Convert.ToDouble(b));
14            Console.WriteLine("c = {0}", c);
15
16            Console.ReadLine();
17        }
18    }
19}

这次果然得到了意料中的结果:float在转成double的时候出现了精度的丢失问题

a = 0.0001
b = 0.100000001490116
c = 0

如果是在类型转换的时候导致的精度丢失,那我把b改为double应该可以解决这个问题:

view sourceprint?
01namespace FloatTest
02{
03    classProgram
04    {
05        staticvoid Main(string[] args)
06        {
07            doublea = 0.0001;
08            doubleb = 0.1;
09
10            intc = (int)((a * 1000) / b);
11
12            Console.WriteLine("a = {0}", a);
13            Console.WriteLine("b = {0}", Convert.ToDouble(b));
14            Console.WriteLine("c = {0}", c);
15
16            Console.ReadLine();
17        }
18    }
19}

这次却是得到我们期望的结果:

a = 0.0001
b = 0.1
c = 1

因此,在程序中,我们应该尽量的避免浮点数类型转换导致的精度丢失。

我在GCC上做了同样的实验,结果不管哪种方式得到的结果都是正确的,我只能说可能是double Convert.ToDouble(float)的实现的原因导致的。