Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Pearu Peterson
On Wed, March 11, 2009 7:50 am, Christopher Barker wrote:

Python does not distinguish between True and
False -- Python makes the distinction between something and nothing.

 In that context, NaN is nothing, thus False.

Mathematically speaking, NaN is a quantity with undefined value. Closer
analysis of a particular case may reveal that it may be some finite number,
or an infinity with some direction, or be intrinsically undefined.
NaN is something that cannot be defined because its value is not unique.
Nothing would be the content of empty set.

Pearu

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Sturla Molden
Charles R Harris wrote:
 It isn't 0 so it should be True. Any disagreement?... Chuck
NaN is not a number equal to 0, so it should be True?

NaN is not a number different from 0, so it should be False?

Also see Pearu's comment.

Why not raise an exception when NaN is evaluated in a boolean context? 
bool(NaN) has no obvious interpretation, so it should be considered an 
error.

Sturla Molden








___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Sturla Molden
Charles R Harris wrote:

 #include math.h
 #include stdio.h

 int main() {
double nan = sqrt(-1);
printf(%f\n, nan);
printf(%i\n, bool(nan));
return 0;
 }

 $ ./nan
 nan
 1


 So resolved, it is True.
Unless specified in the ISO C standard, I'd say this is system and 
compiler dependent.

Should NumPy rely on a specific binary representation of NaN?

A related issue is the boolean value of Inf and -Inf.

Sturla Molden
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Bruce Southey
Sturla Molden wrote:
 Charles R Harris wrote:
   
 #include math.h
 #include stdio.h

 int main() {
double nan = sqrt(-1);
printf(%f\n, nan);
printf(%i\n, bool(nan));
return 0;
 }

 $ ./nan
 nan
 1


 So resolved, it is True.
 
 Unless specified in the ISO C standard, I'd say this is system and 
 compiler dependent.

 Should NumPy rely on a specific binary representation of NaN?

 A related issue is the boolean value of Inf and -Inf.

 Sturla Molden
 ___
 Numpy-discussion mailing list
 Numpy-discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
   
This is one link that shows the different representation of these 
numbers in IEEE 754:
http://www.psc.edu/general/software/packages/ieee/ieee.php
It is a little clearer than Wikipedia:
http://en.wikipedia.org/wiki/IEEE_754-1985

Numpy's nan/NaN/NAN, inf/Inf/PINF, and NINF are not nothing so not zero. 
Also, I think that conversion to an integer should be an error for all 
of these because there is no equivalent representation of these floating 
point numbers as integers and I think that using zero for NaN is wrong.

Now for the other two special representations, I would presume that 
Numpy's PZERO (positive zero) and NZERO (negative zero) are treated as 
nothing. Conversion to integer for these should be zero.

However, I noticed that the standard has just been revised that may 
eventually influence Numpy:
http://en.wikipedia.org/wiki/IEEE_754r
http://en.wikipedia.org/wiki/IEEE_754-2008

Note this defines the min/max behavior:

* |min(x,NaN) = min(NaN,x) = x|
* |max(x,NaN) = max(NaN,x) = x|


Bruce

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Lou Pecora

--- On Wed, 3/11/09, Bruce Southey bsout...@gmail.com wrote:

 From: Bruce Southey bsout...@gmail.com
 Subject: Re: [Numpy-discussion] What is the logical value of nan?
 To: Discussion of Numerical Python numpy-discussion@scipy.org
 Date: Wednesday, March 11, 2009, 10:24 AM

 This is one link that shows the different representation of
 these 
 numbers in IEEE 754:
 http://www.psc.edu/general/software/packages/ieee/ieee.php
 It is a little clearer than Wikipedia:
 http://en.wikipedia.org/wiki/IEEE_754-1985

Thanks.  Useful sites.

 Numpy's nan/NaN/NAN, inf/Inf/PINF, and NINF are not
 nothing so not zero. 

Agreed.  +1

 Also, I think that conversion to an integer should be an
 error for all of these because there is no equivalent 
 representation of these floating 
 point numbers as integers and I think that using zero for
 NaN is wrong.

Another  +1

 Now for the other two special representations, I would
 presume that 
 Numpy's PZERO (positive zero) and NZERO (negative zero)
 are treated as 
 nothing. Conversion to integer for these should be zero.

Yet another  +1.

