flash as3 自动四舍五入数字
我写了一篇很长的文章试图解释我遇到的问题的确切细节,但我认为我会保持简单,并在这里提出一个示例问题:
var n1:Number = 9.99999999999999;
n1 += 0.000000000000009;
var n2:Number = n1 + 10;
var n3:Number = n1 - 10;
长话短说,n1 = 9.99....7,n2 = 20 ,n3 = 10。
如果我尝试对 n1 和 n3 进行比较,它们应该相同,但事实并非如此。我不在乎闪光是否围绕它,我只需要它们是相同的(它们不是,在一种情况下导致闪光,而不是另一种情况)。
对于这样的问题有一些标准的解决方案吗?
PS 我的数字不需要这种精度,但我也不想每次进行操作时都对数字的舍入进行微观管理(这似乎可以在混合中添加很多代码)。然而,如果这是唯一的解决方案,我想我只需在整个代码中进行大量舍入即可,哈哈。
I wrote out a long post trying to explain the exact details of the problem im having, but instead i think ill keep it simple, and ask an example question here:
var n1:Number = 9.99999999999999;
n1 += 0.000000000000009;
var n2:Number = n1 + 10;
var n3:Number = n1 - 10;
Long story short, n1 = 9.99....7, n2 = 20, n3 = 10.
If i try to make a comparison between n1 and n3, they should be the same but they arn't. I dont care if flash rounds it or not, i just need them to be the same (and they arn't, cause flash rounds in one case, and not the other).
Is there some standard solution for a problem like this?
P.S. I dont need this precision on my numbers, but i also would not like to micromanage the rounding of the numbers EVERY time i do a manipulation (that seems like it could add a LOT of code to the mix). If this is the only solution however, i guess ill just have to do a lot of rounding throughout the code, ha.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
flash中的
Number
是一个双精度浮点数。请在此处阅读有关它们的更多信息。这些“问题”并不是闪存独有的,而只是与这些数字的存储方式有关。有几个选择。这是一个快速小库,用于模糊比较数字,在一定的误差范围内。另一种选择是实现定点数学库。
The
Number
in flash is a double precision floating point number. Read more here about them. These "problems" are not unique to flash, but just have to do with how these numbers are stored.There are a couple of options. Here is a quick little library for fuzzy comparing numbers, within a certain margin of error. Another option would be implement a fixed point math library.
如果你想比较,只用 int() 包装有问题吗?
Is it a problem to just wrap in int() if you're trying to compare?