[SciPy-user] Puzzling NAN semantics

Robert Kern robert.kern at gmail.com
Tue Mar 21 19:32:16 CST 2006


Gregory Novak wrote:
> I'm confused by the semantics of nan:
> 
>   def the_test(a,b):
>       print a, " less than ", b, ":", a<b
>       print a, " equals ", b, ":", a==b
>       print a, " greater than ", b, ":",a>b
> 
>   In [85]: the_test(5,6)
>   5  less than  6 : True
>   5  equals  6 : False
>   5  greater than  6 : False
> 
>   In [86]: the_test(5,5)
>   5  less than  5 : False
>   5  equals  5 : True
>   5  greater than  5 : False
> 
> So far so good
> 
>   In [87]: the_test(5,nan)
>   5  less than  nan : False
>   5  equals  nan : True
>   5  greater than  nan : False
> 
> This doens't seem desirable: any number is equal to nan?
> 
>   In [88]: the_test(nan,nan)
>   nan  less than  nan : False
>   nan  equals  nan : True
>   nan  greater than  nan : False
> 
> I believe that the IEEE standard says that nan is _not_ equal to itself
> 
>   In [89]: the_test(array([5,5]), array([5,nan]))
>   [5 5]  less than  [ 5. nan] : [False False]
>   [5 5]  equals  [ 5. nan] : [True False]
>   [5 5]  greater than  [ 5. nan] : [False False]
> 
>   In [90]: the_test(array([nan,nan]), array([5,nan]))
>   [nan nan]  less than  [ 5. nan] : [False False]
>   [nan nan]  equals  [ 5. nan] : [False False]
>   [nan nan]  greater than  [ 5. nan] : [False False]
> 
> When nan appears inside arrays, the behavior is what I expected: nan
> is not equal to, greater than, or less than anything, including
> itself.
> 
> I'm using OS X 10.4.5, Python 2.3.5, IPython 0.6.15, scipy-core 0.9.5,
> scipy 0.4.6, and Numeric 24.2
> 
> Should I consider this a bug?

On OS X 10.4.4:

In [353]: def the_test(a, b):
   .....:   print "%s < %s = %s" % (a, b, a < b)
   .....:   print "%s == %s = %s" % (a, b, a == b)
   .....:   print "%s > %s = %s" % (a, b, a > b)
   .....:

In [357]: the_test(numpy.nan, numpy.nan)
nan < nan = False
nan == nan = False
nan > nan = False

In [361]: the_test(numpy.array([numpy.nan, numpy.nan]), numpy.array([5.,
numpy.nan]))
[        nan         nan] < [ 5.                 nan] = [False False]
[        nan         nan] == [ 5.                 nan] = [False False]
[        nan         nan] > [ 5.                 nan] = [False False]

In [362]: the_test(5, numpy.nan)
5 < nan = False
5 == nan = False
5 > nan = False

In [359]: scipy.__version__
Out[359]: '0.4.7.1607'

In [360]: numpy.__version__
Out[360]: '0.9.6.2148'

-- 
Robert Kern
robert.kern at gmail.com

"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 SciPy-user mailing list