diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-05-08 14:35:02 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-05-08 14:35:02 (GMT) |
commit | 0381e3f16a0d390e956c8adbe905d48e92de9cc6 (patch) | |
tree | eda57579d3b4dda56a9e487766ffe849b0372b6d /Modules | |
parent | 161b024b6d2e68295e89f24837a27da599638ea2 (diff) | |
download | cpython-0381e3f16a0d390e956c8adbe905d48e92de9cc6.zip cpython-0381e3f16a0d390e956c8adbe905d48e92de9cc6.tar.gz cpython-0381e3f16a0d390e956c8adbe905d48e92de9cc6.tar.bz2 |
Issue #8644: Improve accuracy of timedelta.total_seconds, by doing intermediate
computations with integer arithmetic instead of floating point.
td.total_seconds() now agrees with td / timedelta(seconds = 1).
Thanks Alexander Belopolsky for the patch.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/datetimemodule.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 83dab2c..d4714bd 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -2211,9 +2211,25 @@ delta_getstate(PyDateTime_Delta *self) static PyObject * delta_total_seconds(PyObject *self) { - return PyFloat_FromDouble(GET_TD_MICROSECONDS(self) / 1000000.0 + - GET_TD_SECONDS(self) + - GET_TD_DAYS(self) * 24.0 * 3600.0); + PyObject *total_seconds; + PyObject *total_microseconds; + PyObject *one_million; + + total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); + if (total_microseconds == NULL) + return NULL; + + one_million = PyLong_FromLong(1000000L); + if (one_million == NULL) { + Py_DECREF(total_microseconds); + return NULL; + } + + total_seconds = PyNumber_TrueDivide(total_microseconds, one_million); + + Py_DECREF(total_microseconds); + Py_DECREF(one_million); + return total_seconds; } static PyObject * |