Discuss Scratch

squelz
Scratcher
100+ posts

scratch math with decimals and lists

https://scratch.mit.edu/projects/1262670968/

issue:
scratch has floating point calculations, which i tried to fix by using a number greater or equal to 1 then divide by 10
now it has rounding issues where it says 2.5 but its 2, and where it says 1, its 0
jeffnp
Scratcher
100+ posts

scratch math with decimals and lists

I'm not sure if this will work, but mabye if you try starting the number off as a float (F.E. 0.0 + 2.5 instead of 0+2.5), scratch may be able to add them more accurately.
kansea
Scratcher
500+ posts

scratch math with decimals and lists

First, decide the number of decimal places you want. For accuracy to one decimal place, this can help:

((round (((.1) + (.2)) * (10))) / (10))

Where the (.1 + .2) is whatever gives you the floating point error.

For two decimal places, multiply by 100 then divide by 100 after rounding.

May or may not help fix some of the floating point errors.
squelz
Scratcher
100+ posts

scratch math with decimals and lists

kansea wrote:

First, decide the number of decimal places you want. For accuracy to one decimal place, this can help:

((round (((.1) + (.2)) * (10))) / (10))

Where the (.1 + .2) is whatever gives you the floating point error.

For two decimal places, multiply by 100 then divide by 100 after rounding.

May or may not help fix some of the floating point errors.

jeffnp wrote:

I'm not sure if this will work, but mabye if you try starting the number off as a float (F.E. 0.0 + 2.5 instead of 0+2.5), scratch may be able to add them more accurately.


thanks, but sorry for not being more specific but, in my project im using a list with
which returns 2, not 2.5 like i hoped

replace item ( 1) of [list stats] with [item 1 + (item 2/10) * (item 3/10)]
so its this, specifically i need fixed
g6g6g66g6g
Scratcher
100+ posts

scratch math with decimals and lists

Accuracy issues from floating point usually comes in very small values. A value that is incorrect by an amount of 0.5 is probably caused by something else. Where in the script are you getting 2.5 where you want 2?
kansea
Scratcher
500+ posts

scratch math with decimals and lists

squelz wrote:

kansea wrote:

First, decide the number of decimal places you want. For accuracy to one decimal place, this can help:

((round (((.1) + (.2)) * (10))) / (10))

Where the (.1 + .2) is whatever gives you the floating point error.

For two decimal places, multiply by 100 then divide by 100 after rounding.

May or may not help fix some of the floating point errors.

jeffnp wrote:

I'm not sure if this will work, but mabye if you try starting the number off as a float (F.E. 0.0 + 2.5 instead of 0+2.5), scratch may be able to add them more accurately.


thanks, but sorry for not being more specific but, in my project im using a list with
which returns 2, not 2.5 like i hoped

replace item ( 1) of [list stats] with [item 1 + (item 2/10) * (item 3/10)]
so its this, specifically i need fixed
On the buttons sprite, yes? I see where you wrote a comment, but when I try it, it returns the right number. But item 12 and item 14, both of which you're dividing by 10, are 10, so 10/10 and 10/10 are both = 1. Is there something we need to do to reproduce the problem?
squelz
Scratcher
100+ posts

scratch math with decimals and lists

kansea wrote:

thanks, but sorry for not being more specific but, in my project im using a list with
which returns 2, not 2.5 like i hoped

replace item ( 1) of [list stats] with [item 1 + (item 2/10) * (item 3/10)]
so its this, specifically i need fixed
On the buttons sprite, yes? I see where you wrote a comment, but when I try it, it returns the right number. But item 12 and item 14, both of which you're dividing by 10, are 10, so 10/10 and 10/10 are both = 1. Is there something we need to do to reproduce the problem?

The issue starts happening when item 12 (and or item 2) is higher than 10 where instead of outputting what it shows, on the “replace 1 of currency with currency + (the math)” when putting in “log of” it displays 0 instead
sorry for misinfo, again

Last edited by squelz (Jan. 10, 2026 22:29:24)

Powered by DjangoBB