Log In  

While trying to make a "cycle" function that would wrap the value into a range, I have noticed an odd thing about the "%"-operator -- it seems to be trying to do some wrapping on it's own. And that would be okay (so long as it's documented), however:
1%3 == 1
2%3 == 2
3%3 == 0 (this makes sense)
0%3 == 0
-1%3 == 2
-2%3 == 1 (okay, so it goes back, right?)
-3%3 == 3 (fooled you.)
Such inconsistency (-x%x returning x) can be rater bothersome for multiple use cases, as "%" implies that the return value can not be larger or equal to the second argument.
I suppose that this is due to implementation being something like

return x < 0 ? (x % y + y) : (x % y);

while it should have been around

auto r = x % y;
return r < 0 ? (r + y) : r;
P#10707 2015-05-16 10:25 ( Edited 2015-05-16 22:44)

Nice catch. It is indeed supposed to wrap as the default Lua implementation does. So -3 % 3 should be 0. Fixed for 0.1.1

P#10722 2015-05-16 18:44 ( Edited 2015-05-16 22:44)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 14:14:48 | 0.005s | Q:9