diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-11-09 17:45:40 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-11-09 17:45:40 (GMT) |
commit | f263724ea0e97de8644a651f4d78926fa995e209 (patch) | |
tree | f543d43679e60bd24c0211d81e32ef91d613f198 /Python | |
parent | c747d3a5d281226af5e6898ec9ab629d2b3520f0 (diff) | |
download | cpython-f263724ea0e97de8644a651f4d78926fa995e209.zip cpython-f263724ea0e97de8644a651f4d78926fa995e209.tar.gz cpython-f263724ea0e97de8644a651f4d78926fa995e209.tar.bz2 |
Issue #7070: Fix problem with builtin round function for large odd
integer arguments. Also fixes the sign of round(-0.0).
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 015e16b..87da3c9 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 |