[Python-checkins] r76179 - in python/branches/release26-maint: Lib/test/test_builtin.py Misc/NEWS Python/bltinmodule.c

mark.dickinson python-checkins at python.org
Mon Nov 9 18:45:40 CET 2009


Author: mark.dickinson
Date: Mon Nov  9 18:45:40 2009
New Revision: 76179

Log:
Issue #7070: Fix problem with builtin round function for large odd
integer arguments.  Also fixes the sign of round(-0.0).


Modified:
   python/branches/release26-maint/Lib/test/test_builtin.py
   python/branches/release26-maint/Misc/NEWS
   python/branches/release26-maint/Python/bltinmodule.c

Modified: python/branches/release26-maint/Lib/test/test_builtin.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_builtin.py	(original)
+++ python/branches/release26-maint/Lib/test/test_builtin.py	Mon Nov  9 18:45:40 2009
@@ -1,5 +1,6 @@
 # Python test set -- built-in functions
 
+import platform
 import test.test_support, unittest
 from test.test_support import fcmp, have_unicode, TESTFN, unlink, \
                               run_unittest, run_with_locale
@@ -1259,6 +1260,14 @@
         self.assertRaises(TypeError, round, t)
         self.assertRaises(TypeError, round, t, 0)
 
+    def test_round_large(self):
+        # Issue #1869: integral floats should remain unchanged
+        self.assertEqual(round(5e15-1), 5e15-1)
+        self.assertEqual(round(5e15), 5e15)
+        self.assertEqual(round(5e15+1), 5e15+1)
+        self.assertEqual(round(5e15+2), 5e15+2)
+        self.assertEqual(round(5e15+3), 5e15+3)
+
     def test_setattr(self):
         setattr(sys, 'spam', 1)
         self.assertEqual(sys.spam, 1)

Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Mon Nov  9 18:45:40 2009
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Issue #7070: Fix round bug for large odd integer arguments.
+
 - Issue #7078: Set struct.__doc__ from _struct.__doc__.
 
 - Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which

Modified: python/branches/release26-maint/Python/bltinmodule.c
==============================================================================
--- python/branches/release26-maint/Python/bltinmodule.c	(original)
+++ python/branches/release26-maint/Python/bltinmodule.c	Mon Nov  9 18:45:40 2009
@@ -2120,7 +2120,7 @@
 static PyObject *
 builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
 {
-	double number;
+	double number, abs_number, abs_result;
 	double f;
 	int ndigits = 0;
 	int i;
@@ -2137,10 +2137,14 @@
 		number /= f;
 	else
 		number *= f;
-	if (number >= 0.0)
-		number = floor(number + 0.5);
-	else
-		number = ceil(number - 0.5);
+
+	/* round `number` to nearest integer, rounding halves away from zero */
+	abs_number = fabs(number);
+	abs_result = floor(abs_number);
+	if (abs_number - abs_result >= 0.5)
+		abs_result += 1.0;
+	number = copysign(abs_result, number);
+
 	if (ndigits < 0)
 		number *= f;
 	else


More information about the Python-checkins mailing list