]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #9599: Tweak loghelper algorithm to return slightly improved results for power...
authorMark Dickinson <dickinsm@gmail.com>
Tue, 28 Sep 2010 07:22:27 +0000 (07:22 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 28 Sep 2010 07:22:27 +0000 (07:22 +0000)
Misc/NEWS
Modules/mathmodule.c

index 1ed5172a168257acc08d6693c65ddc506ee47d4b..beedb0c4cc0372f7e1641b76fc440f5e260b8833 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -2201,6 +2201,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #9959: Tweak formula used for computing math.log of an integer,
+  making it marginally more accurate for exact powers of 2.
+
 - Issue #9422: Fix memory leak when re-initializing a struct.Struct object.
 
 - Issue #7900: The getgroups(2) system call on MacOSX behaves rather oddly
index 69e142325d55c0a51ded9ed483db899fc655c41b..3cfb5f7c1822f242c5a22f4d11cfc1e58ad91633 100644 (file)
@@ -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);
     }