[Python-Dev] Weekly Python Bug/Patch Summary
Jeff Epler
jepler at unpythonic.net
Wed Feb 18 18:12:00 EST 2004
[I apologize that I'm not adding this information to the bug report, but
I'm not able to log into SF right now, the login page hangs while loading]
> 1==float('nan') (2004-02-17)
> http://python.org/sf/899109 opened by Arman Bostani
I'm sure Tim can explain this better, but what happens is this: 1 is
coerced to float for comparison. Then, the following C expression is
evaluated (Objects/floatobject.c:float_compare):
return (i < j) ? -1 : (i > j) ? 1 : 0;
Because NaN is "unordered", 1<NaN and NaN<1 are both false, and so python
returns 0 from float_compare. Compare to this C program:
$ cat compare.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double a=1.0, b = atof("nan");
printf("a<b: %d b<a: %d a==b: %d cmp(a,b): %d\n",
a<b, b<a, a==b, (a < a) ? -1 : (a > a) ? 1 : 0);
return 0;
}
$ gcc -Wall compare.c && ./a.out nan 1.0
a<b: 0 b<a: 0 a==b: 0 cmp(a,b): 0
Jeff
More information about the Python-Dev
mailing list