summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-08-31 00:27:03 (GMT)
committerGuido van Rossum <guido@python.org>2007-08-31 00:27:03 (GMT)
commit6deb1bf83f93fc4d0b2433b94a28293e0ae6bc90 (patch)
tree8d4ba92470efad16df8d79344316ce320258e818
parent74e68c751f148d6d41dd74d004ff19f05e244859 (diff)
downloadcpython-6deb1bf83f93fc4d0b2433b94a28293e0ae6bc90.zip
cpython-6deb1bf83f93fc4d0b2433b94a28293e0ae6bc90.tar.gz
cpython-6deb1bf83f93fc4d0b2433b94a28293e0ae6bc90.tar.bz2
Use pow() instead of repeated multiplication by 10 in round(x, n).
-rw-r--r--Objects/floatobject.c20
1 files changed, 5 insertions, 15 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index f74b19d..c060d8b 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -771,10 +771,9 @@ float_round(PyObject *v, PyObject *args)
{
#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
double x;
- double f;
+ double f = 1.0;
double flr, cil;
double rounded;
- int i;
int ndigits = UNDEF_NDIGITS;
if (!PyArg_ParseTuple(args, "|i", &ndigits))
@@ -783,14 +782,8 @@ float_round(PyObject *v, PyObject *args)
x = PyFloat_AsDouble(v);
if (ndigits != UNDEF_NDIGITS) {
- f = 1.0;
- i = abs(ndigits);
- while (--i >= 0)
- f = f*10.0;
- if (ndigits < 0)
- x /= f;
- else
- x *= f;
+ f = pow(10.0, ndigits);
+ x *= f;
}
flr = floor(x);
@@ -798,16 +791,13 @@ float_round(PyObject *v, PyObject *args)
if (x-flr > 0.5)
rounded = cil;
- else if (x-flr == 0.5)
+ else if (x-flr == 0.5)
rounded = fmod(flr, 2) == 0 ? flr : cil;
else
rounded = flr;
if (ndigits != UNDEF_NDIGITS) {
- if (ndigits < 0)
- rounded *= f;
- else
- rounded /= f;
+ rounded /= f;
return PyFloat_FromDouble(rounded);
}