-- Lou Pecora,   my views are my own.




  
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Charles R Harris
On Wed, Mar 11, 2009 at 8:24 AM, Bruce Southey bsout...@gmail.com wrote:

 Sturla Molden wrote:
  Charles R Harris wrote:
 
  #include math.h
  #include stdio.h
 
  int main() {
 double nan = sqrt(-1);
 printf(%f\n, nan);
 printf(%i\n, bool(nan));
 return 0;
  }
 
  $ ./nan
  nan
  1
 
 
  So resolved, it is True.
 
  Unless specified in the ISO C standard, I'd say this is system and
  compiler dependent.
 
  Should NumPy rely on a specific binary representation of NaN?
 
  A related issue is the boolean value of Inf and -Inf.
 
  Sturla Molden
  ___
  Numpy-discussion mailing list
  Numpy-discussion@scipy.org
  http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 This is one link that shows the different representation of these
 numbers in IEEE 754:
 http://www.psc.edu/general/software/packages/ieee/ieee.php
 It is a little clearer than Wikipedia:
 http://en.wikipedia.org/wiki/IEEE_754-1985

 Numpy's nan/NaN/NAN, inf/Inf/PINF, and NINF are not nothing so not zero.
 Also, I think that conversion to an integer should be an error for all
 of these because there is no equivalent representation of these floating
 point numbers as integers and I think that using zero for NaN is wrong.

 Now for the other two special representations, I would presume that
 Numpy's PZERO (positive zero) and NZERO (negative zero) are treated as
 nothing. Conversion to integer for these should be zero.

 However, I noticed that the standard has just been revised that may
 eventually influence Numpy:
 http://en.wikipedia.org/wiki/IEEE_754r
 http://en.wikipedia.org/wiki/IEEE_754-2008

 Note this defines the min/max behavior:

* |min(x,NaN) = min(NaN,x) = x|
* |max(x,NaN) = max(NaN,x) = x|


We have this behavior in numpy with the fmax/fmin functions.

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Christopher Barker
Sturla Molden wrote:
 Why not raise an exception when NaN is evaluated in a boolean
 context? bool(NaN) has no obvious interpretation, so it should be
 considered an error.

+1

Though there is clearly a lot of legacy around this, so maybe it's best
to follow C convention (sigh).

Bruce Southey wrote:
 Also, I think that conversion to an integer should be an error for
 all of these because there is no equivalent representation of these
 floating point numbers as integers and I think that using zero for
 NaN is wrong.

+1

A silent wrong conversion is MUCH worse than an exception!

As for MATLAB, it was entirely doubles for a long time -- I don't think
it's a good example of well thought-out float-integer interactions.


 Now for the other two special representations, I would presume that 
 Numpy's PZERO (positive zero) and NZERO (negative zero) are treated
 as nothing. Conversion to integer for these should be zero.

+1

 Note this defines the min/max behavior:
 
 * |min(x,NaN) = min(NaN,x) = x| * |max(x,NaN) = max(NaN,x) = x|

nice -- it's nice to have these defined -- of course, who knows how long 
it will be (never?) before compilers/libraries support this.

-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Charles R Harris
On Wed, Mar 11, 2009 at 11:06 AM, Christopher Barker
chris.bar...@noaa.govwrote:

 Sturla Molden wrote:
  Why not raise an exception when NaN is evaluated in a boolean
  context? bool(NaN) has no obvious interpretation, so it should be
  considered an error.

 +1

 Though there is clearly a lot of legacy around this, so maybe it's best
 to follow C convention (sigh).

 Bruce Southey wrote:
  Also, I think that conversion to an integer should be an error for
  all of these because there is no equivalent representation of these
  floating point numbers as integers and I think that using zero for
  NaN is wrong.

 +1

 A silent wrong conversion is MUCH worse than an exception!

 As for MATLAB, it was entirely doubles for a long time -- I don't think
 it's a good example of well thought-out float-integer interactions.


  Now for the other two special representations, I would presume that
  Numpy's PZERO (positive zero) and NZERO (negative zero) are treated
  as nothing. Conversion to integer for these should be zero.

 +1

  Note this defines the min/max behavior:
 
  * |min(x,NaN) = min(NaN,x) = x| * |max(x,NaN) = max(NaN,x) = x|

 nice -- it's nice to have these defined -- of course, who knows how long
 it will be (never?) before compilers/libraries support this.


Raising exceptions in ufuncs is going to take some work as the inner loops
are void functions without any means of indicating an error.  Exceptions
also need to be thread safe. So I am not opposed but it is something for the
future.

Casting seems to be implemented in arraytypes.inc.src as void functions also
without provision for errors. I would also like to see casting implemented
as ufuncs but that is a separate discussion.

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Sturla Molden
Charles R Harris wrote:

 Raising exceptions in ufuncs is going to take some work as the inner 
 loops are void functions without any means of indicating an error.  
 Exceptions also need to be thread safe. So I am not opposed but it is 
 something for the future.

