Discuss Scratch

badatprogrammingibe
Scratcher
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
Scratcher
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.
medians
Scratcher
1000+ posts

Automatically fix floating point errors.

I mean, if you add 0.1 + 0.2 to a list, you still get this:
IndexErrorException
Scratcher
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.
BlueCarpetGames
Scratcher
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.

IndexErrorException wrote:

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.
This would also be a good idea.
TheCreatorOfUnTV
Scratcher
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
Scratcher
1000+ posts

Automatically fix floating point errors.

BlueCarpetGames wrote:

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.

IndexErrorException wrote:

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.
This would also be a good idea.
yep
han614698
Scratcher
1000+ posts

Automatically fix floating point errors.

TheCreatorOfUnTV wrote:

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.
What do you suppose “exact amount or 1” would return?
medians
Scratcher
1000+ posts

Automatically fix floating point errors.

han614698 wrote:

TheCreatorOfUnTV wrote:

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.
What do you suppose “exact amount or 1” would return?
1?

Last edited by medians (July 11, 2024 01:44:45)

TheCreatorOfUnTV
Scratcher
1000+ posts

Automatically fix floating point errors.

han614698 wrote:

TheCreatorOfUnTV wrote:

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.
What do you suppose “exact amount or 1” would return?
1. However, what about:
([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
Scratcher
1000+ posts

Automatically fix floating point errors.

medians wrote:

han614698 wrote:

TheCreatorOfUnTV wrote:

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.
What do you suppose “exact amount or 1” would return?
1?
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.
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
Scratcher
1000+ posts

Automatically fix floating point errors.

han614698 wrote:

medians wrote:

han614698 wrote:

TheCreatorOfUnTV wrote:

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.
What do you suppose “exact amount or 1” would return?
1?
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.
If I put in 0.123456, like 6 digits as they said, how would scratch know what the rest of the digits are?
Never mind.
BlueCarpetGames
Scratcher
82 posts

Automatically fix floating point errors.

bump
BlueCarpetGames
Scratcher
82 posts

Automatically fix floating point errors.

bump
Dragonmyflesh
Scratcher
500+ posts

Automatically fix floating point errors.

TheCreatorOfUnTV wrote:

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.
What would this return?
([exact amount v] of ((3) / (9)) :: operators)
medians
Scratcher
1000+ posts

Automatically fix floating point errors.

Dragonmyflesh wrote:

TheCreatorOfUnTV wrote:

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.
What would this return?
([exact amount v] of ((3) / (9)) :: operators)
0.333333
Also, earlier that guy said nevermind

Last edited by medians (July 13, 2024 16:40:24)

BlueCarpetGames
Scratcher
82 posts

Automatically fix floating point errors.

bump
BlueCarpetGames
Scratcher
82 posts

Automatically fix floating point errors.

bump
IndexErrorException
Scratcher
500+ posts

Automatically fix floating point errors.

No, the performance impact is simply too much.
BlueCarpetGames
Scratcher
82 posts

Automatically fix floating point errors.

bump

Powered by DjangoBB