lua-users home
lua-l archive

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

"Patrick Donnelly" <> writes:
>>Well, it goes nicely with math.floor(a/b).
> Well, as I understand it, it was implemented that way in C because
> that's how it's done in the hardware. I don't see why math.floor()
> makes a good reason when Lua could have just used the C expression a %
> b to evaluate the same expression in Lua.

That's the thing though -- they _couldn't_ use the C expression "a % b"
because that only works on integer types, and Lua uses floats for all
numbers (in the default case).

So it seems like David's almost certainly right:  since they couldn't
use the underlying % operator, they just used the "equivalent formula",
"a - b * floor (a / b)", and that gives a slightly different result than
the C integer operator on typical hardware.

[I guess something closer to the usual C value could be obtained by
using "trunc" instead of "floor", but trunc only seems to be defined in
C99, so it's not as portable.]

My books focus on timeless truths.  -- Donald Knuth