Skip to content

Commit 33acf47

Browse files
committed
gh-140443:use fma in loghelper to improve accuracy of log for very large integers
Replaced the expression computing the final result in loghelper with an fma() call: result = fma(func(2.0), (double)e, func(x)); This change reduces rounding error and improves the ULP distribution of math.log10() for very large integer inputs.
1 parent 76fea55 commit 33acf47

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,5 +2161,6 @@ Jelle Zijlstra
21612161
Gennadiy Zlobin
21622162
Doug Zongker
21632163
Peter Åstrand
2164+
Abhishek Tiwari
21642165

21652166
(Entries should be added in rough alphabetical order by last names)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Use a fused multiply–add (fma) in math.log helper to compute the
2+
contribution from the exponent when handling very large integer inputs. This
3+
reduces rounding error (fewer intermediate roundings) and produces a tighter
4+
ULP error distribution for functions like math.log10() and math.log()

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)