diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-09-28 07:22:27 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-09-28 07:22:27 (GMT) |
commit | fa41e60c9d359b0e24486b884a87fe97505b9153 (patch) | |
tree | 7b193854da452541dae589287415ced508b380b2 /Modules/mathmodule.c | |
parent | d057cd62f7336220ea4d51bc36fcfcffba4879e8 (diff) | |
download | cpython-fa41e60c9d359b0e24486b884a87fe97505b9153.zip cpython-fa41e60c9d359b0e24486b884a87fe97505b9153.tar.gz cpython-fa41e60c9d359b0e24486b884a87fe97505b9153.tar.bz2 |
Issue #9599: Tweak loghelper algorithm to return slightly improved results for powers of 2.
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r-- | Modules/mathmodule.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 69e1423..3cfb5f7 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1572,12 +1572,14 @@ loghelper(PyObject* arg, double (*func)(double), char *funcname) "math domain error"); return NULL; } - /* Special case for log(1), to make sure we get an - exact result there. */ - if (e == 1 && x == 0.5) - return PyFloat_FromDouble(0.0); - /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */ - x = func(x) + func(2.0) * e; + /* 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); } |