Discuss Scratch

Seth_Zaw
Scratcher
100+ posts

Basic operations with decimals converts to repeating ones

Sometimes, when yo multiply a decimal in Scratch, you get a repeating decimal with endless 9s or 0s. For example, 0.2 x 3 is actually 0.6, but in Scratch, it's 0.6000000000000000001. Please fix!

¡Aprendamos español con los Alfabetons!
In my new series, you will cover genders of nouns, important and useful phrases, conjugating verbs and adjectives, regular and irregular words, and more, in Spanish!
Click here to get started!
Visit the Official Alfabetons Website: https://alfabetons.com
CatsUnited
Scratcher
1000+ posts

Basic operations with decimals converts to repeating ones

Basically, what's happening is known as a floating point error since the program isn't able to accurately store the number 0.2. While you usually don't have to worry about stuff like this with Scratch due to the lack of data types (integers, floats etc.), this is a case where it's quite important to know what's going on. Computers with their binary number system aren't that great at storing decimals, much like how we with our decimal system can't accurately show 1/3 as a number without appromixating (e.g 0.33, which isn't actually 1/3 but is close enough).

A way to fix this issue would be to multiply the number by a factor of 10 so that all of the numbers that we care about are on the left side of the decimal place, round the number and then divide it by that factor of 10, which you can make with this script:
((round (((a) * (b)) * (10))) / (10)) // a and b being the two numbers you want to multiply, the 10 shifts the number one place to the left for rounding

bottom text
Seth_Zaw
Scratcher
100+ posts

Basic operations with decimals converts to repeating ones

Also, numbers with more than 15 digits (such as 1928328167543661), numbers whose place is over 15 will be displayed as a 0. Then, it becomes 1928328167543660.

Last edited by Seth_Zaw (Nov. 17, 2019 03:31:18)


¡Aprendamos español con los Alfabetons!
In my new series, you will cover genders of nouns, important and useful phrases, conjugating verbs and adjectives, regular and irregular words, and more, in Spanish!
Click here to get started!
Visit the Official Alfabetons Website: https://alfabetons.com

Powered by DjangoBB