12-20-2014 03:36 PM
I do not understand what is wrong.
If I convert a double variable with value NaN with sprintf, it correctly converts to string "NaN".
When I run the reverse, string to double by sscanf it fails and the number is not converted.
I do not understand where wrong. printf and scanf should handle NaN. Where is the error?
Help me understand.
Test code:
char DoubleStr[50];
double Valdouble;
i=0;
DoubleStr[0]=0;
Valdouble=NotANumber(); //After This Valdouble==NaN, DoubleStr=="",i==0 (OK)
i=sprintf(DoubleStr,"%lf", Valdouble); //After This Valdouble==NaN, DoubleStr=="NaN", i==3 (OK)
Valdouble=0.0; //After This Valdouble==0.0, DoubleStr=="NaN", i==3 (OK)
i=sscanf(DoubleStr,"%lf",&Valdouble); //After This Valdouble==0.0, DoubleStr=="NaN", i==0 (Why "NaN" not be conveted and stored in Valdouble?)
Thank You.
12-21-2014 03:36 AM - edited 12-21-2014 03:38 AM
12-21-2014 07:27 AM
I thank you very much for the quick response.
After this I tried a solution "manually", and it seems to work well.
Test code:
char DoubleStr[50];
double Valdouble;
i=0;
DoubleStr[0]=0;
Valdouble=NotANumber(); //After This Valdouble==NaN, DoubleStr=="",i==0 (OK)
i=sprintf(DoubleStr,"%lf", Valdouble); //After This Valdouble==NaN, DoubleStr=="NaN", i==3 (OK)
Valdouble=0.0; //After This Valdouble==0.0, DoubleStr=="NaN", i==3 (OK)
i=sscanf(DoubleStr,"%lf",&Valdouble); //After This Valdouble==0.0, DoubleStr=="NaN", i==0 (Why "NaN" not be conveted and stored in Valdouble?)
i=0;
if(stricmp(DoubleStr,"NaN")==0)
{ Valdouble=NotANumber(); i=1; }
else if(stricmp(DoubleStr,"+Inf")==0)
{ Valdouble=PositiveInfinity(); i=1; }
else if(stricmp(DoubleStr,"-Inf")==0)
{ Valdouble=NegativeInfinity(); i=1; }
else
{ i=sscanf(DoubleStr,"%lf",&Valdouble); }
But there is some other way more elegant and faster to accomplish this?
There anything in winapi to solve this?
I should make parsing of large quantities of data and already the standard "sscan" is quite slow.
Thank you very much.