I didn't read that link Bazzy posted, but anyway....
I agree that
abs( -5 % 7 ) == 2
would be far more useful and more logical.
If you figure... you have
x % y
... then logically
(x-1) % y
would be 1 less (unless x%y was zero, in which case you'd have y-1).
This would jive with how 2's compliment and & work, too. If you figure that x&7 is a "shortcut" for x%8:
1 2 3 4 5 6 7
|
9 & 7 = 1
8 & 7 = 0
7 & 7 = 7
...
1 & 7 = 1
0 & 7 = 0
-1 & 7 =7
|
So logically you'd expect %8 to work the same way, yet...
1 2 3 4 5 6 7
|
9 % 8 = 1
8 % 8 = 0
7 % 8 = 7
...
1 % 8 = 1
0 % 8 = 0
-1 % 8 = 1 // wtf? why not 7?
|
On the other hand....
(a/b)*b + a%b is equal to a. |
This makes some sense, too.
-5%7 = -5 makes perfect sense when you consider the above equation. After all, -5/7 is 0 with a remainder of -5. Unless you want -5/7 to be -1 with a remainder of 2 (but ugh).
Maybe a second operator would be better. Like % for modulus like we have now, and %% for the more "AND-like" approach. I don't know how that would fly with the compiled assembly, though.