I just saw David Cournapeau's post regarding a NPY_NAN macro. As it uses 
the IEEE754 binary format, at least NPY_NAN should be True in a boolean 
context. So bool(nan) is True then.

And that's what happens now on my computer as well:

  bool(nan)
True

I don't like Python exception's raised inside ufuncs. In the future we 
NumPy might add OpenMP support to ufuncs (multicore CPUs are getting 
common), and Python exceptions would prevent that, or at least make it 
difficult (cf. the GIL).


S.M.

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread Charles R Harris
On Wed, Mar 11, 2009 at 12:19 PM, Sturla Molden stu...@molden.no wrote:

 Charles R Harris wrote:
 
  Raising exceptions in ufuncs is going to take some work as the inner
  loops are void functions without any means of indicating an error.
  Exceptions also need to be thread safe. So I am not opposed but it is
  something for the future.
 
 I just saw David Cournapeau's post regarding a NPY_NAN macro. As it uses
 the IEEE754 binary format, at least NPY_NAN should be True in a boolean
 context. So bool(nan) is True then.

 And that's what happens now on my computer as well:

   bool(nan)
 True

 I don't like Python exception's raised inside ufuncs. In the future we
 NumPy might add OpenMP support to ufuncs (multicore CPUs are getting
 common), and Python exceptions would prevent that, or at least make it
 difficult (cf. the GIL).


I think numpy needs someway to raise these errors, but error handling is
always tricky. Do you have any suggestions as to how you would like to do
it? I was thinking that adding an int return to the loops would provide some
way of indicating errors without specifying how they were to be handled at
this point.

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-11 Thread David Cournapeau
On Thu, Mar 12, 2009 at 3:36 AM, Charles R Harris
charlesr.har...@gmail.com wrote:


 On Wed, Mar 11, 2009 at 12:19 PM, Sturla Molden stu...@molden.no wrote:

 Charles R Harris wrote:
 
  Raising exceptions in ufuncs is going to take some work as the inner
  loops are void functions without any means of indicating an error.
  Exceptions also need to be thread safe. So I am not opposed but it is
  something for the future.
 
 I just saw David Cournapeau's post regarding a NPY_NAN macro. As it uses
 the IEEE754 binary format, at least NPY_NAN should be True in a boolean
 context. So bool(nan) is True then.

 And that's what happens now on my computer as well:

   bool(nan)
 True

 I don't like Python exception's raised inside ufuncs. In the future we
 NumPy might add OpenMP support to ufuncs (multicore CPUs are getting
 common), and Python exceptions would prevent that, or at least make it
 difficult (cf. the GIL).

 I think numpy needs someway to raise these errors, but error handling is
 always tricky. Do you have any suggestions as to how you would like to do
 it? I was thinking that adding an int return to the loops would provide some
 way of indicating errors without specifying how they were to be handled at
 this point.

I think that we should think carefully about how to set up a good
error system within numpy. If we keep adding ad-hoc error handling, I
am afraid it will be hard to read and maintain. We could have
something like:

typedef struct
{
int error ;
const char  *str ;
} ErrorStruct ;

static
ErrorStruct UfuncErrors [] =
{ {CODE1, error 1 string}, ...};

and the related functions to get strings from code. Currently, we
can't really pass errors through several callees because we don't have
a commonly agreed set of errors. If we don't use an errno, I don't
think there are any other options,

David
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Charles R Harris
It isn't 0 so it should be True. Any disagreement?... Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Nadav Horesh
I think it should be considered as roughly (the numerical) equivalent to None, 
therefore False

  Nadav.


-הודעה מקורית-
מאת: numpy-discussion-boun...@scipy.org בשם Charles R Harris
נשלח: ג 10-מרץ-09 21:08
אל: numpy-discussion
נושא: [Numpy-discussion] What is the logical value of nan?
 
It isn't 0 so it should be True. Any disagreement?... Chuck

winmail.dat___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Pauli Virtanen
Tue, 10 Mar 2009 13:08:17 -0600, Charles R Harris wrote:
 It isn't 0 so it should be True. Any disagreement?

+1

Nonzero Python object, hence True. Moreover, it's also True in Python:

 import numpy as np
 type(np.nan)
type 'float'
 bool(np.nan)
True

IMHO, we should follow Python here, otherwise unnecessary confusion may 
arise.

-- 
Pauli Virtanen

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Stéfan van der Walt
2009/3/10 Pauli Virtanen p...@iki.fi:
 Nonzero Python object, hence True. Moreover, it's also True in Python:

Also in C:

#include math.h
#include stdio.h

int main() {
double nan = sqrt(-1);
printf(%f\n, nan);
printf(%i\n, bool(nan));
return 0;
}

$ ./nan
nan
1

