十进制与双数学精度

时间:2017-05-10 21:55:29

标签: .net vb.net

我注意到数学运算有时对于小数而言不如对双打更准确。例如,当我运行此代码时:

    Dim a As Decimal = 1
    Dim b As Decimal = 3
    Dim c As Decimal = (a / b) * b

    Dim x As Double = 1
    Dim y As Double = 3
    Dim z As Double = (x / y) * y

    Console.WriteLine(c)
    Console.WriteLine(a = c)
    Console.WriteLine(z)
    Console.WriteLine(x = z)

输出:

0.9999999999999999999999999999
False
1
True

如您所见,Decimals给出浮点问题(0.999999),双打给出确切的数字(1)。

如果小数应该比双打更准确,为什么会这样呢?

0 个答案:

没有答案