Discuss Scratch
- Discussion Forums
- » Suggestions
- » Automatically fix floating point errors.
- badatprogrammingibe
-
500+ posts
Automatically fix floating point errors.
People who are starting programming shouldn't have to deal with floating point errors (examples: 0.1 + 0.2 = 0.30000000000000004, cos(90)=6.123233995736766e-17.)
- _nix
-
1000+ posts
Automatically fix floating point errors.
I tried doing this in 3.0 for fun, and it actually wasn't that difficult. I just required the decimal.js library, then converted the operations in scratch-vm/src/blocks/scratch3_operators.js to use the appropriate functions. And bam, instant 0.1 + 0.2 = 0.3. Here are the changes if you're curious.
You should probably create an issue suggesting this in LLK/scratch-vm.
You should probably create an issue suggesting this in LLK/scratch-vm.
- medians
-
1000+ posts
Automatically fix floating point errors.
I mean, if you add 0.1 + 0.2 to a list, you still get this:


- IndexErrorException
-
500+ posts
Automatically fix floating point errors.
Issue is that decimal libraries are slow(I guess Scratch already is) but this isn't a huge deal, a simple little message could be displayed “Math operations with decimals are slightly inaccurate”and site a wiki page explaining the issue for those who are curious.
The inaccuracy to so low it's unnoticeable for all intents and purposes other than displaying, which rounding could be used.
The inaccuracy to so low it's unnoticeable for all intents and purposes other than displaying, which rounding could be used.
- BlueCarpetGames
-
82 posts
Automatically fix floating point errors.
Support. Most people don't even know what a “floating point” is, so encountering these issues is going to be extremely confusing for them. Even if it's not possible to fully prevent them from happening, their appearance should be made less frequent by any means necessary.
This would also be a good idea. a simple little message could be displayed “Math operations with decimals are slightly inaccurate”and site a wiki page explaining the issue for those who are curious.
- TheCreatorOfUnTV
-
1000+ posts
Automatically fix floating point errors.
The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:
([exact amount v] of (9) :: operators)to show the exact amount.
- WindowsAdmin
-
1000+ posts
Automatically fix floating point errors.
yep Support. Most people don't even know what a “floating point” is, so encountering these issues is going to be extremely confusing for them. Even if it's not possible to fully prevent them from happening, their appearance should be made less frequent by any means necessary.This would also be a good idea. a simple little message could be displayed “Math operations with decimals are slightly inaccurate”and site a wiki page explaining the issue for those who are curious.
- han614698
-
1000+ posts
Automatically fix floating point errors.
What do you suppose “exact amount or 1” would return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.
- medians
-
1000+ posts
Automatically fix floating point errors.
1?What do you suppose “exact amount or 1” would return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.
Last edited by medians (July 11, 2024 01:44:45)
- TheCreatorOfUnTV
-
1000+ posts
Automatically fix floating point errors.
1. However, what about:What do you suppose “exact amount or 1” would return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.
([exact amount v] of ((0.1) + (0.2)) :: operators)It would return 0.30000000004 rather than the normal 0.3 due to floating point errors.
Last edited by TheCreatorOfUnTV (July 11, 2024 01:45:41)
- han614698
-
1000+ posts
Automatically fix floating point errors.
My point, even though it makes no sense looking back on it, is that this block works independently, meaning that “exact amount” of any number would return itself.1?What do you suppose “exact amount or 1” would return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.
If I put in 0.123456, like 6 digits as they said, how would scratch know what the rest of the digits are?
Last edited by han614698 (July 11, 2024 01:46:45)
- TheCreatorOfUnTV
-
1000+ posts
Automatically fix floating point errors.
Never mind.My point, even though it makes no sense looking back on it, is that this block works independently, meaning that “exact amount” of any number would return itself.1?What do you suppose “exact amount or 1” would return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.
If I put in 0.123456, like 6 digits as they said, how would scratch know what the rest of the digits are?
- Dragonmyflesh
-
500+ posts
Automatically fix floating point errors.
What would this return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.
([exact amount v] of ((3) / (9)) :: operators)
- medians
-
1000+ posts
Automatically fix floating point errors.
0.333333What would this return? The best idea would be to round by 6 decimal places where visible (it would work the same internally), then add a block like this:([exact amount v] of (9) :: operators)to show the exact amount.([exact amount v] of ((3) / (9)) :: operators)
Also, earlier that guy said nevermind
Last edited by medians (July 13, 2024 16:40:24)
- IndexErrorException
-
500+ posts
Automatically fix floating point errors.
No, the performance impact is simply too much.
- Discussion Forums
- » Suggestions
-
» Automatically fix floating point errors.