Title: modulo operator bug
Type: behavior Stage: resolved
Components: Versions: Python 3.1, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: Sergio.Ĥlutĉin, mark.dickinson, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2010-12-01 11:53 by Sergio.Ĥlutĉin, last changed 2011-04-21 08:50 by mark.dickinson. This issue is now closed.

Messages (10)
msg122991 - (view) Author: Сергей Хлутчин (Sergio.Ĥlutĉin) Date: 2010-12-01 11:53
Result of the modulo operator is wrong:

>>> (-2.22044604925e-16)%4
>>> (-4.0)%4
msg122992 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-12-01 12:24
What results were you expecting here?  Both those results look fine to me (though it's arguable that the second should be +0.0 rather than -0.0).
msg122993 - (view) Author: Сергей Хлутчин (Sergio.Ĥlutĉin) Date: 2010-12-01 13:10
From the documetation:
The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand.
msg122995 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-12-01 14:40
Did you look at the second footnote on that page?
msg123340 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-12-04 12:27
Fixed the sign of the zero (in py3k) in r87032.  I'll backport to 2.7 and 3.1, then close this.

Sergio, is that acceptable?  You still haven't said what results you were expecting for these operations.
msg123344 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-12-04 13:06
Backported to 3.1 (after one botched backport attempt) and 2.7 in r87037 and r87033.
msg123438 - (view) Author: Сергей Хлутчин (Sergio.Ĥlutĉin) Date: 2010-12-05 20:32
Yes i agree,
the first example is the result of rounding error, as well as here:
>>> 4.0-2.22044604925e-16==4.0
msg134081 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-19 17:13
Uncle Timmy, was this the right thing to do?
msg134122 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2011-04-20 04:14
Raymond, Mark pointed to the footnote explaining the first result.  As to the second, Kahan tried his best, but I'm afraid nobody can make me care about the sign bit on a zero ;-)  Whatever Mark thought best is fine by me.
msg134199 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-04-21 08:50
Raymond: just curious---why do you ask?  Did this fix break something?
Date User Action Args
2011-04-21 08:50:06mark.dickinsonsetmessages: + msg134199
2011-04-20 04:14:55tim.peterssetmessages: + msg134122
2011-04-19 17:13:36rhettingersetassignee: mark.dickinson -> tim.peters

messages: + msg134081
nosy: + rhettinger, tim.peters
2010-12-05 20:32:58Sergio.Ĥlutĉinsetmessages: + msg123438
2010-12-04 13:06:28mark.dickinsonsetstatus: open -> closed

messages: + msg123344
2010-12-04 12:27:42mark.dickinsonsetresolution: fixed
stage: resolved
messages: + msg123340
versions: + Python 3.1, Python 2.7, - Python 2.6
2010-12-01 14:40:07mark.dickinsonsetmessages: + msg122995
2010-12-01 13:10:03Sergio.Ĥlutĉinsetmessages: + msg122993
2010-12-01 12:24:27mark.dickinsonsetassignee: mark.dickinson
messages: + msg122992
2010-12-01 12:06:57pitrousetnosy: + mark.dickinson
2010-12-01 11:53:17Sergio.Ĥlutĉincreate