Cheers
Stéfan
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Timothy Hochberg
On Tue, Mar 10, 2009 at 2:49 PM, Charles R Harris charlesr.har...@gmail.com
 wrote:



 On Tue, Mar 10, 2009 at 3:16 PM, Stéfan van der Walt ste...@sun.ac.zawrote:

 2009/3/10 Pauli Virtanen p...@iki.fi:
  Nonzero Python object, hence True. Moreover, it's also True in Python:

 Also in C:

 #include math.h
 #include stdio.h

 int main() {
double nan = sqrt(-1);
printf(%f\n, nan);
printf(%i\n, bool(nan));
return 0;
 }

 $ ./nan
 nan
 1


 So resolved, it is True.


I appear to be late to the party, but IMO it should raise an exception in
those cases where it's feasible to do so.


-- 
.  __
.   |-\
.
.  tim.hochb...@ieee.org
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Charles R Harris
On Tue, Mar 10, 2009 at 4:19 PM, Timothy Hochberg tim.hochb...@ieee.orgwrote:



 On Tue, Mar 10, 2009 at 2:49 PM, Charles R Harris 
 charlesr.har...@gmail.com wrote:



 On Tue, Mar 10, 2009 at 3:16 PM, Stéfan van der Walt ste...@sun.ac.zawrote:

 2009/3/10 Pauli Virtanen p...@iki.fi:
  Nonzero Python object, hence True. Moreover, it's also True in Python:

 Also in C:

 #include math.h
 #include stdio.h

 int main() {
double nan = sqrt(-1);
printf(%f\n, nan);
printf(%i\n, bool(nan));
return 0;
 }

 $ ./nan
 nan
 1


 So resolved, it is True.


 I appear to be late to the party, but IMO it should raise an exception in
 those cases where it's feasible to do so.


That also seems reasonable to me. There is also the unresolved issue of
whether casting nan to an integer should raise an exception, currently it is
just converted to 0.

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread David Cournapeau
Charles R Harris wrote:


 On Tue, Mar 10, 2009 at 4:19 PM, Timothy Hochberg
 tim.hochb...@ieee.org mailto:tim.hochb...@ieee.org wrote:



 On Tue, Mar 10, 2009 at 2:49 PM, Charles R Harris
 charlesr.har...@gmail.com mailto:charlesr.har...@gmail.com wrote:



 On Tue, Mar 10, 2009 at 3:16 PM, Stéfan van der Walt
 ste...@sun.ac.za mailto:ste...@sun.ac.za wrote:

 2009/3/10 Pauli Virtanen p...@iki.fi mailto:p...@iki.fi:
  Nonzero Python object, hence True. Moreover, it's also
 True in Python:

 Also in C:

 #include math.h
 #include stdio.h

 int main() {
double nan = sqrt(-1);
printf(%f\n, nan);
printf(%i\n, bool(nan));
return 0;
 }

 $ ./nan
 nan
 1


 So resolved, it is True.


 I appear to be late to the party, but IMO it should raise an
 exception in those cases where it's feasible to do so.


 That also seems reasonable to me. There is also the unresolved issue
 of whether casting nan to an integer should raise an exception,
 currently it is just converted to 0.

I think it is reasonable as well - but I am worried about the
integration with seterr (not just for this case, but in general in our
way toward better handling of this kind of things). I note that matlab
convert nan to 0 as well - presumably they did not handle it besides
what C guarantees (that is not much in that case I believe):

a = nan;
int32(a); % gives 0

in C:

#define _ISOC99_SOURCE   

 

#include
stdio.h   

#include
math.h


 

int
main(void)  
 

{   
 


 

printf(nan is %f\n,
NAN);  
printf(nan is %d\n,
(int)NAN); 
return
0;
}

prints nan and 0 respectively - it may well be implementation dependent,
but it seems that (int)nan simply gives back the nan binary representation.

cheers,

David
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] What is the logical value of nan?

2009-03-10 Thread Christopher Barker


Pauli Virtanen wrote:
 Tue, 10 Mar 2009 13:08:17 -0600, Charles R Harris wrote:
 It isn't 0 so it should be True. Any disagreement?
  - 1
 Nonzero Python object, hence True.

Empty sequences are False also. There was a lot of discussion about all 
this when Guido added Bool types to python. Personally, I don't think 
zero should be false, I think only False (and maybe None) should be 
false -- is it so hard to write: if x != 0:, rather than if x:? but 
there is a LOT of legacy to 0 being False!

Anyway, Laura Creighton wrote a great post about it, with this basic thesis:

   Python does not distinguish between True and
   False -- Python makes the distinction between something and nothing.

In that context, NaN is nothing, thus False.

my $0.02

-Chris

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion