Skip to content

bpo-44339: Fix math.pow corner case to comply with IEEE 754#26606

Merged
mdickinson merged 9 commits intopython:mainfrom
mdickinson:fix-float-pow-ieee-754-corner-case
Jun 12, 2021
Merged

bpo-44339: Fix math.pow corner case to comply with IEEE 754#26606
mdickinson merged 9 commits intopython:mainfrom
mdickinson:fix-float-pow-ieee-754-corner-case

Conversation

@mdickinson
Copy link
Copy Markdown
Member

@mdickinson mdickinson commented Jun 8, 2021

This PR changes the behaviour of math.pow(0.0, -math.inf) and math.pow(-0.0, -math.inf) to return positive infinity instead of raising ValueError. This makes math.pow consistent with the built-in pow (and the ** operator) for this particular special case, and brings the math.pow special-case handling into compliance with IEEE 754.

Before:

Python 3.11.0a0 (heads/main:257e400a19, Jun  8 2021, 18:00:50) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.pow(0.0, -math.inf)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.pow(-0.0, -math.inf)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

After:

Python 3.11.0a0 (heads/fix-float-pow-ieee-754-corner-case:1be10ea32f, Jun  8 2021, 17:43:05) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.pow(0.0, -math.inf)
inf
>>> math.pow(-0.0, -math.inf)
inf

https://bugs.python.org/issue44339

Comment thread Lib/test/ieee754.txt
Comment thread Lib/test/test_math.py
@bedevere-bot
Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@mdickinson mdickinson merged commit 4a42ceb into python:main Jun 12, 2021
@mdickinson mdickinson deleted the fix-float-pow-ieee-754-corner-case branch June 12, 2021 09:23
jdevries3133 pushed a commit to jdevries3133/cpython that referenced this pull request Jun 19, 2021
…-26606)

Change the behaviour of `math.pow(0.0, -math.inf)` and `math.pow(-0.0, -math.inf)` to return positive infinity instead of raising `ValueError`. This makes `math.pow` consistent with the built-in `pow` (and the `**` operator) for this particular special case, and brings the `math.pow` special-case handling into compliance with IEEE 754.
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.

5 participants