Discuss Scratch

Prinseskat
Scratcher
1000+ posts

Subtraction bug - 2.2 - 2 = 0.20000000000000018?

I've found a bug where, if you subtract a number from a decimal number over 1 or vice versa, it'll sometimes result in a long number such as 1.299999999998. This seems to happen on any project on the online editor. Example:
1.5 - 1.2 = 0.30000000000000004, not 0.3
5.6 - 3.2 = 2.3999999999999995, not 2.4
17.4 - 18 = -0.6000000000000014, not -0.6
5 - 4.56 = 0.4400000000000004, not 44.
However, it doesn't always happen.
5 - 3.5 = 1.5
2.1 - 1 = 1.1
78.5 - 45 = 33.5
The interesting thing is that the numbers aren't that far from the real numbers, and you can fix it by rounding them.
My browser / operating system: ChromeOS 4731.81.0, Chrome 31.0.1650.62, Flash 11.9 (release 900)

haha funny kumquat meme
TheHockeyist
Scratcher
1000+ posts

Subtraction bug - 2.2 - 2 = 0.20000000000000018?

Computers aren't perfect. It's because these binary numbers go on forever and Scratch has to snip some off. As a result, the final result gets flawed.

It's like trying to subtract (1/3 + 1/3 + 1/3) from 1 in decimal. 1/3 is 0.3333333333333333333333333… forever. And we have to snip some of that off so that we don't have to work it out infinitely.

Let's assume 5 places of accuracy.

1/3 = 0.33333

1 - 0.33333 = 0.66667

0.66667 - 0.33333 = 0.33334

0.33334 - 0.33333 = 0.00001

And since 1 is 3/3, 0 = 0.00001.

A similar thing is occurring here.


Powered by DjangoBB