However, 0.1+0.2 is not actually equal to 0.3, the program below proves this because it does not print ‘equal’.
if 0.1 + 0.2 == 0.3 then
The reason for this is due to floating point approximations, but why does the print not output the ‘true’ value of 0.1+0.2 (showing the floating point error), and how can I view actual representation of 0.1+0.2?
While it is a bit unfortunate that this is a necessity here, it’s best used to avoid these sort of problems.
As long as you know for the most part how many decimal points you’d like to account for, just change the 10s to anything like 100 or 1000.
This is all well and good for this scenario, however my reason for this post is because I am dealing with a program that relies on infinite precision (which obviously isn’t possible in computers) and so I would like to be able to print the actual representation of my values to see where my program is going wrong. Addressing the floating point approximations in my program is not as simple as this unfortunately.
A direct answer to my question needs to address how I can view the true representation of my numbers.
Roblox’s print function rounds numbers to the first ~11 digits, and doesn’t go beyond that, so you won’t see that level of precision unless you explicitly format it yourself:
print(string.format("%.52f",0.1 + 0.2)) 0.3000000000000000444089209850062616169452667236328125