我正在尝试在 Flex / actionscript 3 中构建一个计算器,但使用 Math 类得到了一些奇怪的结果:
trace(1.4 - .4); //should be 1 but it is 0.9999999999999999
trace(1.5 - .5); //should be 1 and it is 1
trace(1.444 - .444); //should be 1 and it is 1
trace(1.555 - .555); //should be 1 but it is 0.9999999999999999
我知道浮点数存在一些问题,但正如您所看到的,它至少应该在我的所有示例中失败,对吗?
这个问题在其他计算器中是如何解决的?我应该如何继续在 Actionscript 3 中构建一个可用的计算器?
先感谢您,
阿德南
欢迎使用 IEEE 754 浮点。享受不准确之处 http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems。如果您想避免它们,请使用定点机制。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)