[Numpy-discussion] The risks of empty()

Robert Kern robert.kern at gmail.com
Tue Jan 2 19:04:13 EST 2007


Bock, Oliver BGI SYD wrote:
> Some of my arrays are not fully populated.  (I separately record which
> entries are valid.)  I want to use numpy.empty() to speed up the
> creation of these arrays, but I'm worried about what will happen if I
> apply operations to the entire contents of these arrays.  E.g.
> 
> a + b
> 
> I care about the results where valid entries align, but not otherwise.
> Given that numpy.empty() creates an ndarray using whatever junk it finds
> on the heap, it seems to me that there is the possibility that this
> could include bit patterns that are not valid floating point
> representations, which might raise floating point exceptions if used in
> operations like the one above (if they are "signalling" NaNs).  Will
> this be a problem, or will the results of operations on invalid floating
> point numbers yield NaN?
> 
> Or to put it another way: do I need to ensure that array data is
> initialised before using it?

You have essentially full control over floating point exceptions using seterr(),
so you can silence even the signalling NaNs if you want.

  olderrstate = seterr(all='ignore')
  # Do stuff that might generate spurious warnings.
  seterr(**olderrstate)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list