This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fail at divide a negative integer number for a positive integer number
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, marcosthomazs, martin.panter
Priority: normal Keywords:

Created on 2017-03-15 07:56 by marcosthomazs, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg289647 - (view) Author: Marcos Thomaz (marcosthomazs) Date: 2017-03-15 07:56
At divide a negative integer number for a positive integer number, the result is wrong. For example, in operation:

a, b, c = -7, 2, 7
d = divmod(a, b)
print a//b, a%b, c[0], c // b, c%b

The values printed are -4  1 3 1
msg289648 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-03-15 08:16
If you ignore the c[0] argument, the rest looks fine to me. See the documentation at <https://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations> and <https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex>.

The double-slash operator is called “floor division”. If you are expecting some other rounding, see <https://docs.python.org/2/faq/programming.html#why-does-22-10-return-3>.
msg289655 - (view) Author: Marcos Thomaz (marcosthomazs) Date: 2017-03-15 09:39
I'm sorry, but was a typing error. Try this operations:

a, b, c = 7, -7, 2
print "1:", a // c, a % c
print "2:", b // c, b % c

the result is:
1: 3 1
2: -4 1

The division is the same, except by the signal (variable b is negative, but both, variables "a" and "b" are integers).
msg289656 - (view) Author: Marcos Thomaz (marcosthomazs) Date: 2017-03-15 09:47
Try this operations, in interactive environment:

>> 7 // 2
3
>> -7 // 2
-4
msg289659 - (view) Author: Marcos Thomaz (marcosthomazs) Date: 2017-03-15 09:55
Note that mathematic expression is wrong. -7 divided by 2 is -3, not -4
msg289665 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-03-15 10:39
> -7 divided by 2 is -3, not -4

Integer division in Python is floor division, and it's self-consistent with the implementation of the modulo operation such that the following identity is satisfied: (a % n) == a - n * (a // n). For example: 

    (-7 % 2) == -7 - 2 * (-7 // 2)
           1 == -7 - 2 * (-4)
             == -7 + 8

This behavior is consistent with mathematical analysis languages such as MATLAB, Mathematica, Mathcad, and R. It's also consistent with Ruby, Perl, and Tcl. However, it's different from C, C++, C#, Java, PHP and many other languages. See the following Wikipedia article:

https://en.wikipedia.org/wiki/Modulo_operation

Please do not change the status and resolution of this issue again. This is not a bug.
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74001
2017-03-15 10:39:48eryksunsetstatus: open -> closed

type: crash -> behavior
versions: - Python 3.3
nosy: + eryksun

messages: + msg289665
resolution: rejected -> not a bug
2017-03-15 09:55:41marcosthomazssettype: behavior -> crash
resolution: rejected
messages: + msg289659
2017-03-15 09:47:04marcosthomazssetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg289656

versions: + Python 3.3
2017-03-15 09:39:44marcosthomazssetmessages: + msg289655
2017-03-15 08:16:52martin.pantersetstatus: open -> closed

type: crash -> behavior
versions: - Python 3.3, Python 3.4
nosy: + martin.panter

messages: + msg289648
resolution: not a bug
stage: resolved
2017-03-15 07:56:40marcosthomazscreate