0

For example why 2 %-8 results in -6?

I was reading What is the result of % in Python? nut I didn't manage to get it, for example I read in comments exaplanation of

7%2 its 7-2=5 and then 5-2 = 3 and then 3-2 = 1 and so thats the result, but can't manage to apply that to

2 - (-8) = 10 - (-8) = 18, can someone explain? and whats the criteria to stop? thanks :)

Barmar
  • 669,327
  • 51
  • 454
  • 560
BugsForBreakfast
  • 662
  • 4
  • 26
  • `2 %-8` has a negative result because it follows the sign of the right modulo operand – Jean-François Fabre Oct 31 '19 at 22:08
  • That question is about the first operand being negative, not the second. – Barmar Oct 31 '19 at 22:08
  • @Barmar: but the [answer](https://stackoverflow.com/a/3883019/6451573) explains just what I commented "Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Your expression yields 3 because". I wasn't going to answer just that (specially because I didn't know about that 5 minutes earlier :)) – Jean-François Fabre Oct 31 '19 at 22:09
  • The answer is in the [third answer](https://stackoverflow.com/a/4432228/5987) to the question you linked, towards the end. – Mark Ransom Oct 31 '19 at 22:10

0 Answers0