3

I find this strange.

While it makes sense that strtod accepts 'e' as one of the characters (exactly one to be precise) in the input string I find that it also accepts 'd'.

Can someone please explain?

#include < stdio.h >
#include < stdlib.h >
int main ()
{
char *s[] = {"1a1", "1e1", "1d1", "1f1"};
char * pEnd;
double d0, d1, d2, d3;
d0 = strtod (s[0],&pEnd);
d1 = strtod (s[1],NULL);
d2 = strtod (s[2],NULL);
d3 = strtod (s[3],NULL);
printf ("::: [%f] [%f] [%f] [%f] \n", d0, d1, d2, d3);
return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26
  • This may help: http://stackoverflow.com/questions/2555097/reading-ascii-numbers-using-d-instead-of-e-for-scientific-notation-using-c – cnicutar Jun 02 '11 at 15:29

4 Answers4

4

What do you mean by "accepts"? This is the output I'm getting

::: [1.000000] [10.000000] [1.000000] [1.000000]

strtod (as all other conversion routines in C) parses the string until it finds the character that "doesn't belong". That character is not considered an error, it is simply treated as a terminator character. In case of "1d1" only the first "1" is parsed and the parsing stops at 'd'. The result of conversion is 1.0 (as it should be).

If you asked strtod to return the "end pointer" from each call, you'd see that the pointer points to character 'd' for the input string with 'd' in it (the same applies to 'a' and 'f' as well).

If you are getting a different result, it must be a quirk of the implementation you are using.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • well when i used gcc and libc, this is exactly what i observed but problem was faced with visual studio (cl.exe) and corresponding libraries. it seems it is documented by Microzoft that 'd' and 'D' are also supported to mark double. – Manoj Awasthi Jun 03 '11 at 13:55
3

What Compiler/Libraries are you using to compile this code? Assuming you're on Visual Studio, this behaviour is expected (quoting text from the MSDN):

strtod expects nptr to point to a string of the following form:

[whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits]

You can find the full documentation for strtod here

Other implementations of the library would probably support something similar. However, the man-page for strtod found here, doesn't state that d is recognized as a valid character for the conversion purpose. In such a case, it would cause the parsing of the input string to stop and only characters parsed until that point would be converted (the same happens for the strings containing a and f).

Perhaps you should look at documentation for your library implementation and find out the format that strtod would be able to parse for your specific case.

Bhargav
  • 9,869
  • 1
  • 19
  • 29
1

Use of D instead of E is the Fortran way to mark out double data (instead of float). You probably are using a standard library which accept it as an extension.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
0

Which platform and compiler? In both Linux and Mac OS X I get this result:

::: [1.000000] [10.000000] [1.000000] [1.000000]

which means only e worked. Note that you didn't check for errors...

lhf
  • 70,581
  • 9
  • 108
  • 149