For some reason, Roblox Studio is not returning the correct answer for 12*0.2. I tried this on multiple places, and I am not sure whether it is something to do with my settings or a Studio bug.
Try it for yourselves and let me know, or let me know if you know the issue here.
As you can see above, it believes that 12*0.2 is not 2.4, but rather 2.4000000000000004, which is leading to strange issues in my code.
This does not happen just in Studio:
This is due to some weird computer stuff. The best you can do is to manually round it to a set amount of decimals.
this a floating point precision error - in short, numbers are effectively floats (any number with a decimal point) and can only hold a set number of significant digits, and what you attempted to do caused a rounding error due to it exceeding the # of digits during calculation.
this is also found when calculating 0.1 + 0.2, which leads to 0.30000000000000004.
one solution is to just do 12 * 2 / 10 (which is effectively the same thing), but the other solutions work aswell
2.4000000000000004 is the exact answer when 0.2 is rounded to the closest floating point value, since 0.2 is not representable in base 2 with any number of digits. Use whole numbers if you need closed multiplication.
To round a number to decimal places to get rid of floating point error try this:
math.round(number or equation you want to round * 100) / 100
You can round to 2 decimal places using 100 in both places, or 1 decimal place using 10s instead.
This is a floating point precision error, and it has to do with how data is stored in computers. If you don’t already know, all data in computers is stored in binary (base 2) (0 or 1), but humans commonly use base 10 (0,1,2,3,4,5,6,7,8,9). This results in us having to store base 10 floating point numbers in base 2 binary.
For example, for a 32 bit floating point number (this means there are a total of 32 ones or zeros):
0.1 would be something like:
00111101110011001100110011001101
0.2 would be something like:
00111110010011001100110011001101
In the conversion between base 10 and base 2, because there is only a finite amount of bits (ones or zeros) that can be used, the computer ends up truncating the number. So in reality, instead of storing 0.1, the number: 0.100000001490116119384765625 is actually stored, and instead of storing 0.2 the number: 0.20000000298023223876953125 is actually stored. Now, when the computer tries to add these two numbers together, you don’t get 0.3 like you would expect, you instead get around 0.300000004 , which can also be observed when you do: