diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-09-29 19:06:36 +0000 |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-09-29 19:06:36 +0000 |
commit | 67b971dc95b98174253915868c6a3eea60ced268 (patch) | |
tree | 5b7b547135675872df5715d2a698bffbf3376a61 /Modules/mathmodule.c | |
parent | 8e05faf9ce7a31d9a712df121e0fafb016f4df44 (diff) | |
download | cpython-67b971dc95b98174253915868c6a3eea60ced268.tar.gz |
Issue #9599: Further accuracy tweaks to loghelper. For an integer n that's small enough to be converted to a float without overflow, log(n) is now computed as log(float(n)), and similarly for log10.
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r-- | Modules/mathmodule.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 3cfb5f7c18..29c32a30d8 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1562,25 +1562,33 @@ loghelper(PyObject* arg, double (*func)(double), char *funcname) { /* If it is long, do it ourselves. */ if (PyLong_Check(arg)) { - double x; + double x, result; Py_ssize_t e; - x = _PyLong_Frexp((PyLongObject *)arg, &e); - if (x == -1.0 && PyErr_Occurred()) - return NULL; - if (x <= 0.0) { + + /* Negative or zero inputs give a ValueError. */ + if (Py_SIZE(arg) <= 0) { PyErr_SetString(PyExc_ValueError, "math domain error"); return NULL; } - /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. - - It's slightly better to compute the log as log(2 * x) + log(2) * (e - - 1): then when 'arg' is a power of 2, 2**k say, this gives us 0.0 + - log(2) * k instead of log(0.5) + log(2)*(k+1), and so marginally - increases the chances of log(arg, 2) returning the correct result. - */ - x = func(2.0 * x) + func(2.0) * (e - 1); - return PyFloat_FromDouble(x); + + x = PyLong_AsDouble(arg); + if (x == -1.0 && PyErr_Occurred()) { + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) + return NULL; + /* Here the conversion to double overflowed, but it's possible + to compute the log anyway. Clear the exception and continue. */ + PyErr_Clear(); + x = _PyLong_Frexp((PyLongObject *)arg, &e); + if (x == -1.0 && PyErr_Occurred()) + return NULL; + /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */ + result = func(x) + func(2.0) * e; + } + else + /* Successfully converted x to a double. */ + result = func(x); + return PyFloat_FromDouble(result); } /* Else let libm handle it by itself. */ |