我注意到数学运算有时对于小数而言不如对双打更准确。例如,当我运行此代码时:
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)。
如果小数应该比双打更准确,为什么会这样呢?