initialize a double with NAN?

D

David Mathog

How does one initialize a variable to NAN in compliance with the c99
standard? This doesn't work:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
int main(void){
double dnan=FP_NAN;
fprintf(stdout,"dnan is %lf\n",dnan);
dnan=log(-1);
fprintf(stdout,"dnan is %lf\n",dnan);
exit(EXIT_SUCCESS);
}

gcc -std=c99 -pedantic -lm -o test test.c
../test
dnan is 0.000000
dnan is nan

Changing the declaration line to

double dnan=log(-1);

does result in dnan being initialized to nan. Is that method standard
compliant, or are compilers allowed to thow an error there???

Thanks,

David Mathog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top