Discuss Scratch
- Discussion Forums
- » Bugs and Glitches
- » The operators multiplication and division returns wrong results when calculation with point numbers.
- Scratch_Andy_DE
-
Scratcher
10 posts
The operators multiplication and division returns wrong results when calculation with point numbers.
Hi,
The operators multiplication and division returns wrong results when calculation with point numbers.
For example the result of 2.3 x 6 is 13.799999999999999 in place of 13.8.
And reversed the result of 13.8 / 6 is 2.3000000000000003 in place of 2.3.
When I set a variable with this result they shows 13.8 respectively 2.3.
But the length of this variable is 18.
And the blocks “Say” and “Think” shows also different results of this variable. Say shows 13.8 respective 2.3 and Think 13.799999999999999 respective 2.3000000000000003
The operators multiplication and division returns wrong results when calculation with point numbers.
For example the result of 2.3 x 6 is 13.799999999999999 in place of 13.8.
And reversed the result of 13.8 / 6 is 2.3000000000000003 in place of 2.3.
When I set a variable with this result they shows 13.8 respectively 2.3.
But the length of this variable is 18.
And the blocks “Say” and “Think” shows also different results of this variable. Say shows 13.8 respective 2.3 and Think 13.799999999999999 respective 2.3000000000000003
- ResExsention
-
New Scratcher
1000+ posts
The operators multiplication and division returns wrong results when calculation with point numbers.
Hi,
The operators multiplication and division returns wrong results when calculation with point numbers.
For example the result of 2.3 x 6 is 13.799999999999999 in place of 13.8.
And reversed the result of 13.8 / 6 is 2.3000000000000003 in place of 2.3.
When I set a variable with this result they shows 13.8 respectively 2.3.
But the length of this variable is 18.
And the blocks “Say” and “Think” shows also different results of this variable. Say shows 13.8 respective 2.3 and Think 13.799999999999999 respective 2.3000000000000003
Scratch isn't wrong. Scratch is just rounding off the variables for some reason.
Not sure how to overcome this, perhaps the variables may not have enough bytes?
- Scratch_Andy_DE
-
Scratcher
10 posts
The operators multiplication and division returns wrong results when calculation with point numbers.
Hi,
The operators multiplication and division returns wrong results when calculation with point numbers.
For example the result of 2.3 x 6 is 13.799999999999999 in place of 13.8.
And reversed the result of 13.8 / 6 is 2.3000000000000003 in place of 2.3.
When I set a variable with this result they shows 13.8 respectively 2.3.
But the length of this variable is 18.
And the blocks “Say” and “Think” shows also different results of this variable. Say shows 13.8 respective 2.3 and Think 13.799999999999999 respective 2.3000000000000003
Scratch isn't wrong. Scratch is just rounding off the variables for some reason.
Not sure how to overcome this, perhaps the variables may not have enough bytes?
It is not necessary to round something because 2.3*6 is always exactly 13.8.
The results of multiplication and division are definitely wrong.
- ResExsention
-
New Scratcher
1000+ posts
The operators multiplication and division returns wrong results when calculation with point numbers.
Hi,
The operators multiplication and division returns wrong results when calculation with point numbers.
For example the result of 2.3 x 6 is 13.799999999999999 in place of 13.8.
And reversed the result of 13.8 / 6 is 2.3000000000000003 in place of 2.3.
When I set a variable with this result they shows 13.8 respectively 2.3.
But the length of this variable is 18.
And the blocks “Say” and “Think” shows also different results of this variable. Say shows 13.8 respective 2.3 and Think 13.799999999999999 respective 2.3000000000000003
Scratch isn't wrong. Scratch is just rounding off the variables for some reason.
Not sure how to overcome this, perhaps the variables may not have enough bytes?
It is not necessary to round something because 2.3*6 is always exactly 13.8.
The results of multiplication and division are definitely wrong.
Hm. Interesting, The calculator that comes with DuckDuckGo gives 13.799999999999999. The calculator that comes with Windows gives 13.8. The text programming language C++ gives 13.8.
#include <iostream> int main(){std::cout << 2.3 * 6 << std::endl;}
Google gives 13.8. The online-calculator. com gives 13.8. Speedcrunch gives 13.8.
Hm. Curious.
- Flowermanvista
-
Scratcher
1000+ posts
The operators multiplication and division returns wrong results when calculation with point numbers.
This is actually normal - it's because of the number format Scratch uses, which is a double precision floating point number. Double floats, as they are called, are able to represent a very wide range of numbers from very small to absolutely massive, but only with about 15 digits of accuracy.
The classic example is adding 0.1 + 0.2, which equates to 0.30000000000000004, instead of 0.3 as expected. To paraphrase a Scratcher named TheLogFather, the issue is that the number 0.3 doesn't exist as the computer sees it. If you end up going from Scratch to more serious languages, you'll have to learn to code around these limitations.
Here's a quote from 0.30000000000000004.com, which I think gives a really good explanation of this:
The classic example is adding 0.1 + 0.2, which equates to 0.30000000000000004, instead of 0.3 as expected. To paraphrase a Scratcher named TheLogFather, the issue is that the number 0.3 doesn't exist as the computer sees it. If you end up going from Scratch to more serious languages, you'll have to learn to code around these limitations.
Here's a quote from 0.30000000000000004.com, which I think gives a really good explanation of this:
It's actually pretty simple. When you have a base 10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7. In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals. While, 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is operating in. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer's base 2 (binary) number into a more human readable base 10 number.
- ResExsention
-
New Scratcher
1000+ posts
The operators multiplication and division returns wrong results when calculation with point numbers.
This is actually normal - it's because of the number format Scratch uses, which is a double precision floating point number. Double floats, as they are called, are able to represent a very wide range of numbers from very small to absolutely massive, but only with about 15 digits of accuracy.
The classic example is adding 0.1 + 0.2, which equates to 0.30000000000000004, instead of 0.3 as expected. To paraphrase a Scratcher named TheLogFather, the issue is that the number 0.3 doesn't exist as the computer sees it. If you end up going from Scratch to more serious languages, you'll have to learn to code around these limitations.
Here's a quote from 0.30000000000000004.com, which I think gives a really good explanation of this:It's actually pretty simple. When you have a base 10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7. In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals. While, 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is operating in. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer's base 2 (binary) number into a more human readable base 10 number.
Oh, really? They use doubles for numerical variables?
Nice to know, though sometimes it's a hassle if you're evaluating for the expected number, not a double.
- Scratch_Andy_DE
-
Scratcher
10 posts
The operators multiplication and division returns wrong results when calculation with point numbers.
Great. Now we know the background of this issue. Many thanks for the description.
But for the users it can't be acceptable (cite: “This is actually normal”).
Computer calculate very important things in all areas of our life. And the computer should help the people and not vice versa.
We need to be able to rely on the results. So the development have to ensure that the results are correct and not nearly correct, also when it need more effort. And there are a lot of examples on all platforms they establish that it is possible.
OK, Scratch is a fun development to learn for kids where it doesn't seem to be that important. I think it is however very important. The Scratch kids will be the developer from tomorrow.
But for the users it can't be acceptable (cite: “This is actually normal”).
Computer calculate very important things in all areas of our life. And the computer should help the people and not vice versa.
We need to be able to rely on the results. So the development have to ensure that the results are correct and not nearly correct, also when it need more effort. And there are a lot of examples on all platforms they establish that it is possible.
OK, Scratch is a fun development to learn for kids where it doesn't seem to be that important. I think it is however very important. The Scratch kids will be the developer from tomorrow.
- Scratch_Andy_DE
-
Scratcher
10 posts
The operators multiplication and division returns wrong results when calculation with point numbers.
The solution for the user is actually already implemented in Scratch but unfortunately not complete.
The calculation 2.3 * 6 calculated wrong 13,799999999999999. But when I set the result to a variable they shows right 13.8. Great.
Unfortunately the length of this variable is still 18.
That means, Scratch can provide the right result but it is not consistent.
In the background it calculate still with the wrong result.
I think it is important that this bug have to correct by the Scratch team.
And also have to correct the different shows of the blocks think and say.
The calculation 2.3 * 6 calculated wrong 13,799999999999999. But when I set the result to a variable they shows right 13.8. Great.
Unfortunately the length of this variable is still 18.
That means, Scratch can provide the right result but it is not consistent.
In the background it calculate still with the wrong result.
I think it is important that this bug have to correct by the Scratch team.
And also have to correct the different shows of the blocks think and say.
- Flowermanvista
-
Scratcher
1000+ posts
The operators multiplication and division returns wrong results when calculation with point numbers.
It's not a bug, it's just a well known side effect, and it's an important thing for kids to know when developing, since double floats are really the only practical kind of representation for decimal numbers. Trying to use anything else would require an impractical amount of memory and/or computing power. As such, it wouldn't be practical to “fix” this “bug”. As said before, kids should also know this when developing programs in “real” languages, because double floats are used pretty much everywhere.
- Discussion Forums
- » Bugs and Glitches
-
» The operators multiplication and division returns wrong results when calculation with point numbers.


