[Python-checkins] cpython: pytime: add _PyTime_Round() helper to factorize code

victor.stinner python-checkins at python.org
Wed Sep 9 23:48:23 CEST 2015


https://hg.python.org/cpython/rev/2b38a6cd010c
changeset:   97837:2b38a6cd010c
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Sep 09 22:28:58 2015 +0200
summary:
  pytime: add _PyTime_Round() helper to factorize code

files:
  Python/pytime.c |  45 ++++++++++++++++--------------------
  1 files changed, 20 insertions(+), 25 deletions(-)


diff --git a/Python/pytime.c b/Python/pytime.c
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -72,6 +72,17 @@
     return rounded;
 }
 
+static double
+_PyTime_Round(double x, _PyTime_round_t round)
+{
+    if (round == _PyTime_ROUND_HALF_EVEN)
+        return _PyTime_RoundHalfEven(x);
+    else if (round == _PyTime_ROUND_CEILING)
+        return ceil(x);
+    else
+        return floor(x);
+}
+
 static int
 _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
                             double denominator, _PyTime_round_t round)
@@ -83,12 +94,7 @@
     floatpart = modf(d, &intpart);
 
     floatpart *= denominator;
-    if (round == _PyTime_ROUND_HALF_EVEN)
-        floatpart = _PyTime_RoundHalfEven(floatpart);
-    else if (round == _PyTime_ROUND_CEILING)
-        floatpart = ceil(floatpart);
-    else
-        floatpart = floor(floatpart);
+    floatpart = _PyTime_Round(floatpart, round);
     if (floatpart >= denominator) {
         floatpart -= denominator;
         intpart += 1.0;
@@ -139,12 +145,7 @@
         volatile double d;
 
         d = PyFloat_AsDouble(obj);
-        if (round == _PyTime_ROUND_HALF_EVEN)
-            d = _PyTime_RoundHalfEven(d);
-        else if (round == _PyTime_ROUND_CEILING)
-            d = ceil(d);
-        else
-            d = floor(d);
+        d = _PyTime_Round(d, round);
         (void)modf(d, &intpart);
 
         *sec = (time_t)intpart;
@@ -255,7 +256,7 @@
 
 static int
 _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
-                        long to_nanoseconds)
+                        long unit_to_ns)
 {
     double err;
     /* volatile avoids optimization changing how numbers are rounded */
@@ -263,14 +264,8 @@
 
     /* convert to a number of nanoseconds */
     d = value;
-    d *= to_nanoseconds;
-
-    if (round == _PyTime_ROUND_HALF_EVEN)
-        d = _PyTime_RoundHalfEven(d);
-    else if (round == _PyTime_ROUND_CEILING)
-        d = ceil(d);
-    else
-        d = floor(d);
+    d *= (double)unit_to_ns;
+    d = _PyTime_Round(d, round);
 
     *t = (_PyTime_t)d;
     err = d - (double)*t;
@@ -283,12 +278,12 @@
 
 static int
 _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
-                   long to_nanoseconds)
+                   long unit_to_ns)
 {
     if (PyFloat_Check(obj)) {
         double d;
         d = PyFloat_AsDouble(obj);
-        return _PyTime_FromFloatObject(t, d, round, to_nanoseconds);
+        return _PyTime_FromFloatObject(t, d, round, unit_to_ns);
     }
     else {
 #ifdef HAVE_LONG_LONG
@@ -305,8 +300,8 @@
                 _PyTime_overflow();
             return -1;
         }
-        *t = sec * to_nanoseconds;
-        if (*t / to_nanoseconds != sec) {
+        *t = sec * unit_to_ns;
+        if (*t / unit_to_ns != sec) {
             _PyTime_overflow();
             return -1;
         }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list