Modulo for negative dividends in Python

Looked at other answers and I still don't understand modulo for negative numbers in python

For example, the answer is df

x == (x/y)*y + (x%y) 

therefore, it makes sense that (-2)% 5 = -2 - (-2/5) * 5 = 3

Is it (-2 - (-2/5) * 5) = 0, or am I just crazy? Is working with negative values ​​a strange thing?

Same thing with this negative modulo numbers in python Where did he get -2 from?

Finally, if the sign depends on the dividend, why do negative dividends not have the same result as their positive copies?

For example, the conclusion

 print([8%5,-8%5,4%5,-4%5]) 

there is

 [3, 2, 4, 1] 
+6
source share
6 answers

In Python, modulo is computed according to two rules:

  • (a // b) * b + (a % b) == a and
  • a % b has the same sign as b .

Combine this with the fact that integer division is rounded down (-∞), and the resulting behavior is explained.

If you do -8 // 5 , you get rounded -1.6, which is -2. Multiply this by 5 and you get -10; 2 is the number you must add to this to get -8. Therefore, -8 % 5 is 2.

+9
source

In Python, a // b is defined as floor (a / b), unlike most other languages, where integer division is defined as trunc (a / b). There is a corresponding difference in the interpretation of a % b = a - (a // b) * b .

The reason for this is that the Python definition of the % (and divmod ) operator is usually more useful than the definition of other languages. For instance:

 def time_of_day(seconds_since_epoch): minutes, seconds = divmod(seconds_since_epoch, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) return '%02d:%02d:%02d' % (hours, minutes, seconds) 

Using this function, time_of_day(12345) returns '03:25:45' , as you would expect.

But what time is 12345 seconds before the era? Using the Python definition of divmod , time_of_day(-12345) correctly return '20:34:15' .

What if we override divmod to use the definition of C / and % ?

 def divmod(a, b): q = int(a / b) # I'm using 3.x r = a - b * q return (q, r) 

Now time_of_day(-12345) returns '-3:-25:-45' , which is not a valid time of day. If the standard Python function divmod were implemented this way, you would have to write special code to handle negative inputs. But with dividing the floor style, as in my first example, it just works.

+5
source

The rationale for this is actually the mathematical definition of the smallest remainder . Python respects this definition, while in most other programming languages, the module operator is really more like the "reaminder after division" operator. To calculate the smallest remainder of -5 % 11 , simply add from 11 to -5 until you get a positive integer in the range [0,10] and the result is 6.

+3
source

When you divide ints (-2/5) * 5, it does not evaluate to -2, as it would be in the algebra you are used to. Try to break it into two steps, first evaluating the part in parentheses.

  • (- 2/5) * 5 = (-1) * 5
  • (- 1) * 5 = -5

The reason for step 1 is that you are doing int-division, which in python 2.x returns the equivalent of the result with a floating-point rounding to the nearest integer.

In python 3 and above, 2/5 will return a float, see PEP 238 .

0
source

Check out this BetterExplained article and see @David's comment (No. 6) to see what others are talking about.

Since we work with integers, we do int-division, which in Python will put the answer, not C. Read the Guido article for more on this.

Regarding your question:

 >>> 8 % 5 #B'coz (5*1) + *3* = 8 3 >>> -8 % 5 #B'coz (5*-2) + *2* = -8 2 

Hope this helps. This also confused me at the beginning (this is still the case)! :)

0
source

Say that -a% b needs to be calculated. e.g. r = 11% 10 find the next number after 11, which is perfectly divided by 10, that is, dividing this next number after 11 gives the remainder 0.

In the above case, its 20, which when divided by 10 gives 0. Therefore, 20-11 = 9 is the number that needs to be added to 11.

The concept is, if 60 marbles need to be divided equally between 8 people, in fact, what you get after dividing 60/8 is 7.5, since you cannot reduce half the marble, the next value after 60, which is perfectly divided by 8 , equal to 64 Therefore, 4 more balls must be added to the lot so that everyone shares the same joy from marble.

Here's how Python does it when negative numbers are separated using the module operator.

0
source

Source: https://habr.com/ru/post/912690/


All Articles