summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-11-09 17:45:40 +0000
committerMark Dickinson <dickinsm@gmail.com>2009-11-09 17:45:40 +0000
commitf263724ea0e97de8644a651f4d78926fa995e209 (patch)
treef543d43679e60bd24c0211d81e32ef91d613f198
parentc747d3a5d281226af5e6898ec9ab629d2b3520f0 (diff)
downloadcpython-git-f263724ea0e97de8644a651f4d78926fa995e209.tar.gz
Issue #7070: Fix problem with builtin round function for large odd
integer arguments. Also fixes the sign of round(-0.0).
-rw-r--r--Lib/test/test_builtin.py9
-rw-r--r--Misc/NEWS2
-rw-r--r--Python/bltinmodule.c14
3 files changed, 20 insertions, 5 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index d8e604158c..04c2216765 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -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 @@ class BuiltinTest(unittest.TestCase):
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)
diff --git a/Misc/NEWS b/Misc/NEWS
index 6d42187264..ae1be75729 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6.5
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
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 015e16bbb8..87da3c9fff 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -2120,7 +2120,7 @@ For most object types, eval(repr(object)) == object.");
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 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
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