lua-users home
lua-l archive

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


> > Out of curiosity (apologies if I'm missing something obvious), why isn't % the same as fmod?
The answer below explains why changing it now would create a backward compatibility problem, but doesn't really provide a very satisfying answer to why mod in Lua didn't use fmod all along rather than inventing its own rules for how % should behave for floating point values. My use of the % operator is pretty rare, so I hadn't noticed the curious handling of a negative first operand or infinity, so I guess I don't really care that much, but it was surprising when I noticed it.

> Main reason: % always gives a non-negative result, even for negative x,
> whereas fmod(x,y) gives a result of the same sign as x.
I'm going to have to think a bit to see when I'd want that, but that is a good point in that it's another argument that they are different operations.

> $ lua
> Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> > =-5%3
> 1
> > =math.fmod(-5,3)
> -2
That's actually a bit bizarre since -5 % 3 in C is -2, so now Lua's mod also diverges from C behavior, too.

> So in C the two things are totally different.
Is there a case where double values that happen to contain integer values (no fractional portion) produces a different result than % would, though?

> The longer I use Lua, the more I realize that patches to the source should
> be restricted to C applications with embedded Lua…
Can't argue there. Even though the apps at Adobe using Lua do embed their own compiled versions, I've been conservative about changes to Lua built-ins. Almost all of my own changes to Lua are additions of debugging capabilities that simply are not possible any other way (high performance breakpoints, low overhead sampling profiler, instruction level code coverage, release build symbolication of back traces). 

Once you start down the path of tweaking core behavior, forever will you write your own libraries and bindings. :o)

DT