lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Lua does use fmod but it doesn't have exactly the same semantics. See the comment on luai_nummod on limits.h for details.

More exactly, Lua uses this definition:

  { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }

The correction is due to fmod assumes a rounding towards zero in
the division, while Lua rounds towards minus infinite. But the test
adds little to the cost; 'fmod' is the expensive operation here.

Earlier Lua versions used the mathematical definition:

  (m) = ((a) - l_mathop(floor)((a)/(b))*(b))

It is *much* faster than 'fmod', but it gives NaN when 'b' is infinite
(it should give 'a'). I am not sure whether there are other problematic
cases besides 'b' being inf or -inf.

-- Roberto