Discuss Scratch

mrCamelCase
Scratcher
100+ posts

Bug with Using Join Block to Create Floating Point Numbers

My browser / operating system: MacOS Macintosh X 10.15, Firefox 81.0, ofc there's no Flash version detected!
((join [1.] [2])- (1))

Outputs 0.199999999999996 (not sure how many 9's).
Considering this is not possible in most other languages the fact that this even compiles is cause for celebration (this is one of the best things about Scratch imo).

Even so, it would be nice if it equaled 0.2. Would this be possible with a quick patch, or is the problem a bit more complicated than that (guessing the latter, but figured I'd ask anyways)?

Yours truly,
-mCC

Last edited by mrCamelCase (Oct. 7, 2020 22:49:35)


“A lie told often enough becomes the truth.”
-Benjamin Franklin, 1532

^ Food for thought. Yum! ^
Scratch-Minion
Scratcher
1000+ posts

Bug with Using Join Block to Create Floating Point Numbers

1/3 = 0.33333333 recurring be represented exactly in base 10.
So if we had 8 decimal places 2 x 1/3 = 0.66666667

Computers use base 2 and cannot represent any decimal if, as a fraction, its denominator is not a factor of 2.
So 1/2, 1/4, 3/8, 7/16 etc can be represented exactly in binary.

But 0.2 = 1/5 cannot. It has an infinite “binimal” part (like a decimal part but base 2).
So 1.2 cannot be represented exactly in base 2.

If you want to correct any number to 1 decimal place:

set [Number v] to ((round ((Number) * (10))) / (10))

eg. 0.199999999999996 * 10 = 1.99999999999996
round(1.99999999999996) = 2
2/10 = 0.02
mrCamelCase
Scratcher
100+ posts

Bug with Using Join Block to Create Floating Point Numbers

Scratch-Minion wrote:

1/3 = 0.33333333 recurring be represented exactly in base 10.
So if we had 8 decimal places 2 x 1/3 = 0.66666667

Computers use base 2 and cannot represent any decimal if, as a fraction, its denominator is not a factor of 2.
So 1/2, 1/4, 3/8, 7/16 etc can be represented exactly in binary.

But 0.2 = 1/5 cannot. It has an infinite “binimal” part (like a decimal part but base 2).
So 1.2 cannot be represented exactly in base 2.

If you want to correct any number to 1 decimal place:

set [Number v] to ((round ((Number) * (10))) / (10))

eg. 0.199999999999996 * 10 = 1.99999999999996
round(1.99999999999996) = 2
2/10 = 0.02

Ok, that's a really good workaround. I'll keep that in mind for future projects, thanks!

“A lie told often enough becomes the truth.”
-Benjamin Franklin, 1532

^ Food for thought. Yum! ^

Powered by DjangoBB