Why modulo with negative oprands returns first sign of its expression? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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;

11th Oct 2017, 6:38 AM
Rifat
Rifat - avatar
9 Antworten
+ 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
11th Oct 2017, 7:40 AM
Babak
Babak - avatar
+ 18
Do simple division on a paper. E.g. ____ 4 ) -3 3 -1 ------ 1
11th Oct 2017, 7:44 AM
Babak
Babak - avatar
+ 16
+---------------------------+ | m | d | / | % | +-----+-----+-----+-----+ | 4 | 3 | 1 | 1 | +-----+-----+-----+-----+ | -4 | 3 | -1 | -1 | +-----+-----+-----+-----+ | 4 | -3 | -1 | 1 | +-----+-----+-----+-----+ | -4 | -3 | 1 | -1 | +---------------------------+ [https://pasteboard.co/GCmp9GQ.jpg]
11th Oct 2017, 6:54 AM
Babak
Babak - avatar
+ 16
See the link dear Sakshi.
11th Oct 2017, 7:32 AM
Babak
Babak - avatar
+ 5
ohhhhhh Thnk u
11th Oct 2017, 7:49 AM
SAKSHI
SAKSHI - avatar
+ 4
@babak sheykahan (PERS) I didn't understood question and answer also
11th Oct 2017, 7:31 AM
SAKSHI
SAKSHI - avatar
+ 3
didn't understood can u explain clearly
11th Oct 2017, 7:40 AM
SAKSHI
SAKSHI - avatar
+ 2
@babak Thanks a lot for your helpful information.
13th Oct 2017, 12:13 AM
Rifat
Rifat - avatar
+ 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...
21st Jan 2018, 5:55 AM
TheSickBoy
TheSickBoy - avatar