lua-users home
lua-l archive

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


In message <381725110.20071003215139@gmail.com> you wrote:

> On Wednesday, October 03, 2007 Miles Bader wrote:

> 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.

It depends on how "quotient" and "remainder" are defined. The most
critical criterion for the use of % for arithmetic, programming
circular buffers, etc, is that x%n == y%n should only hold when
x-y is a multiple of n.
It is this aspect which an awful lot of programming languages get wrong.
It amounts to making a fixed choice of representative for each
residue class modulo n and defining x%n to be the representative of the
class that x belongs to.

-- 
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/