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

Apparently, the following formula gives the correct answer for all
cases:

  { (m) = l_mathop(floor)((a)/(b)); \
    (m) = ((m) == 0) ? ((a) * (b) < 0 ? (a) + (b) : (a)) : (a) - (m)*(b); }

In my machine, it can be up to 5x faster than 'fmod'. ('fmod' time
seems to depend heavily on the parameters.)

-- Roberto