When is min(a, b) != min(b, a)?

Grant Edwards grante at visi.com
Mon Jan 21 11:15:02 EST 2008


On 2008-01-21, Albert Hopkins <marduk at python.invalid> wrote:
> This issue may have been referred to in 
> news:<mailman.1864.1196703799.13605.python-list at python.org> but I didn't 
> entirely understand the explanation.  Basically I have this:
>
>     >>> a = float(6)
>     >>> b = float('nan')
>     >>> min(a, b)
>     6.0
>     >>> min(b, a)
>     nan
>     >>> max(a, b)
>     6.0
>     >>> max(b, a)
>     nan
>
> Before I did not know what to expect, but I certainly didn't expect 
> this.  So my question is what is the min/max of a number and NaN or is it 
> not defined (for which I would have expected either an exception to be 
> raised or NaN returned in each case).

For applications I work on, it should be NaN.  But I think the
result of comparing a Normal to a NaN is undefined, so the
above behavior is allowed by the IEEE spec.

> As a corrollary would I be able to rely on the above behavior or is it 
> subject to change (to fix a bug in min/max perhaps :-)?

According to Wikipedia:

  In the proposed IEEE 754r revision of that standard the same
  rule applies, except that a few anomalous functions (such as
  the maxnum function, which returns the maximum of two
  operands which are expected to be numbers) favour numbers --
  if just one of the operands is a NaN then the value of the
  other operand is returned.

  A different approach has been implemented in the NaN
  'toolbox' for GNU Octave and MATLAB. In that toolbox, NaNs
  are assumed to represent missing values and so the
  statistical functions ignore NaNs in the data instead of
  propagating them. Every computation in the NaN toolbox is
  based on the data values only, which can be useful if it is
  known that NaNs cannot be produced by errors.
  

-- 
Grant Edwards                   grante             Yow! Remember, in 2039,
                                  at               MOUSSE & PASTA will
                               visi.com            be available ONLY by
                                                   prescription!!



More information about the Python-list mailing list