lua-users home
lua-l archive

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

I was reading old posts about modulo,

Does anyone write Lua code that uses % negative divisor behavior ?

If we really need a minus sign, It seems better to explicitly say it.

floor-mod: 1000 % -71 rewrite as -(1000 % 71)

It may be better if % always return non-negative value, Euclidean way.
So, above situation with an unknown n divisor, we can write -(1000 % n)

Regarding the C behavior of fmod() returning sign of dividend,
there is an advantage that other mod does not have ... accuracy.

Example, fmod(-1, 1e100) = -1

With floor-mod or euclidean-mod, we get result rounded to 1e100
Since it is a modulo, that is same as 0, losing all significant digits.