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: Integer division for negative numbers
Type: enhancement Stage:
Components: IDLE Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Nitin.Kumar, georg.brandl, mark.dickinson
Priority: normal Keywords:

Created on 2013-10-30 07:18 by Nitin.Kumar, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Integer_division.py Nitin.Kumar, 2013-10-30 07:22 Examples Showing the Issue
Messages (4)
msg201715 - (view) Author: Nitin Kumar (Nitin.Kumar) Date: 2013-10-30 07:18
Mathematically python is not giving correct output for integer division for negative number, e.g. :  -7//2= -3 but python is giving output -4.
msg201716 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-30 07:30
Hi Nitin,

"a // b" is defined as the floor division operation, same as what "math.floor(a / b)" gives: the largest integer <= a / b.

-7/2 is -3.5, the largest integer <= -3.5 is -4.
msg201719 - (view) Author: Nitin Kumar (Nitin.Kumar) Date: 2013-10-30 09:25
Hi  Georg,

Is there any operator for integer division in python?

On Wed, Oct 30, 2013 at 1:00 PM, Georg Brandl <report@bugs.python.org>wrote:

>
> Georg Brandl added the comment:
>
> Hi Nitin,
>
> "a // b" is defined as the floor division operation, same as what
> "math.floor(a / b)" gives: the largest integer <= a / b.
>
> -7/2 is -3.5, the largest integer <= -3.5 is -4.
>
> ----------
> nosy: +georg.brandl
> resolution:  -> invalid
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue19446>
> _______________________________________
>
msg202837 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-11-14 12:28
Nitin:

> Is there any operator for integer division in python?

Yes, there is: the '//' operator.  :-)

There's no universally agreed upon definition for 'integer division' when negative numbers enter the mix, but I'm guessing that in this case you need something that rounds towards 0 instead of towards -infinity.  There's no dedicated operator for that, but you can simply do (assuming that b is positive):

   -(-a // b) if a < 0 else a // b

References:

[1] http://python-history.blogspot.co.uk/2010/08/why-pythons-integer-division-floors.html
[2] http://docs.python.org/2/faq/programming.html#why-does-22-10-return-3
History
Date User Action Args
2022-04-11 14:57:52adminsetgithub: 63645
2013-11-14 12:28:16mark.dickinsonsetnosy: + mark.dickinson
messages: + msg202837
2013-11-13 21:34:50r.david.murraylinkissue19574 superseder
2013-10-30 09:25:52Nitin.Kumarsetmessages: + msg201719
2013-10-30 07:30:18georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg201716

resolution: not a bug
2013-10-30 07:22:52Nitin.Kumarsetfiles: + Integer_division.py
2013-10-30 07:20:56Nitin.Kumarsetfiles: - Integer_division.py
2013-10-30 07:18:16Nitin.Kumarcreate