| This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet as we cannot guarantee the safety of other websites. |
| Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (March 2022) |
The variable decimal glitch is a bug in Scratch 2.0 and Scratch 3.0 that may occur when using decimal numbers in calculations. The glitch involves the decimal that the number has showing (for example) a number such as 1.0100000000000002 or 1.0099999999999998, when the number is supposed to be 1.01.
Example
An example of this glitch can be seen with the following script:
when gf clicked set [my variable v] to ((1.1)-([floor v] of (1.1)))
When the green flag is clicked, (my variable) returns 0.10000000000000009 when .
Cause
This bug is caused by the fact that Scratch uses the JavaScript number data type, which stores numbers in floating-point format. At each computation, an approximate result is generated and rounded-off, and the final result is the accumulation of the error at each step.
Issues
This glitch can make (length of(variable)) show the length of a variable to be bigger than it actually is.
Troubleshooting
The effects of round-off error can be mitigated by using the round () block if the desired result is an integer. Another method to fix this is by having a separate variable that only displays the first digits of the number.