lua-users home
lua-l archive

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



How should the % operator be defined for complex numbers, then?  :)

Off the LNUM patch:
<<
/* Complex modulus:
*
* C99 does not provide modulus for complex numbers. We should at least
* give an error here.
*/
lua_Complex luai_vectmod( lua_Complex a, lua_Complex b )
{
return luai_nummod( creal(a), creal(b) ) + luai_nummod( cimag (a), cimag(b) ) * I;
}
<<


Doug Currie kirjoitti 4.10.2007 kello 4:51:

On Wednesday, October 03, 2007 Miles Bader wrote:

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.

Yes, the point is that in programming languages we want to have
matched pairs of "quotient" and "remainder" operations such that

if
q = quotient(a,b)
r = remainder(a,b)
then
a = b * q + r

Lua's % and math.floor make such a pair.

An excellent comparison of some choices in this space is
http://portal.acm.org/citation.cfm?id=128862&coll=portal&dl=ACM
which is summarized at
http://en.wikipedia.org/wiki/Modulo_operation

e

--
Doug Currie
Londonderry, NH, USA