用Flex简单的计算问题 [英] Simple calculation problem with Flex
问题描述
从69.8开始:
以像素计算MM:69.8 mm
69.8 * 300 = 20940
20940 / 2.54 = 8244.094488188975
8244.094488188975 / 10 = 824.4094488188975
并计算回来: 我们想要69.8,但以69.79999999999998结尾。 在这将是伟大的。 好吧,这不是一个Flex问题,因为它是关于一般编程。你有没有想过如何存储在电脑中的数字?细节更多的是真实的数学,但我会尽量简化。你知道有无数个不同的实数(例如,像实线上的点),但是在你的计算机中,你把所有的东西都存储为零和一个,以及有限的数量(32或64比特)。所以现在我们有一个问题 - 如何在有限的空间中表示无限数量的数字。在浮点数中使用的想法(可以使用像1.03或4.2232这样的点的值)是因为你不能拥有所有的数字,所以你把数字四舍五入到最接近的数字。 这有点像记得有人在咖啡里放了多少糖,你不记得他喜欢吃1.1232汤匙糖,因为桌上的勺子是只是测量它的确切数量的东西不好。大部分时间你都可以正常工作。 所以浮点数也有类似的想法,还有一些额外的扭曲 - 数字更密集接近于0而不是离开它,它们之间的空白空间真的很大。例如,如果你从最大值减去10000,一个数字可以得到它仍然是相同的,因为没有数字接近它,以致在寻找最接近的数字时有所不同。 所以你的问题来自假设数字是完全精确的而他们不是,你总是把事情弄圆。 as3中的Number遵循双精度IEEE-754标准,这就是它如何决定它有哪个数字以及哪个数字取整。 看看这个: p> 更多阅读: Im having a trouble with the flex calculation, im trying to transform a pixel value to mm and reversing it. Starting out with 69.8: And calculating back: We wanted 69.8 but ended up with 69.79999999999998.
I tracked the proccess using simple windows calc and the first place it wents wrong is at 20940 / 2.54 = 8244.094488188975 wich should be 8244,094488188976. Anny help on this would be great. Ok, this is not as much a Flex question as it is about general programming. Have you ever wondered how exactly are numbers stored in the computer? The details are more in the real of mathematics, but I'll try to put it simple. You know that there is an infinite number of different Real numbers (eg. like points on a continuous line), but in your computer you store everything as zeroes and ones, and a limited amount of them (32 or 64 "bits"). So now we have a problem - how to represent an unlimited amount of numbers in limited space. The idea used in the floating-point number (ones that can have values withe a "dot" like 1.03 or 4.2232) is that, because you can't have them all, you round the number to the closest one you have. It's a bit like remembering how much sugar someone puts in his coffee, you don't remember that he likes to have 1.1232 table spoons of sugar, because the table spoon is just not that good in measuring the exact amount of stuff on it. You round it to one and it works fine most of the time. So with floating point numbers a similar idea stands, with an extra twist - the numbers are much more dense near 0 than away from it, where the "empty space" between them get's really big. For example if you subtract 10000 from the maximal value a number can get it will still be the same, because there is no number as close to it that it makes a difference when looking for the closest one. So your problem comes from assuming that numbers are totally precise while they aren't, you always get things rounded. The Number in as3 adheres to the double-precision IEEE-754 standard, and that's how it decides which number it has and which it rounds. Look at this: Further reading: 这篇关于用Flex简单的计算问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< 824.4094488188975
824.4094488188975 / 300 = 2.7480314960629917
2.7480314960629917 * 2.54 = 6.979999999999999
6.979999999999999 * 10 = 69.79999999999998
/ code>
我使用简单的windows calc跟踪了这个过程,第一个错误的地方是20940 / 2.54 = 8244.094488188975,应该是8244,094488188976。
trace(Number.MAX_VALUE == Number.MAX_VALUE-10000);
//返回true
trace(200000 == 200000 - 10000);
//返回false
trace(8244.094488188976);
//返回8244.094488188975
Calculating MM to pixel from: 69.8 mm
69.8*300 = 20940
20940 / 2.54 = 8244.094488188975
8244.094488188975 / 10 = 824.4094488188975
Calculating pixel to MM from: 824.4094488188975
824.4094488188975/300 = 2.7480314960629917
2.7480314960629917 * 2.54 = 6.979999999999999
6.979999999999999 * 10 = 69.79999999999998
trace (Number.MAX_VALUE == Number.MAX_VALUE-10000);
// returns "true"
trace (200000 == 200000 - 10000);
// returns "false"
trace (8244.094488188976);
// returns "8244.094488188975"