Why are the same (5% 2) and (-5% 2) in Python

I guessed that 5% 2 is 1, -5% 2 - -1

But, in Python, I get the same result.

I think this is not a mathematical problem.

>>> -5 % 2 
1  ( I think this should be -1 )
>>>  5 % 2
1

>>> -7 % 6 
5 ( I think this should be -1 )
>>> 7 % 6
1
+4
source share
2 answers

Why? Since the modulo operator is defined this way in python.

The documentation states:

The modulo operator always gives a result with the same sign as its second operand (or zero); [...]

and

The function math.fmod()returns a result whose sign matches the sign of the first argument, [...] Which approach is more dependent on the application.

+2
source

%, , . , m % n Z [n], m, Z [n] - , 0, 1, 2,..., n, n. , , , 0, 0, 1, 2,..., n Z [n].

( ) . , Z [n], . , , , Python % - , n.

( , n) , .

+1

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


All Articles