New submission from Pierre Denis <[email protected]>:
Python returns inconsistent results when negative infinity is raised to a
non-integer power. This happens with the ** operator as well as with the pow
and math.pow functions. The most blatant symptom occurs with power 0.5, which
is expectedly equivalent to a square root:
>>> float('-inf') ** 0.5
inf
>>> pow(float('-inf'), 0.5)
inf
>>> import math
>>> math.pow(float('-inf'), 0.5)
inf
Mathematically, these operations are invalid if we restrict to real numbers.
Also, if we extend to complex numbers, the results are wrong since the result
should be infj, which is the value returned by cmath.sqrt(float('-inf')).
IMHO, there are three possible ways to fix this:
1) raise an exception ValueError
2) return nan
3) return (nan + nanj)
Discussion:
- Solution 1) is consistent with current handling of *finite* negative base
with non-integer exponent; also, it is consistent with
math.sqrt(float('-inf')), which raises ValueError.
- I expected solution 2) to be more in line with IEEE754 … until I read the
following statement in this specification: "pow(x, y) signals the invalid
operation exception for finite x<0 and finite non-integer y". I’m not an expert
of this topic but I think that there is miss here since IEEE754 does not state
what happens for *infinite* x<0 and finite non-integer y.
- Solution 3) emphasizes the fact that, although the result is generally
undefined, it belongs to complex type.
- In any case, the solution should be consistent also with the case with
negative fractional exponent… even if I would tend to accept that
(float('-inf')**-0.5) == 0.0 is mathematically sensible!
- The test assertions shall be updated in Python standard test suite
(test_float.py).
Note that Python 2.6 behaves consistently for all negative bases, finite or not
finite: it raises ValueError exception with the message "negative number cannot
be raised to a fractional power". The behavior described here seems to be
introduced in this commit:
https://github.com/python/cpython/commit/9ab44b509a935011beb8e9108a2271ee728e8ad4#diff-b7e3652f51768cec742ef07326413ad0
----------
messages: 307256
nosy: pdenis
priority: normal
severity: normal
status: open
title: Inconsistent results for fractional power of -infinity
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32171>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com