[Numpy-discussion] RE: [Numpy-developers] Error behavior. (2)

Paul F. Dubois paul at pfdubois.com
Tue Jul 31 11:18:23 EDT 2001


I understand why some people might want the behavior (return with inf or NaN
etc in arrays rather than get an exception).

Let's ask some basic questions. Having done such an operation, how do I tell
if I have any errant values in the result? For example, what can I compare
x[i] to, in order to know if it is invalid? We have had several requests for
full support of IEEE but nobody knows how to do it portably, as far as I
know.

I resist bringing back fastumath because I wonder if it really would work
portably, I don't know the answers to the simpleest questions like the
above, and if it would I know that it made people reluctant to fix bugs or
add new functions because they would have to do it twice. Adding a new
function already requires something like five different places to fix.

Using optional package MA, a user *can* achieve the goal you desire. For
example, if x and y are numpy arrays, and I want to divide them without
possibility of error, I can do:

z = MA.masked_array(x) / MA.masked_array(y)
if MA.getmask(z) is not None:
     print "Your operation failed in one or more components"

The masked_array constructor does not copy its argument. (MAs mix with
Numerics and scalars, so one of the masked_array calls above could be
omitted.)

>From a Windows session:

>>> print x/y
[  1.#INF0000e+000,  2.00000000e+000,  1.50000000e+000,  1.33333333e+000,]
>>> print MA.masked_array(x)/y
[-- ,2.0 ,1.5 ,1.33333333333 ,]
>>> >>> print MA.sqrt(y-2.)
[-- ,-- ,0.0 ,1.0 ,]

I realize this isn't exactly what you had in mind, and it is more expensive
than just letting it rip, but it is reliable and portable. With the infs and
Nans, I'm not sure what you can do with the answer other than print it. It
does place the burden on the programmer to distinguish cases where he wants
to survive a divide by zero from those where he does not, but in my mind
that is a good thing.

Paul







More information about the NumPy-Discussion mailing list