[Numpy-discussion] numpy.log does not raise exceptions

dpo dominique.orban@gmail....
Sun Aug 28 11:36:27 CDT 2011



Robert Kern-2 wrote:
> 
> The reason we don't raise exceptions by default is because when
> processing large arrays, you usually don't want to cancel the whole
> operation just because some values were out of the domain. You would
> rather get an array with NaNs in the elements that had invalid inputs
> so you can do something useful with the other elements and actually
> track down where the NaNs got their bad inputs. Always raising an
> exception destroys that information.
> 
> That said, if you do want to raise an exception, this is entirely
> configurable.
> 
>   http://docs.scipy.org/doc/numpy/reference/generated/numpy.seterr.html
> 
> [~]
> |1> import numpy as np
> 
> [~]
> |2> np.log([-1])
> Warning: invalid value encountered in log
> array([ nan])
> 
> [~]
> |3> np.seterr(invalid='raise')
> {'divide': 'print', 'invalid': 'print', 'over': 'print', 'under':
> 'ignore'}
> 
> [~]
> |4> np.log([-1])
> ---------------------------------------------------------------------------
> FloatingPointError                        Traceback (most recent call
> last)
> /Users/rkern/<ipython-input-4-6f7031b9c723> in <module>()
> ----> 1 np.log([-1])
> 
> FloatingPointError: invalid value encountered in log
> 

Excellent, thanks. I was hoping it would be configurable.

Dominique
-- 
View this message in context: http://old.nabble.com/numpy.log-does-not-raise-exceptions-tp32348907p32352209.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.



More information about the NumPy-Discussion mailing list