Skip to content

[3.13] gh-113841: fix possible undefined division by 0 in _Py_c_pow() (GH-127211) #127216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Nov 24, 2024

Note, that transformed expression is not an equivalent for original one (1/exp(-x) != exp(x) in general for floating-point numbers). Though, the difference seems to be ~1ULP for good libm implementations.

It's more interesting why division was used from beginning. Closest algorithm I've found (no error checks, of course;)) - it's Algorithm 190 from ACM: https://dl.acm.org/doi/10.1145/366663.366679. It uses subtraction in the exponent.

(cherry picked from commit f7bb658)

Co-authored-by: Sergey B Kirpichev skirpichev@gmail.com

…ythonGH-127211)

`x**y == 1/x**-y ` thus changing `/=` to `*=` by negating the exponent.
(cherry picked from commit f7bb658)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@Yhg1s
Copy link
Member

Yhg1s commented Dec 2, 2024

@gpshead do you still want to change the commit message in this backport (as mentioned in the original PR after merge?)

@skirpichev
Copy link
Member

(Description was updated with a proposal.)

@Yhg1s Yhg1s merged commit f41d8d8 into python:3.13 Dec 2, 2024
39 checks passed
@gpshead gpshead added the needs backport to 3.12 only security fixes label Dec 2, 2024
@miss-islington-app
Copy link

Thanks @miss-islington for the PR, and @Yhg1s for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington added a commit to miss-islington/cpython that referenced this pull request Dec 2, 2024
…_pow() (pythonGH-127211) (pythonGH-127216)

Note, that transformed expression is not an equivalent for original one (1/exp(-x) != exp(x) in general for floating-point numbers). Though, the difference seems to be ~1ULP for good libm implementations.

It's more interesting why division was used from beginning. Closest algorithm I've found (no error checks, of course;)) - it's Algorithm 190 from ACM: https://dl.acm.org/doi/10.1145/366663.366679. It uses subtraction in the exponent.

(cherry picked from commit f7bb658)

(cherry picked from commit f41d8d8)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 2, 2024

GH-127530 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Dec 2, 2024
gpshead pushed a commit that referenced this pull request Dec 2, 2024
…GH-127211) (GH-127216) (GH-127530)

[3.13] gh-113841: fix possible undefined division by 0 in _Py_c_pow() (GH-127211) (GH-127216)

Note, that transformed expression is not an equivalent for original one (1/exp(-x) != exp(x) in general for floating-point numbers). Though, the difference seems to be ~1ULP for good libm implementations.

It's more interesting why division was used from beginning. Closest algorithm I've found (no error checks, of course;)) - it's Algorithm 190 from ACM: https://dl.acm.org/doi/10.1145/366663.366679. It uses subtraction in the exponent.

(cherry picked from commit f7bb658)

(cherry picked from commit f41d8d8)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.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.

4 participants