Discuss Scratch

Choppead
Scratcher
5 posts

Repeating decimal problem

SO…
When I try to add/subtract 2 decimals
((0.7) + (0.2))
You think that the answer would be 0.9, but for some reason it just ends up being a very long string of decimals (in this case being 0.899999…)
And it doesn't only just happen in the editor, but in the project aswell.
This is a problem for clone-based counters like the one I'm using, so please fix this bug or give me some workarounds.
Knightbot63
Scratcher
1000+ posts

Repeating decimal problem

This is some sort of JavaScript float bug I believe. The only way to fix this is using the round operator unless you're using the decimals.
EDawg2011
Scratcher
1000+ posts

Repeating decimal problem

british_rod wrote:

Choppead wrote:

snip

((((0.7) + (0.2)) * (10)) / (10))
It's actually

(round (((0.7) + (0.2)) * (10))) / (10)
Knightbot63
Scratcher
1000+ posts

Repeating decimal problem

EDawg2011 wrote:

It's actually

(round (((0.7) + (0.2)) * (10))) / (10)
That returns 1… Not 0.9. To get 0.9, you need to remove the multiply and divide operators.
Edit: Javascript floats are very finicky and it returns 1 no matter what. Closest result is without the round block…

Last edited by Knightbot63 (Feb. 7, 2024 02:37:39)

medians
Scratcher
1000+ posts

Repeating decimal problem

Knightbot63 wrote:

EDawg2011 wrote:

It's actually

(round (((0.7) + (0.2)) * (10))) / (10)
That returns 1… Not 0.9. To get 0.9, you need to remove the multiply and divide operators.
Edit: Javascript floats are very finicky and it returns 1 no matter what. Closest result is without the round block…
I can't replicate, did you multiply by 10 inside the round?
PhiPhenomenon
Scratcher
500+ posts

Repeating decimal problem

Knightbot63 wrote:

This is some sort of JavaScript float bug I believe.
That's right, it's because numbers are represented in binary.

Here's a more in-depth explanation. Computers currently use the binary number system to represent numbers. This is pretty easy to do with whole numbers:


However, with numbers with decimals, it might not be possible to represent them in a way that doesn't repeat in binary. Because computers have a limited amount of memory, they have to make an approximation.


Usually, the approximation is close to what the actual number is, but it usually won't be perfect.
HarryCreator26518
Scratcher
100+ posts

Repeating decimal problem

Probably some java bug

Powered by DjangoBB