Lua math question: how to get rid of tiny reminder

The task is to check if the sum of angles of the triangle equals 180. After trigonometric calculations and summing the final result most likely there will be a result something like 180.0000000000000000001. Such a tiny bit is enough for the app to say that the triangle is not valid.
What is interesting:
print(summ_int) will give “180” in the console,
but
print(summ_int-180) will reveal this tiny thing as -2.8421709430404e-14 or any other…

At the moment I am using a work-around to get rid of this bit with the following two lines:

summ_int = tostring(summ_int)
summ_int = tonumber(summ_int)

I wonder is there a proper way of doing so?

Would summ_int = math.round(summ_int) work? I’m not sure if that would remove the additional digits, or if you’d still have the same representation of the integer under the hood, meaning the .0000000000000000001 remains.

This is a general issue with floats, where you can end up with if (0.1+0.2 == 0.3) then returning false due to those “hidden” decimal places.

Another approach is to measure the difference, for instance:

-- Used to prevent double float precision errors when comparing values to zero (0).
local zero = 1e-9

if math.abs(number1 - number2) < zero then
    ....
end

You can determine how precise you want the comparison to be.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.