[issue8644] timedelta.total_seconds needlessly inaccurate, especially for negative timedeltas
Mark Dickinson
report at bugs.python.org
Fri May 7 14:24:55 CEST 2010
New submission from Mark Dickinson <dickinsm at gmail.com>:
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.
----------
components: Extension Modules
messages: 105197
nosy: belopolsky, mark.dickinson
priority: normal
severity: normal
stage: needs patch
status: open
title: timedelta.total_seconds needlessly inaccurate, especially for negative timedeltas
type: behavior
versions: Python 2.7, Python 3.2
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8644>
_______________________________________
More information about the Python-bugs-list
mailing list