lua-users home
lua-l archive

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


On 9/20/2010 8:43 PM, Roberto Ierusalimschy wrote:
Have a look at the Lua reference manual:

	Modulo is defined as

		a % b == a - math.floor(a/b)*b

	That is, it is the remainder of a division that rounds the
	quotient towards minus infinity.

"Defined as" is not the same as "implemented as". [...]

Then have a look at the code (http://www.lua.org/source/5.1/luaconf.h.html):

   #define luai_nummod(a,b)        ((a) - floor((a)/(b))*(b))

As the problem seems to happen only in some platforms, there is a good
chance that something (floor itself?) is messing with the rounding mode.

In libm, the software implementation of floor() seems normal enough. Looking at i387 assembly code in libm, the rounding mode is indeed changed, but it is restored, pretty standard stuff -- no one in the right mind would have left it as changed.

Since there is nothing amiss on Tony Finch's Mac OS version of Lua, I don't think Mac OS or FreeBSD developers would have missed such a simple thing.

I guess that the 'something' is likely to be found in additional libraries or code that the OP is using, like a heavy maths library that can have its floating point modes adjusted and assumes that it is king of its environment.

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia