summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-05-09 14:42:25 (GMT)
committerGuido van Rossum <guido@python.org>1998-05-09 14:42:25 (GMT)
commit1e162d3753528ef25885f20106512599ebad9b0b (patch)
treebb88757effc3aba352db9f956a09a4cda8b48812 /Python
parentae94cf292ba0317ac70a5eca3004a2fa5bcaad80 (diff)
downloadcpython-1e162d3753528ef25885f20106512599ebad9b0b.zip
cpython-1e162d3753528ef25885f20106512599ebad9b0b.tar.gz
cpython-1e162d3753528ef25885f20106512599ebad9b0b.tar.bz2
Implement round() slightly different, so that for negative ndigits no
additional errors happen in the last step. The trick is to avoid division by 0.1**n -- multiply by 10.0**n instead.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index fd31405..68a5109 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1488,14 +1488,22 @@ builtin_round(self, args)
if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
return NULL;
f = 1.0;
- for (i = ndigits; --i >= 0; )
+ i = abs(ndigits);
+ while (--i >= 0)
f = f*10.0;
- for (i = ndigits; ++i <= 0; )
- f = f*0.1;
+ if (ndigits < 0)
+ x /= f;
+ else
+ x *= f;
if (x >= 0.0)
- return PyFloat_FromDouble(floor(x*f + 0.5) / f);
+ x = floor(x + 0.5);
+ else
+ x = ceil(x - 0.5);
+ if (ndigits < 0)
+ x *= f;
else
- return PyFloat_FromDouble(ceil(x*f - 0.5) / f);
+ x /= f;
+ return PyFloat_FromDouble(x);
}
static PyObject *