Skip to content

Commit f0291c3

Browse files
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]>
1 parent 918a9ac commit f0291c3

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@ Tim Tisdall
19211921
Jason Tishler
19221922
Christian Tismer
19231923
Jim Tittsler
1924+
Abhishek Tiwari
19241925
Frank J. Tobin
19251926
James Tocknell
19261927
Bennett Todd
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The logarithm functions (such as :func:`math.log10` and :func:`math.log`) may now produce
2+
slightly different results for extremely large integers that cannot be
3+
converted to floats without overflow. These results are generally more
4+
accurate, with reduced worst-case error and a tighter overall error
5+
distribution.

Modules/mathmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ loghelper(PyObject* arg, double (*func)(double))
23092309
assert(e >= 0);
23102310
assert(!PyErr_Occurred());
23112311
/* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */
2312-
result = func(x) + func(2.0) * e;
2312+
result = fma(func(2.0), (double)e, func(x));
23132313
}
23142314
else
23152315
/* Successfully converted x to a double. */

0 commit comments

Comments
 (0)