Home python Integer division of negative integers

# Integer division of negative integers

Author

Date

Category

The question arose when solving the palindrome problem.
Here is the solution

``````def palindrom (param):
# convert to str
word = str (param)
# in cycle char by char comparing from start and from end to middle of the word
return True if word [: len (word) // 2] == word [: - len (word) // 2-1: -1] else False
if palindrom (input ("give me a word:")):
print ("Palindrome")
else:
print ("Well, not")
``````

which works for words of even length, but does not work for words with an odd number of characters.
It turned out that `7 // 2 == 3 `, and `-7 // 2 == - 4 `
that is, I solved the problem:

``````return word [: len (word) // 2] == word [:-( len (word) / / 2) -1: -1]
``````

but why is integer division so implemented?
I would like to `-7 // 2 == - 3 `.
What is the deep meaning here? Because

``````7 // 2 + (- 7 // 2) == -1
7 // 2 - 7 // 2 == 0
``````

P.S .: Thanks to gil9red for hints and a quick solution to the palindrome problem in the notes below.
P.P.S .: interestingly, bitwise binary shift operations also follow this rule:

``````7 & gt; & gt; 1 == 3
-7 & gt; & gt; 1 == -4
``````

This is how integer division is defined.

Definition

Divide integer a by integer b! = 0 with remainder means

find two integers q and r that satisfy the following conditions:

1) a = b * q + r;

2) 0 & lt; = r & lt; | b |.

Then the definition will be:

``````- 7 // 2 = -4
``````

Explanation:

1. When dividing a negative number by a positive number, you get a negative number
2. It can be assumed that the correct answer is `-3 `, but in this case, multiplying `-3 * 2 `will get `-6 `. To get the original `-7 `, add the number `-1 `to the result, but the remainder cannot be negative by definition (r & gt; = 0). Therefore, in this case, the remainder is `1 `and the quotient is `-4 `.

Here’s what I got.
As it turned out, the laws of mathematics and the formula in Python for calculating the remainder are different.
The general formula is a% b = r (a = b * q + r), where q is an incomplete quotient, r is the remainder.
For mathematics, in which, according to the rules, the remainder cannot be negative:

``````- 19% 5 = 1 (-19 = 5 * (- 4) +1)
19% -5 = 4 (-21 = 5 * (- 5) +4)
-19% -5 = 1 (-19 = (- 5) * 4 + 1)
-5% 19 = 14 (-5 = 19 * (- 1) +14)
5% -19 = 5 (5 = (- 19) * 0 + 5)
-5% -19 = 14 (-5 = (- 19) * 1 + 14)
``````

For Python, where the remainder can be negative:

``````- 19% 5 = 1 (-19 = 5 * (- 4) +1) converges
19% -5 = -1 (19 = (- 5) * (- 4) -1) does not converge
-19% -5 = -4 (-19 = (- 5) * 3-4) does not converge
-5% 19 = 14 (-5 = 19 * (- 1) +14) converges
5% -19 = -14 (5 = (- 19) * (- 1) -14) does not converge
-5% -19 = -5 (-5 = (- 19) * 0-5) does not converge
``````

Total: incomplete quotient “q” is rounded down, as well as with integer division. Those. if you fit one number in another -3, XX times, take -4, if 3, XX times, take 3, if 0, xx times, take 0. My logic drew it this way)

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.