[SciPy-dev] python pow versus np.power

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Apr 2 10:11:07 EDT 2009


In stats distribution, I don't want exceptions to be raised for corner
cases. In many places python builtin pow is used which cannot handle
several cases.
Until now I switched to the use of np.power for each individual case
when there where problems.

Is there any strong reason not to change all occurrences of pow to np.power?
It would provide a more robust implementation, even if there is a
small performance hit.

Josef

examples:
>>> np.power(0.0,-2)
inf
>>> pow(0.,-2)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    pow(0.,-2)
ZeroDivisionError: 0.0 cannot be raised to a negative power

>>> pow(np.inf,2)
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    pow(np.inf,2)
OverflowError: (34, 'Result too large')
>>> np.power(np.inf,2)
inf

>>> np.power(np.nan,2)
nan
>>> pow(np.nan,2)
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    pow(np.nan,2)
ValueError: (33, 'Domain error')



More information about the SciPy-Dev mailing list