[Python-Dev] PyObject_RichCompareBool identity shortcut

Robert Kern robert.kern at gmail.com
Thu Apr 28 18:01:28 CEST 2011


On 4/28/11 12:37 AM, Alexander Belopolsky wrote:
> On Thu, Apr 28, 2011 at 12:33 AM, Robert Kern<robert.kern at gmail.com>  wrote:
>> On 2011-04-27 23:24 , Guido van Rossum wrote:
> ..
>>> So do new masks get created when the outcome of an elementwise
>>> operation is a NaN?
>>
>> No.
>
> Yes.
>
>>>> from MA import array
>>>> print array([0])/array([0])
> [-- ]
>
> (I don't have numpy on this laptop, so the example is using Numeric,
> but I hope you guys did not change that while I was not looking:-)

This behavior is not what you think it is. Rather, some binary operations have 
been augmented with a domain of validity, and the results will be masked out 
when the domain is violated. Division is one of them, and division by zero will 
cause the result to be masked. You can produce NaNs in other ways that will not 
be masked in both numpy and old Numeric:

[~]
|4> minf = np.ma.array([1e300]) * np.ma.array([1e300])
Warning: overflow encountered in multiply

[~]
|5> minf
masked_array(data = [ inf],
              mask = False,
        fill_value = 1e+20)


[~]
|6> minf - minf
masked_array(data = [ nan],
              mask = False,
        fill_value = 1e+20)

[~]
|14> import MA

[~]
|15> minf = MA.array([1e300]) * MA.array([1e300])

[~]
|16> minf
array([              inf,])

[~]
|17> (minf - minf)[0]
nan

[~]
|25> (minf - minf)._mask is None
True


Numeric has a bug where it cannot print arrays with NaNs, so I just grabbed the 
element out instead of showing it. But I guarantee you that it is not masked.

Masked arrays are not a way to avoid NaNs arising from computations. NaN 
handling is an important part of computing with numpy.

-- 
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 Python-Dev mailing list