Issue 19446: Integer division for negative numbers (original) (raw)

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:52 admin set github: 63645
2013-11-14 12:28:16 mark.dickinson set nosy: + mark.dickinsonmessages: +
2013-11-13 21:34:50 r.david.murray link issue19574 superseder
2013-10-30 09:25:52 Nitin.Kumar set messages: +
2013-10-30 07:30:18 georg.brandl set status: open -> closednosy: + georg.brandlmessages: + resolution: not a bug
2013-10-30 07:22:52 Nitin.Kumar set files: + Integer_division.py
2013-10-30 07:20:56 Nitin.Kumar set files: - Integer_division.py
2013-10-30 07🔞16 Nitin.Kumar create