nan.c:$ gcc nan.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x = strtod("NAN", 0);
printf("%f\n", x);
printf("%d\n", (int)(x == x));
return 0;
}
$ ./a.out
nan
0
This is portable, right?
nan1.c:$ gcc nan1.c
#include <math.h>
#include <stdio.h>
int main()
{
printf("%f\n", NAN);
return 0;
}
$ ./a.out
nan
Is this portable??
I'm working under Mac OS X 10.4 here. Let's see if these programs work under Red Hat Linux. Yes and no:
vincent% gcc nan1.c
nan1.c: In function ‘main’:
nan1.c:6: error: ‘NAN’ undeclared (first use in this function)
nan1.c:6: error: (Each undeclared identifier is reported only once
nan1.c:6: error: for each function it appears in.)
So, strtod seems to be one way to go. How about that nice sounding nan function?
vincent% cat nan2.c(I'm also working here under Linux. I'm not really sure what string I should drop in for the argument for nan. "hi!" also gives the same results as above. I'll stick with strtod for now.)
#include <math.h>
#include <stdio.h>
int main()
{
double x = nan("");
printf("%f\n", x);
printf("%d\n", (int)(x == x));
return 0;
}
vincent% gcc nan2.c -lm
vincent% ./a.out
0.000000
1
No comments:
Post a Comment