Search code examples
pythonpython-3.xmatharithmetic-expressionsinteger-division

Integer Division in Negative Numbers in Python


I'm testing big integers in Python; they are implemented as an object with sign and an array of digits. It's, basically, to describe Karatsuba Multiplication, and, for those big integers, I need the same behaviour like oridinary numbers with integer division by 10, and, there is a problem:
Why, in Python, -22 // 10 = -3?


Solution

  • Dividing by // is a floor division.

    Floor division goes to the lower number without a .

    • 22 // 10 results to the next lower value 2.

    • -22 // 10 results to the next lower value -3

    To do a normal division you can run -22 / 10 This results into

    - 2.2