Skip to content

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

Merged
tim-one merged 3 commits into
python:mainfrom
Abhi210:gh-140443-fused-multiply
Oct 23, 2025
Merged

gh-140443: Use fma in loghelper to improve accuracy of log for very large integers#140469
tim-one merged 3 commits into
python:mainfrom
Abhi210:gh-140443-fused-multiply

Conversation

@Abhi210

@Abhi210 Abhi210 commented Oct 22, 2025

Copy link
Copy Markdown
Contributor

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.log for very large integer inputs.

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst Outdated
@tim-one

tim-one commented Oct 22, 2025

Copy link
Copy Markdown
Member

Please add your name to Misc/ACKS.

@Abhi210 Abhi210 force-pushed the gh-140443-fused-multiply branch from de6ba0d to 33acf47 Compare October 22, 2025 18:00
Comment thread Misc/ACKS
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst Outdated
@Abhi210 Abhi210 force-pushed the gh-140443-fused-multiply branch from 33acf47 to 760614c Compare October 22, 2025 18:11
…ery 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.
@Abhi210 Abhi210 force-pushed the gh-140443-fused-multiply branch from 760614c to f05b4b2 Compare October 23, 2025 03:34
@python-cla-bot

python-cla-bot Bot commented Oct 23, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@tim-one tim-one left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a joy! Thank you for your efforts and patience. It's amazing how much "busy work" there is to make a 1-line change 🤣, eh?

It all looks good to me. I'll merge it tomorrow. Perhaps @StanFromIreland knows more than me about what kind of markup decorations are wanted in NEWS entries. I can never remember that stuff ☹️.

@Abhi210

Abhi210 commented Oct 23, 2025

Copy link
Copy Markdown
Contributor Author

Thank you, @tim-one and @StanFromIreland, for your guidance throughout this PR (and thank you for pointing out my mistakes.) I’ve learned a great deal from your feedback and reviews.

@tim-one tim-one linked an issue Oct 23, 2025 that may be closed by this pull request
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-23-26-37.gh-issue-140443.wT5i1A.rst Outdated
…e-140443.wT5i1A.rst

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Comment thread Modules/mathmodule.c
@tim-one tim-one merged commit f0291c3 into python:main Oct 23, 2025
45 checks passed
@Abhi210 Abhi210 deleted the gh-140443-fused-multiply branch October 24, 2025 08:47
StanFromIreland added a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…for very large integers (python#140469)

* pythongh-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 <27881020+Abhi210@users.noreply.github.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use fused multiply add to improve logarithms of bigints

4 participants