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.

This commit is contained in:
Mark Dickinson 2010-09-29 19:06:36 +00:00
parent 0c0714f954
commit c60371748b
2 changed files with 30 additions and 15 deletions

View File

@ -641,8 +641,12 @@ class MathTests(unittest.TestCase):
self.ftest('log(32,2)', math.log(32,2), 5) self.ftest('log(32,2)', math.log(32,2), 5)
self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
self.assertEquals(math.log(INF), INF) self.ftest('log(10**1000)', math.log(10**1000),
2302.5850929940457)
self.assertRaises(ValueError, math.log, -1.5)
self.assertRaises(ValueError, math.log, -10**1000)
self.assertRaises(ValueError, math.log, NINF) self.assertRaises(ValueError, math.log, NINF)
self.assertEquals(math.log(INF), INF)
self.assertTrue(math.isnan(math.log(NAN))) self.assertTrue(math.isnan(math.log(NAN)))
def testLog1p(self): def testLog1p(self):
@ -655,8 +659,11 @@ class MathTests(unittest.TestCase):
self.ftest('log10(0.1)', math.log10(0.1), -1) self.ftest('log10(0.1)', math.log10(0.1), -1)
self.ftest('log10(1)', math.log10(1), 0) self.ftest('log10(1)', math.log10(1), 0)
self.ftest('log10(10)', math.log10(10), 1) self.ftest('log10(10)', math.log10(10), 1)
self.assertEquals(math.log(INF), INF) self.ftest('log10(10**1000)', math.log10(10**1000), 1000.0)
self.assertRaises(ValueError, math.log10, -1.5)
self.assertRaises(ValueError, math.log10, -10**1000)
self.assertRaises(ValueError, math.log10, NINF) self.assertRaises(ValueError, math.log10, NINF)
self.assertEquals(math.log(INF), INF)
self.assertTrue(math.isnan(math.log10(NAN))) self.assertTrue(math.isnan(math.log10(NAN)))
def testModf(self): def testModf(self):

View File

@ -1562,25 +1562,33 @@ loghelper(PyObject* arg, double (*func)(double), char *funcname)
{ {
/* If it is long, do it ourselves. */ /* If it is long, do it ourselves. */
if (PyLong_Check(arg)) { if (PyLong_Check(arg)) {
double x; double x, result;
Py_ssize_t e; Py_ssize_t e;
x = _PyLong_Frexp((PyLongObject *)arg, &e);
if (x == -1.0 && PyErr_Occurred()) /* Negative or zero inputs give a ValueError. */
return NULL; if (Py_SIZE(arg) <= 0) {
if (x <= 0.0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"math domain error"); "math domain error");
return NULL; 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 x = PyLong_AsDouble(arg);
- 1): then when 'arg' is a power of 2, 2**k say, this gives us 0.0 + if (x == -1.0 && PyErr_Occurred()) {
log(2) * k instead of log(0.5) + log(2)*(k+1), and so marginally if (!PyErr_ExceptionMatches(PyExc_OverflowError))
increases the chances of log(arg, 2) returning the correct result. return NULL;
*/ /* Here the conversion to double overflowed, but it's possible
x = func(2.0 * x) + func(2.0) * (e - 1); to compute the log anyway. Clear the exception and continue. */
return PyFloat_FromDouble(x); 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. */ /* Else let libm handle it by itself. */