]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #25155: Fix _PyTime_Divide() rounding
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 18 Sep 2015 12:21:14 +0000 (14:21 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 18 Sep 2015 12:21:14 +0000 (14:21 +0200)
_PyTime_Divide() rounding was wrong: copy code from Python default which has
now much better unit tests.

Lib/test/test_time.py
Python/pytime.c

index 6bcd2124fa494f796624965ff2476067c1c8fe2c..de0cbc4020f614478cad3f3d5aeaa4d4232d1231 100644 (file)
@@ -946,14 +946,14 @@ class TestPyTime_t(unittest.TestCase):
             # 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 @@ class TestPyTime_t(unittest.TestCase):
             # 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)
index 85e1ca8388a594e33adaf03623d29ca1a6a95927..5a5cdd9c7add4306a83ef79e3949847da85a6748 100644 (file)
@@ -305,17 +305,22 @@ _PyTime_AsNanosecondsObject(_PyTime_t t)
 }
 
 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