2 ms·
In general, trying to perform operations which truncate on negative numbers is fraught with peril, because everyone seems to do it differently and you need to l
by saagarjha 7y ago
In general, trying to perform operations which truncate on negative numbers is fraught with peril, because everyone seems to do it differently and you need to look it up. Another one that tends to bite people is the modulus operator where either operand is negative: programming languages tend to differ on what the result should be. For example:
$ echo "print(-7 % 3)" | python3
2
$ echo "print(-7 % -3)" | python3
-1
$ echo "System.out.println(-7 % 3)" | jshell -
-1
$ echo "System.out.println(-7 % -3)" | jshell -
-1
- 0xff00ffee 7y agoWikipedia has a huge list of mod implementations: https://en.wikipedia.org/wiki/Modulo_operation https://en.wikipedia.org/wiki/Modulo_operation
- deleted 7y ago[deleted]