+ 1
Why modulo with negative oprands returns first sign of its expression?
Why modulo with negative oprands returns first sign of its expression? example: int main() { string b= "\n"; cout << (25/7); cout << (-25%-7); cout << (-25%7); cout << (25%-7); return 0; } outputs : 4,-4,-4,4;
9 Answers
+ 18
Expanded version
4 / 3 = 1
4 % 3 = 1
-4 / 3 = -1
-4 % 3 = -1
4 / -3 = -1
4 % -3 = 1
-4 / -3 = 1
-4 % -3 = -1
+ 18
Do simple division on a paper.
E.g.
____
4 ) -3
3 -1
------
1
+ 16
+---------------------------+
| m | d | / | % |
+-----+-----+-----+-----+
| 4 | 3 | 1 | 1 |
+-----+-----+-----+-----+
| -4 | 3 | -1 | -1 |
+-----+-----+-----+-----+
| 4 | -3 | -1 | 1 |
+-----+-----+-----+-----+
| -4 | -3 | 1 | -1 |
+---------------------------+
[https://pasteboard.co/GCmp9GQ.jpg]
+ 16
See the link dear Sakshi.
+ 5
ohhhhhh Thnk u
+ 4
@babak sheykahan (PERS) I didn't understood question and answer also
+ 3
didn't understood can u explain clearly
+ 2
@babak Thanks a lot for your helpful information.
+ 1
it is somewhat implementation dependent
like in c++ (-7%3 == -1)
but in python
(-7%3 == 1)
so it varies with the language using...