LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

sscanf and NaN floating point

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.

0 Kudos
Message 1 of 3
(5,031 Views)

Nothing is wrong, it's a missing feature, see here and here

0 Kudos
Message 2 of 3
(5,016 Views)

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.

0 Kudos
Message 3 of 3
(4,995 Views)