How to make Python correctly divide?

How to make Python correctly divide ???
It issues -29 // 5 = - 6 ??? and it should be - 5 !!!
It issues -29 % 5 = 1 ??? and it should be - 4 !!!

Python divides correctly. Your answers are also correct.

For integer division involving only positive numbers, everybody agrees you round the result down. When thereā€™s a negative number involved, you have a choice: Do you round down to -āˆž or ā€œdownā€ to 0?* Python does the former. C does the latter. Both approaches have their benefits; Pythonā€™s approach is mathematically nicer in some ways.

You will notice from your example that (-29 // 5) * 5 + (-29 % 5) == -29: the combination of division and remainder is correct.

*Thereā€™s actually a third common approach: round in such a way that the remainder is always positive.

Hi Alexey,

Python is correctly dividing. -29 is -6*5 + 1. Python uses the rule
that the remainder is always the same sign as the denominator.

There is always a choice in how to define truncating division and
remainder, and Pythonā€™s choice is to use the rule explained in the FAQs:

https://docs.python.org/3/faq/programming.html#why-does-22-10-return-3

See also:

which has many useful links to Wikipedia, Guidoā€™s history of the
operator, and more.

If you want division with another set of rules, you will have to use a
function. If you fill in the table, perhaps we can help you write that
function:

Floor division a // b and modulo operator a % b

if a is...      and b is ...     the remainder should be
Positive        Positive         Positive
Positive        Negative         ???
Negative        Positive         ???
Negative        Negative         ???

Pythonā€™s rule is that the remainder is always the same as the
denominator b, but I donā€™t know which rule you want.

The world seems to have lost its mind and forgot how to count.
Iā€™ll try to draw the division!
I think it will be clearer.


After Guido van Rossum joined Microsoft in 2020.
The calculator in Windows began to count incorrectly.
(after version 19)
Before that, he believed correctly, over the years.
It looks like his Monty Python style joke.
:rofl: :rofl: :rofl:

Extraordinarily unclear.

Well, one thing is clear: as you write, -n/d == n/-d == -(n/d). This is fine for division with a rational (or real, or complex) result. What about integer division? Well, integer division has two results - the quotient n//d and the remainder n%d (using Python notation). And we can write down a wishlist of properties we would like the operation to have:

  1. -n//d == n//-d
  2. -n%d == n%-d
  3. -n//d == -(n//d)
  4. n//-d == -(n//d)
  5. (n + a*d)//d == n//d + a for any integer a
  6. (n + a*d)%d == n%d for any integer a

There is no valid definition of integer division that satisfies all of these.

Truncating division (used in C) satisfies 1, 3 and 4, but not 2, 5 and 6. Floor division (used in Python) satisfies 1, 5 and 6, but not 2, 3 and 4. You can also define your division to satisfy 2, but then youā€™ll sacrifice 1.

2 Likes

We know how to count. Division isnā€™t counting.

Also, Microsoft is a company with approximately 181000 employees and

Guido does not work in their calculator division. If you have an issue

with the MS calculator, it has nothing to do with him.

Iā€™m sorry that Python does not follow your understanding of division,

but mathematicians and computer scientists recognise that there are many

ways to define integer division and modulo. They are all correct.

No programming language can satisfy everyone, because different people

expect integer division and modulo to work differently. If you donā€™t

like Pythonā€™s definition, you probably wonā€™t like R, Lua, Mathematica,

Perl, Excel, TCL, or many others that work like Python.

We are happy to help you write a function that implements truncating

division, or Euclidean division, or ceiling division, or whatever form

you want. But weā€™re not being paid to help you. If you are just going

to troll by claiming we canā€™t count, you might find that we get bored

and move on to something else.

Thanks to everyone who answered me. I thoroughly studied everything that was advised to me here.
The integer division rule used in Python is convenient for calculating authentic loan payments. But for my program that works with simple fractions, this is somewhat inconvenient and counterintuitive to understand.

I was amazed that I used three calculators to find the remainder of division and got three different results. :grin:

To solve my problem, I decided to use real division:

...
if b < 0:
    a = -a
    b = -b
quotient = int ( a / b )
remainder = a - b * quotient
...