[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Modulo with negative operands
- From: Albert Chan <albertmcchan@...>
- Date: Mon, 22 Apr 2019 08:00:40 -0400
I was reading old posts about modulo, http://lua-users.org/lists/lua-l/2017-02/msg00097.html
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.