mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
Use pow() instead of repeated multiplication by 10 in round(x, n).
This commit is contained in:
parent
74e68c751f
commit
6deb1bf83f
1 changed files with 5 additions and 15 deletions
|
@ -771,10 +771,9 @@ float_round(PyObject *v, PyObject *args)
|
||||||
{
|
{
|
||||||
#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
|
#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
|
||||||
double x;
|
double x;
|
||||||
double f;
|
double f = 1.0;
|
||||||
double flr, cil;
|
double flr, cil;
|
||||||
double rounded;
|
double rounded;
|
||||||
int i;
|
|
||||||
int ndigits = UNDEF_NDIGITS;
|
int ndigits = UNDEF_NDIGITS;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|i", &ndigits))
|
if (!PyArg_ParseTuple(args, "|i", &ndigits))
|
||||||
|
@ -783,13 +782,7 @@ float_round(PyObject *v, PyObject *args)
|
||||||
x = PyFloat_AsDouble(v);
|
x = PyFloat_AsDouble(v);
|
||||||
|
|
||||||
if (ndigits != UNDEF_NDIGITS) {
|
if (ndigits != UNDEF_NDIGITS) {
|
||||||
f = 1.0;
|
f = pow(10.0, ndigits);
|
||||||
i = abs(ndigits);
|
|
||||||
while (--i >= 0)
|
|
||||||
f = f*10.0;
|
|
||||||
if (ndigits < 0)
|
|
||||||
x /= f;
|
|
||||||
else
|
|
||||||
x *= f;
|
x *= f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -804,9 +797,6 @@ float_round(PyObject *v, PyObject *args)
|
||||||
rounded = flr;
|
rounded = flr;
|
||||||
|
|
||||||
if (ndigits != UNDEF_NDIGITS) {
|
if (ndigits != UNDEF_NDIGITS) {
|
||||||
if (ndigits < 0)
|
|
||||||
rounded *= f;
|
|
||||||
else
|
|
||||||
rounded /= f;
|
rounded /= f;
|
||||||
return PyFloat_FromDouble(rounded);
|
return PyFloat_FromDouble(rounded);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue