Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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 9 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion 7 Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ Power and logarithmic functions
.. function:: pow(x, y)

Return ``x`` raised to the power ``y``. Exceptional cases follow
Annex 'F' of the C99 standard as far as possible. In particular,
the IEEE 754 standard as far as possible. In particular,
``pow(1.0, x)`` and ``pow(x, 0.0)`` always return ``1.0``, even
when ``x`` is a zero or a NaN. If both ``x`` and ``y`` are finite,
``x`` is negative, and ``y`` is not an integer then ``pow(x, y)``
Expand All @@ -419,6 +419,11 @@ Power and logarithmic functions
its arguments to type :class:`float`. Use ``**`` or the built-in
:func:`pow` function for computing exact integer powers.

.. versionchanged:: 3.11
The special cases ``pow(0.0, -inf)`` and ``pow(-0.0, -inf)`` were
changed to return ``inf`` instead of raising :exc:`ValueError`,
for consistency with IEEE 754.


.. function:: sqrt(x)

Expand Down
10 changes: 8 additions & 2 deletions 10 Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ string. (Contributed by Sergey B Kirpichev in :issue:`44258`.)
math
----

Add :func:`math.cbrt()`: return the cube root of x.
(Contributed by Ajith Ramachandran in :issue:`44357`.)
* Add :func:`math.cbrt`: return the cube root of x.
(Contributed by Ajith Ramachandran in :issue:`44357`.)

* The behaviour of two :func:`math.pow` corner cases was changed, for
consistency with the IEEE 754 specification. The operations
``math.pow(0.0, -math.inf)`` and ``math.pow(-0.0, -math.inf)`` now return
``inf``. Previously they raised :exc:`ValueError`. (Contributed by Mark
Dickinson in :issue:`44339`.)


Removed
Expand Down
6 changes: 2 additions & 4 deletions 6 Lib/test/ieee754.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,15 @@ infinity and NaN.
1.0

The power of 0 raised to x is defined as 0, if x is positive. Negative
values are a domain error or zero division error and NaN result in a
finite values are a domain error or zero division error and NaN result in a
silent NaN.

>>> pow(0, 0)
1.0
>>> pow(0, INF)
0.0
>>> pow(0, -INF)
Traceback (most recent call last):
...
ValueError: math domain error
inf
>>> 0 ** -1
Traceback (most recent call last):
...
Expand Down
4 changes: 2 additions & 2 deletions 4 Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ def testPow(self):
self.assertRaises(ValueError, math.pow, 0., -2.)
self.assertRaises(ValueError, math.pow, 0., -2.3)
self.assertRaises(ValueError, math.pow, 0., -3.)
self.assertRaises(ValueError, math.pow, 0., NINF)
self.assertEqual(math.pow(0., NINF), INF)
corona10 marked this conversation as resolved.
Show resolved Hide resolved
self.assertTrue(math.isnan(math.pow(0., NAN)))

# pow(INF, x)
Expand All @@ -1256,7 +1256,7 @@ def testPow(self):
self.assertRaises(ValueError, math.pow, -0., -2.)
self.assertRaises(ValueError, math.pow, -0., -2.3)
self.assertRaises(ValueError, math.pow, -0., -3.)
self.assertRaises(ValueError, math.pow, -0., NINF)
self.assertEqual(math.pow(-0., NINF), INF)
self.assertTrue(math.isnan(math.pow(-0., NAN)))

# pow(NINF, x)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Change ``math.pow(±0.0, -math.inf)`` to return ``inf`` instead of raising
``ValueError``. This brings the special-case handling of ``math.pow`` into
compliance with the IEEE 754 standard.
2 changes: 0 additions & 2 deletions 2 Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2810,8 +2810,6 @@ math_pow_impl(PyObject *module, double x, double y)
r = y;
else if (y < 0. && fabs(x) < 1.0) {
r = -y; /* result is +inf */
if (x == 0.) /* 0**-inf: divide-by-zero */
errno = EDOM;
}
else
r = 0.;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.