lua-users home
lua-l archive

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


Op Ma. 22 Apr. 2019 om 14:11 het Albert Chan <albertmcchan@yahoo.com> geskryf:

> Does anyone write Lua code that uses % negative divisor behavior ?
>
> If we really need a minus sign, It seems better to explicitly say it.

The manual for Lua 5.3 says:

Floor division (//) is a division that rounds the quotient towards
minus infinity, that is, the floor of the division of its operands.
Modulo is defined as the remainder of a division that rounds the
quotient towards minus infinity (floor division).

I.e. % and // are locked together so that a == b*(a//b)+a%b

Previous versions of Lua did not have //, and the discussiion of % was
accordingly longer and may have been confusing to some readers.

It's quite simple now: the result of a%b always lies in the half-open
range from 0 to b, up or down, with b not included.