[Python-checkins] cpython: Issue 19933: Provide default argument for ndigits in round. Patch by Vajrasky

steve.dower python-checkins at python.org
Wed Apr 15 22:16:17 CEST 2015


https://hg.python.org/cpython/rev/e3cc75b1000b
changeset:   95678:e3cc75b1000b
user:        Steve Dower <steve.dower at microsoft.com>
date:        Wed Apr 15 16:10:59 2015 -0400
summary:
  Issue 19933: Provide default argument for ndigits in round. Patch by Vajrasky Kok.

files:
  Doc/library/functions.rst |  4 ++--
  Lib/test/test_float.py    |  8 ++++++++
  Misc/NEWS                 |  3 +++
  Objects/floatobject.c     |  5 +++--
  4 files changed, 16 insertions(+), 4 deletions(-)


diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -1225,8 +1225,8 @@
 .. function:: round(number[, ndigits])
 
    Return the floating point value *number* rounded to *ndigits* digits after
-   the decimal point.  If *ndigits* is omitted, it defaults to zero. Delegates
-   to ``number.__round__(ndigits)``.
+   the decimal point.  If *ndigits* is omitted, it returns the nearest integer
+   to its input.  Delegates to ``number.__round__(ndigits)``.
 
    For the built-in types supporting :func:`round`, values are rounded to the
    closest multiple of 10 to the power minus *ndigits*; if two multiples are
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -773,6 +773,14 @@
             test(sfmt, NAN, ' nan')
             test(sfmt, -NAN, ' nan')
 
+    def test_None_ndigits(self):
+        for x in round(1.23), round(1.23, None), round(1.23, ndigits=None):
+            self.assertEqual(x, 1)
+            self.assertIsInstance(x, int)
+        for x in round(1.78), round(1.78, None), round(1.78, ndigits=None):
+            self.assertEqual(x, 2)
+            self.assertIsInstance(x, int)
+
 
 # Beginning with Python 2.6 float has cross platform compatible
 # ways to create and represent inf and nan
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@
 Library
 -------
 
+- Issue 19933: Provide default argument for ndigits in round. Patch by
+  Vajrasky Kok.
+
 - Issue #23193: Add a numeric_owner parameter to
   tarfile.TarFile.extract and tarfile.TarFile.extractall. Patch by
   Michael Vogt and Eric Smith.
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -986,8 +986,9 @@
     x = PyFloat_AsDouble(v);
     if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
         return NULL;
-    if (o_ndigits == NULL) {
-        /* single-argument round: round to nearest integer */
+    if (o_ndigits == NULL || o_ndigits == Py_None) {
+        /* single-argument round or with None ndigits:
+         * round to nearest integer */
         rounded = round(x);
         if (fabs(x-rounded) == 0.5)
             /* halfway case: round to even */

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


More information about the Python-checkins mailing list