https://github.com/python/cpython/commit/f0291c3f2df8139870359c7d1d9a4858f19ee7bf
commit: f0291c3f2df8139870359c7d1d9a4858f19ee7bf
branch: main
author: Abhishek Tiwari <[email protected]>
committer: tim-one <[email protected]>
date: 2025-10-23T12:05:12-05:00
summary:

gh-140443: Use `fma` in `loghelper` to improve accuracy of log for very large 
integers (#140469)

* gh-140443:use fma in loghelper to improve accuracy of log for very large 
integers

Use fused multiply-add in log_helper() for huge ints.

Saving a rounding here is remarkably effective. Across some millions
of randomized test cases with ints up to a billion bits, on Windows
and using log10, the ULP error distribution was dramatically
flattened, and its range was nearly cut in half. In fact, the largest
error Tim saw was under 0.6 ULP.

---------

Co-authored-by: abhi210 <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst
M Misc/ACKS
M Modules/mathmodule.c

diff --git a/Misc/ACKS b/Misc/ACKS
index 6876380e0ba8d2..f5f15f2eb7ea24 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1921,6 +1921,7 @@ Tim Tisdall
 Jason Tishler
 Christian Tismer
 Jim Tittsler
+Abhishek Tiwari
 Frank J. Tobin
 James Tocknell
 Bennett Todd
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst
new file mode 100644
index 00000000000000..a1fff8fef7ebe2
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst
@@ -0,0 +1,5 @@
+The logarithm functions (such as :func:`math.log10` and :func:`math.log`) may 
now produce
+slightly different results for extremely large integers that cannot be
+converted to floats without overflow. These results are generally more
+accurate, with reduced worst-case error and a tighter overall error
+distribution.
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index c631beb9ce5477..be88841716b004 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2309,7 +2309,7 @@ loghelper(PyObject* arg, double (*func)(double))
             assert(e >= 0);
             assert(!PyErr_Occurred());
             /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */
-            result = func(x) + func(2.0) * e;
+            result = fma(func(2.0), (double)e, func(x));
         }
         else
             /* Successfully converted x to a double. */

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to