4

As per MSDN:

  • strtod returns 0 if no conversion can be performed or an underflow occurs.

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

OK, I use the following code to verify the idea:

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

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

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

I see that MyNum = 0, but never see the content of Y copied into MyEnPtr, or errno = 0 in this forced error. Any idea?

CaTx
  • 1,421
  • 4
  • 21
  • 42
  • check `errno`. `errno` set to `ERANGE` when underflow. – BLUEPIXY Jan 15 '15 at 22:01
  • yes, but my case is not about underflow. it is about converting a string whose numeric value is zero. – CaTx Jan 15 '15 at 22:03
  • `strtod(nptr, endp);` : check `*endp` If due to invalid characters. – BLUEPIXY Jan 15 '15 at 22:06
  • Why did you make `MyEndPtr` an array of 100? And why are you using `strcmp`? I literally posted code that works. – Cornstalks Jan 15 '15 at 22:54
  • @Cornstalks, you are right. I do not need 100 there. by the way, I realized that errno will always be 0 even if the conversion is a forced error. – CaTx Jan 19 '15 at 15:00

2 Answers2

4

Use the str_end parameter of the function. For example:

const char* str = "123junk";
char* str_end;
double d = strtod(str, &str_end);
// Here:
//   d will be 123
//   str_end will point to (str + 3) (the 'j')

// You can tell the string has some junk data if *str_end != '\0'
if (*str_end != '\0') {
    printf("Found bad data '%s' at end of string\n", str_end);
}

If conversion totally fails, str will equal str_end:

const char* str = "junk";
char* str_end;
double d = strtod(str, &str_end);
// Here:
//   d will be 0 (conversion failed)
//   str_end will equal str

if (str == str_end) {
    printf("The string doesn't start with a number!\n");
}

You can combine these two methods to make sure the string was (completely) successfully converted (that is, by checking str != str_end && *str_end == '\0')

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • hey, in my code I spec'ed MyEndPtr [100] because the original code makes multiple comparisons. the snippet is recycled - cannot use your 1-timed char declaration. plus, the length of the input string is variable. I actually spec'ed MyEndPtr [maxlength] in my code. any fix for that? – CaTx Jan 15 '15 at 23:03
2

The signature is (give or take restrict keywords):

 double strtod(const char *nptr, char **endptr);

If you pass a non-null pointer as the second argument, it will be returned with the value of nptr if it could perform no conversion. If it found a genuine zero in the input string, then the value stored in *endptr won't be nptr.

char *end;
const char *data = "0.00000";

errno = 0;
double d = strtod(data, &end);
if (end != data)
    ...a conversion was performed...
else
    ...trouble...

You can also look at errno, but you need to zero it before the call because no function in the standard C library or the POSIX library sets errno to zero.

The standard says:

If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

Returns

The functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value overflows and default rounding is in effect (7.12.1), plus or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to the return type and sign of the value), and the value of the macro ERANGE is stored in errno. If the result underflows (7.12.1), the functions return a value whose magnitude is no greater than the smallest normalized positive number in the return type; whether errno acquires the value ERANGE is implementation-defined.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • so you are saying if my input string equals zero, it will return a zero, but the content of endptr and input string are not the same? – CaTx Jan 15 '15 at 22:10
  • @Leffler, I realized that errno will always be 0 even if the conversion is a forced error. – CaTx Jan 19 '15 at 15:00
  • The only time `errno` is always set is when the number was recognizably a number but the absolute value was too large to fit into the type. If the number is so small that, although it isn't zero, it cannot be represented, you might or might not get a value set in `errno`. But if you present it with `"@#$()"`, the function will fail, return `0.0`, but the value of `endptr` will be the pointer to the `@` sign in the string; no conversion took place. If the string contains `"-0@Gadzooks"`, the return value will be `0.0` and `endptr` will point at the `@` sign; a valid conversion took place. – Jonathan Leffler Jan 19 '15 at 15:38