changeset: 98046:f1cc1f379b00 branch: 3.5 parent: 98044:5bfcccf229c4 user: Victor Stinner date: Fri Sep 18 14:21:14 2015 +0200 files: Lib/test/test_time.py Python/pytime.c description: Issue #25155: Fix _PyTime_Divide() rounding _PyTime_Divide() rounding was wrong: copy code from Python default which has now much better unit tests. diff -r 5bfcccf229c4 -r f1cc1f379b00 Lib/test/test_time.py --- a/Lib/test/test_time.py Fri Sep 18 13:59:09 2015 +0200 +++ b/Lib/test/test_time.py Fri Sep 18 14:21:14 2015 +0200 @@ -946,14 +946,14 @@ # nanoseconds (1, 0, FLOOR), (1, 1, CEILING), - (-1, 0, FLOOR), - (-1, -1, CEILING), + (-1, -1, FLOOR), + (-1, 0, CEILING), # seconds + nanoseconds (1234 * MS_TO_NS + 1, 1234, FLOOR), (1234 * MS_TO_NS + 1, 1235, CEILING), - (-1234 * MS_TO_NS - 1, -1234, FLOOR), - (-1234 * MS_TO_NS - 1, -1235, CEILING), + (-1234 * MS_TO_NS - 1, -1235, FLOOR), + (-1234 * MS_TO_NS - 1, -1234, CEILING), ): with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd): self.assertEqual(PyTime_AsMilliseconds(ns, rnd), ms) @@ -983,14 +983,14 @@ # nanoseconds (1, 0, FLOOR), (1, 1, CEILING), - (-1, 0, FLOOR), - (-1, -1, CEILING), + (-1, -1, FLOOR), + (-1, 0, CEILING), # seconds + nanoseconds (1234 * US_TO_NS + 1, 1234, FLOOR), (1234 * US_TO_NS + 1, 1235, CEILING), - (-1234 * US_TO_NS - 1, -1234, FLOOR), - (-1234 * US_TO_NS - 1, -1235, CEILING), + (-1234 * US_TO_NS - 1, -1235, FLOOR), + (-1234 * US_TO_NS - 1, -1234, CEILING), ): with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd): self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms) diff -r 5bfcccf229c4 -r f1cc1f379b00 Python/pytime.c --- a/Python/pytime.c Fri Sep 18 13:59:09 2015 +0200 +++ b/Python/pytime.c Fri Sep 18 14:21:14 2015 +0200 @@ -305,17 +305,22 @@ } static _PyTime_t -_PyTime_Divide(_PyTime_t t, _PyTime_t k, _PyTime_round_t round) +_PyTime_Divide(const _PyTime_t t, const _PyTime_t k, + const _PyTime_round_t round) { assert(k > 1); if (round == _PyTime_ROUND_CEILING) { if (t >= 0) return (t + k - 1) / k; else + return t / k; + } + else { + if (t >= 0) + return t / k; + else return (t - (k - 1)) / k; } - else - return t / k; } _PyTime_t