mirror of
https://github.com/python/cpython.git
synced 2025-11-01 10:45:30 +00:00
Revert change 0eb8c182131e:
"""Issue #23517: datetime.timedelta constructor now rounds microseconds to nearest with ties going away from zero (ROUND_HALF_UP), as Python 2 and Python older than 3.3, instead of rounding to nearest with ties going to nearest even integer (ROUND_HALF_EVEN).""" datetime.timedelta uses rounding mode ROUND_HALF_EVEN again.
This commit is contained in:
parent
1638bdfa1a
commit
69cc487df4
4 changed files with 36 additions and 15 deletions
|
|
@ -2149,9 +2149,29 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|||
if (leftover_us) {
|
||||
/* Round to nearest whole # of us, and add into x. */
|
||||
double whole_us = round(leftover_us);
|
||||
int x_is_odd;
|
||||
PyObject *temp;
|
||||
|
||||
whole_us = _PyTime_RoundHalfUp(leftover_us);
|
||||
whole_us = round(leftover_us);
|
||||
if (fabs(whole_us - leftover_us) == 0.5) {
|
||||
/* We're exactly halfway between two integers. In order
|
||||
* to do round-half-to-even, we must determine whether x
|
||||
* is odd. Note that x is odd when it's last bit is 1. The
|
||||
* code below uses bitwise and operation to check the last
|
||||
* bit. */
|
||||
temp = PyNumber_And(x, one); /* temp <- x & 1 */
|
||||
if (temp == NULL) {
|
||||
Py_DECREF(x);
|
||||
goto Done;
|
||||
}
|
||||
x_is_odd = PyObject_IsTrue(temp);
|
||||
Py_DECREF(temp);
|
||||
if (x_is_odd == -1) {
|
||||
Py_DECREF(x);
|
||||
goto Done;
|
||||
}
|
||||
whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd;
|
||||
}
|
||||
|
||||
temp = PyLong_FromLong((long)whole_us);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue