问题描述
是否有一个很好的浮点错误解决方案?
如果我得到:
0.00831 + 0.000001 = 0.00831109999999999999
我可以避免这比通过比较2个数字长度更好的方法
小数点后使用toFixed()来匹配较短的数字和
另一个?
谢谢。
Is there a good solution for floating-point errors?
If I got:
0.00831 + 0.000001 = 0.00831109999999999999
can I avoid this by a better method than comparing 2 number length
after decimal point and using toFixed() to match shorter number with
another one?
Thanks.
推荐答案
是的。为了简单起见,在添加之前将每个乘以1000000:
8310 + 1 = 8311
Yes. To make it simpler, multiply each by 1000000 before adding:
8310 + 1 = 8311
是的。为了简单起见,在添加之前将每个乘以1000000:
8310 + 1 = 8311
Yes. To make it simpler, multiply each by 1000000 before adding:
8310 + 1 = 8311
非常好。谢谢。
very good. thank you.
是的。为了简单起见,在添加之前将每个乘以1000000:
8310 + 1 = 8311
Yes. To make it simpler, multiply each by 1000000 before adding:
8310 + 1 = 8311
0.00831 + 0.000001!= 8311
-
兰迪
机会有利于准备好的心灵
comp.lang.javascript常见问题 -
Javascript最佳实践 -
这篇关于浮点错误的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!