Is there a way of doing modulus in lua? or some mathematical work-around?

I need to find the remainder of a division by 28...

Thanks, TheFalcon

NerdanelVampire: I'm not particularly familiar with lua, but I checked the documentation and found that math.mod is what you want.

http://www.lua.org/manual/5.0/manual.html#5.5 is the rather unhelpful manual page. I suppose the right way to call the modulus function would be math.mod(a, b). You may need to call luaopen_math (mentioned in the beginning of the library section) to gain access to the mathematical library.

You could also try x - x/28*28. The important thing is that this works only if the result of the division is a truncated integer. However I'm not sure how well lua is compliant with that kind of thing. I'm a fan of strongly typed languages myself.

In the worst extremity it is also possible to write one's own modulus function. It shouldn't be very hard (subtract 28 in a loop until the result is smaller than 28), although as lua is interpreted the efficiency would be less than stellar.

NeilStevens: You want lua 4, not 5, I'm 99% sure.

NerdanelVampire: Well, live and learn. The 4.0 manual has lua_mathlibopen and mod without math. before it.

ZizzoTheInfinite: Actually, AFAICT, the lua_mathlibopen function has to be called from which in the C host problem (ToME in this case) for the mod function to be available, and ToME doesn't do so. ToME does, however, define a Lua function imod(), which appears to do what you want.

NeilStevens: I wrote imod, why couldn't I remember it? heh

TheFalcon: Thanks! :)

Module Developers Discussion/Modulus in lua (last edited 2005-09-26 08:20:56 by TheFalcon)