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.
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:
-n//d == n//-d
-n%d == n%-d
-n//d == -(n//d)
n//-d == -(n//d)
-
(n + a*d)//d == n//d + a
for any integera
-
(n + a*d)%d == n%d
for any integera
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.
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.
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
...