|
Apparently, the following formula gives the correct answer for all>
> More exactly, Lua uses this definition:
>
> { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
>
> 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.
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.)