Inaccuracy when using modulo in Lua

I used modulo (%) in Lua just a minute ago, and I noticed that it was somehow very inaccurate. I used it in World of Warcraft, but I would suggest that it is at least slightly updated.

Anyway, using the following example, the output will be 1;

print((0.6/(0.1^(2-1)))%1) 

But when I use the following, it will return 0;

 print((0.6*(10^(2-1)))%1) 

And since I was educated, 0.6 / 0.1 should be equivalent to 0.6 * 10.

This is not a question or a problem, but I'm just interested in this reason. Inaccuracies in mathematics would be very destructive, in my opinion.

+4
source share
1 answer

If you look in the Lua manual, the % operation is defined as

  a % b == a - math.floor(a/b)*b 

So the expression

  6 - math.floor(6/1) * 1 

will be zero.

However, due to floating point inaccuracies ( 0.1 does not exist as a floating point number, the closest should be 0.1000000014... ), these should be your intermediate results:

 > print(math.floor(0.6*10)) 6 > print(math.floor(0.6/.1)) 5 

So you get

 6 - 6 * 1 = 0 6 - 5 * 1 = 1 

additional literature

+6
source

Source: https://habr.com/ru/post/1482774/


All Articles