飞镖错误计算总和
Dart wrong calculation in sum
有人可以向我解释为什么以下总和在 dart 中给出错误的结果吗?
final double result = 90071992547409.9 + 0.01;
print(result);
它打印数字 90071992547409.92
Dart doubles are 64-bit floating-point numbers as specified in the IEEE 754 standard.
因为floating-point arithmetic. In your case (I used this converter):
90071992547409.9 = 90071992547409.90625 ~= 90071992547409.91
0.01 = 0.01000000000000000020816681711721685132943093776702880859375 ~= 0.01
90071992547409.91 + 0.01 = 90071992547409.92
dart 中最好的解决方案是使用 decimal 包。
有人可以向我解释为什么以下总和在 dart 中给出错误的结果吗?
final double result = 90071992547409.9 + 0.01;
print(result);
它打印数字 90071992547409.92
Dart doubles are 64-bit floating-point numbers as specified in the IEEE 754 standard.
因为floating-point arithmetic. In your case (I used this converter):
90071992547409.9 = 90071992547409.90625 ~= 90071992547409.91
0.01 = 0.01000000000000000020816681711721685132943093776702880859375 ~= 0.01
90071992547409.91 + 0.01 = 90071992547409.92
dart 中最好的解决方案是使用 decimal 包。