Author mark.dickinson
Recipients belopolsky, mark.dickinson
Date 2010-05-07.12:24:53
SpamBayes Score 1.54606e-09
Marked as misclassified No
Message-id <1273235095.12.0.903457246009.issue8644@psf.upfronthosting.co.za>
In-reply-to
Content
I just noticed (while responding to issue 8643) that timedelta.total_seconds() has some accuracy problems, especially for negative timedeltas:

Python 3.2a0 (py3k:80840:80842, May  7 2010, 12:29:35) 
[GCC 4.2.1 (Apple Inc. build 5659)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import timedelta
>>> td = timedelta(microseconds = -123)
>>> td.total_seconds() # expect -0.000123
-0.0001230000052601099

This could be easily fixed by using integer arithmetic internally instead of float arithmetic:

>>> 1e-6 * td.microseconds + td.seconds + 86400 * td.days
-0.0001230000052601099
>>> (td.microseconds + 1000000 * (td.seconds + 86400 * td.days)) / 1000000
-0.000123

This works especially nicely in combination with the new float repr and the improved accuracy of true division in 2.7 / 3.2, to give a total_seconds() whose repr is free of noise digits.  (Well, for small timedeltas, anyway.)

Since total_seconds is new in 2.7 and 3.2, I think it would be good to fix this before the 2.7 release.
History
Date User Action Args
2010-05-07 12:24:55mark.dickinsonsetrecipients: + mark.dickinson, belopolsky
2010-05-07 12:24:55mark.dickinsonsetmessageid: <1273235095.12.0.903457246009.issue8644@psf.upfronthosting.co.za>
2010-05-07 12:24:53mark.dickinsonlinkissue8644 messages
2010-05-07 12:24:53mark.dickinsoncreate