From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

return value of strtod() if string equals to zero

as per article:

zone.ni.com/reference/en-XX/help/370051V-01/cvi/libref/cvistrtod/

 

If no conversion is possible, the function returns zero.

 

what if my string equals to zero (i.e., 0.0000)?  how can I know if there is no error from the conversion?

0 Kudos
Message 1 of 5
(4,947 Views)

You may check errno. If it is set to EINVAL , you know rthat there was an error.  But unfortunarely the standard (http://pubs.opengroup.org/onlinepubs/009695399/functions/strtod.html)  doesn't require setting errno to EINVAL for that case.

 

0 Kudos
Message 2 of 5
(4,933 Views)

using this code as an example:

 

char    *Y = "XYZ";
double  MyNum;
char    *MyEndPtr;
int     Err_Conversion = 0;

errno = 0;  //reset
MyNum = strtod (Y, &MyEndPtr);

if ( (MyNum == 0) && (strcmp(Y, MyEndPtr) == 0) && (*MyEndPtr != '\0'))
        { Err_Conversion = 1;   }

when an error occurs:

  1. MyNum = 0
  2. the address where the error occurs (beginning or middle of string) is stored in MyEndPtr.
  3. the content of MyEndPtr is not null.
0 Kudos
Message 3 of 5
(4,890 Views)

OK, scratch my reply above.  this is the more correct one:

 

char    *Y = "XYZ";
double  MyNum;
char    *MyEndPtr;
int     Err_Conversion = 0;

errno = 0;  //reset
MyNum = strtod (Y, &MyEndPtr);

if ( (MyNum == 0) && (strcmp(Element [LineCount][i],MyEndPtr) == 0) && (*MyEndPtr != '\0') && (errno != 0))
        { Err_Conversion = 1;   }

when an error occurs:

  1. MyNum = 0
  2. the address where the error occurs (beginning or middle of string) is stored in MyEndPtr.
  3. the content of MyEndPtr is not null.
  4. errno != 0 if underflow or overflow occurs, in which case MyNum is set to +/–HUGE_VAL, with HUGE_VAL sign matching the sign of the value that cannot be represented.
0 Kudos
Message 4 of 5
(4,883 Views)

I just want to highlight that a test for equality on a double value is very prone to errors: I suggest to use FP_Compare () function instead. See the function help for a detailed explanation of the problems such a test can encounter and how the function manages to avoid them.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 5
(4,863 Views)