[Lua Pit] Floating point number problem

Floating point precision issues:

For example: local number = 0, always add a score such as 1/3, in reality 1/3 * 3 = 1, but the computer will recognize it as 1/3 = 0.3333333... Then add these 3 numbers to get 0.9999999999... so 1/3 * 3 is approximately equal to 1

At this time, if your code writes number >= 1.0, it is not true, but the number you print is 1.0, because Lua will print out 0.99999999...rounded to 1.0 for you to see.

The real data printing method can be math.floor(number * 10000000) / 10000000, that is, multiplying by a large integer, then rounding, and dividing by the large integer to get the real floating point number.

number >= 1.0 does not hold, but you can use number >= 0.99 to solve the problem, 0.99999999... must be greater than or equal to 0.99.

Guess you like

Origin blog.csdn.net/qq_39574690/article/details/113632077