LabWindows/CVI Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
m-a

Make scanf read back [+][-]Inf and NaN

Greetings,

 

LabWindows/CVI 9.0.1's implementation of <stdio.h> *scanf and <stdlib.h> strtod functions appears not to support reading back "NaN", "Inf", "+Inf" or "-Inf" values, although printf() can generate them. May I suggest to add functionality like described below.

 

Practical use would be that it's easier to propagate "invalid measurement" through strings if strto*() and *scanf() understand all classes of strings that *printf() family functions will generate.

 

This suggestion is in alignment with ISO 9899:1999 (withdrawn) and its successor ISO 9899:2011, i. e. C99 and C11. Note that there is a "stronger" suggestion to implement all of the C11 standard library in the idea exchange already, which - if implemented - would subsume this suggestion, but as the bare minimum, I'd certainly appreciate seeing this in CVI 2012.

 

Demo code:

 

 printf("%g\n", -NotANumber());
 printf("%+g\n", PositiveInfinity());
 printf("%g\n", NegativeInfinity());
 double x = 0; int i;
 i = sscanf("NaN", "%lf", &x); printf("i=%d, x=%g\n", i, x);
 i = sscanf("Inf", "%lf", &x); printf("i=%d, x=%g\n", i, x);
 errno = 0; x = strtod("NaN", NULL); printf("x=%g, errno=%d\n", x, errno);

 

Desired result:

 

 -NaN

 +Inf

 -Inf

 i=1, x=NaN

 i=1, x=Inf

 x=NaN, errno=0

 

Actual result: we see that printf supports NaN/Inf, but scanf and strtod do not:

 

 NaN
 +Inf
 -Inf
 i=0, x=0
 i=0, x=0
 x=0, errno=0

 

Thank you.

 

1 Comment
LuisG
NI Employee (retired)
Status changed to: Under Consideration