Flash Player says n * 0.01 != n / 100
[Update] – Thanks to Wendel and everyone else on Twitter who pointed out why I should have gone to school for programming. This isn’t a bug, just “an artifact of float point operations”.
- - -
It’s no secret I’m an performance freak. I get excited whenever I learn of a new way to save a few milliseconds here or there. A few months ago, I learned that multiplication is slightly faster than division. Since then, I’ve been using multiplication over division when possible. Last night, when implementing alternate unit types in Dwarf, I experienced a strange behavior. As I resized a ruler, the size would randomly jump from two decimal points to 16—for example, 0.95 would appear as 0.9500000000000001.
At first, I just accepted it, but after a while, I decided to conduct an experiment. I tried using division instead of multiplication—surprisingly, this results in 0.95 appearing as 0.95. I then took it a step further and looped through a range of values:
Running from 0 to 500, checking each number for differences in multiplication vs division results in 62 instances where the bug occurs. From the test, I can conclude the following:
– The results do not vary from test to test. (ex: 35 * 0.01 = 0.35000000000000003 every time)
– The additional decimal points vary between 15 and 17 digits in length, with the last number ranging from 1 to 5
– The numbers affected are based on the divisor.
The results of the loop seemed to have a loose pattern, so I plugged them into the Google Charts API. The visuals are interesting, but don’t reveal an obvious pattern:
Next steps are to submit the bug to the mothership and hope for a fix!
This is not a bug. It’s an artifact of float point operations. See http://en.wikipedia.org/wiki/Floating_point
A good way to see what’s happening is thinking in base-10: 1/3 = 0.3333333… , but you don’t have infinite storage, you write 0.3333 . Then you multiply by 3, you get 0.9999 instead of 1.
Now, imagine what happens when computers use base-2. Not only 1/3 have repeating decimals, but now even 1/5 (0.2 in decimal) would have repeating decimals in binary.
Hey, this isn’t evidence for needing to go to programming school, this is evidence of the utility of Twitter (and commenters)!
I am seriously glad that not all coders did Computer Science! I didn’t, many of the best coders I know didn’t. Occasionally we expose our ignorance (I thought this was a bug, but it isn’t: http://alecmce.com/as3/the-problem-with-vector). I’d much rather blog about it and find out than I’m wrong than worry to never blog. This article has exposed this bit of maths to a lot of people.
Thanks, Alec
Well, even if programming school is not really “needed”, I don’t see the problem of coders doing Computer Science, Alec.
There you gain a lot of scientific insights about things. Like calculating complexity of algorithms, or dynamic programming… Okay, you can learn all those things by yourself (and it’s not a bad thing to do), but if one likes studying math/science, and it may make one a better coder, then what’s the problem ?
ps: yes, I did CS, but it was because I always liked math
@Alec — Thanks! I guess I didn’t think of it that way. It’s a bit embarrassing, but now I know.
@Wendel — I don’t think Alec is saying there’s anything wrong with Computer Science, just that some coders can get by without it. Sure, the ones who are formally educated have a leg up, but those who aren’t can still manage. I love math too, but growing up in a family of artists, art school had my name written all over it. I did, however, pick up a number of programming books recently, so I’ll be getting my learn on.
@Wendel
Yes, as Jonnie interpreted, my point was in favour of plurality, not against Computer Science.
In my experience, a plurality of backgrounds creates a more interesting, creative environment in which better applications are created. Experiences outside of CS can add richness, and variety, that can be missing if everyone comes from (roughly) the same background, read (roughly) the same books. This is probably less true for people working on compilers, but I think it is particularly true for User Interface development, which I suspect is what all of us do.
Sorry if I offended you – I certainly didn’t intend to.
Regards, Alec
@Jonnie Well, he explicity said he was “glad that not all coders did Computer Science”, as if “not doing CS” was a great thing, instead of just ok. That’s why I thought he meant that CS was wrong. (ok, that was too much metalinguistic
)
@Alec, no, you didn’t offended me, sorry me if I seemed to be offended (ok, that was way more metalinguistic). As I said, it seemed to me that you were agains CS, but now I see it wasn’t the case.
But you are right, being confined to what is taught in CS makes one miss a lot.
[ and, just to clarify - as you suspected "all of us" are doing UI - I'm kinda between the two worlds, actually. Not so low level ("compilers"), and not so high level ("user interface") ]
Now that everything is clear, and no one was offended, I gotta try to sleep
( it’s 0:50 here, and way too hot :-/ )
Bye!
@Wendel – metalinguistics? I’m a maths & philosophy graduate as it happens. Don’t get me started! I’ll be on about negating universal and existential quantifiers and you’ll be asleep in no time!
Crazy! I always find it exciting when I find bugs. Way to find it and document it soo well. ; )