Questions tagged [strtod]

strtod is the C standard library function for converting an ASCII string into a floating-point number.

strtod is the C standard library function (found in the ANSI standard, section 2.13.2.4 - found in stdlib.h) which converts a string into a floating point number.


double strtod(const char *ptr, char **endptr)

  • ptr: A pointer to the string that will be converted
  • endptr: A pointer to where the last successful character match occurred.

    For example, calling strtod on the string "123abc" yields an endptr which is equal to ptr + 3. This is often used to test whether or not a string conversion went correctly - if no conversion took place, then *endptr == ptr.

    Note that this does not occur if endptr is NULL.


According to ANSI,

The number may consist of an optional sign, a string of digits with an optional decimal character, and an optional e or E followed by a optionally signed exponent.

(This effectively allows both "normal" decimal numbers, as well as scientific notation).

When the converter experiences either an overflow or underflow, errno is set to ERANGE, and either 0 (for underflows) or HUGE_VAL (for overflows) - it is thus recommended to clear errno before calling this function, otherwise an underflow error and the conversion from "0" are impossible to differentiate.

61 questions
62
votes
9 answers

How to convert string to float?

#include #include int main() { char s[100] ="4.0800" ; printf("float value : %4.8f\n" ,(float) atoll(s)); return 0; } I expect the output should be 4.08000000 whereas I got only 4.00000000. Is there any way to…
abubacker
  • 4,638
  • 6
  • 32
  • 35
15
votes
3 answers

Converting char* to float or double

I have a value I read in from a file and is stored as a char*. The value is a monetary number, #.##, ##.##, or ###.##. I want to convert the char* to a number I can use in calculations, I've tried atof and strtod and they just give me garbage…
Andrew
  • 830
  • 3
  • 10
  • 27
12
votes
3 answers

strtod underflow, return value != 0

Here's my test code: errno = 0; d = strtod("1.8011670033376514e-308", NULL); With this code, I get d == 1.8011670033376514e-308 and errno == ERANGE. From strtod(3): If the correct value would cause overflow, plus or minus HUGE_VAL (HUGE_VALF,…
Petri Lehtinen
  • 1,995
  • 2
  • 17
  • 22
11
votes
1 answer

What is the result of `strtod("3ex", &end)` supposed to be? What about `sscanf`?

In my experiments this expression double d = strtod("3ex", &end); initializes d with 3.0 and places end pointer at 'e' character in the input string. This is exactly as I would expect it to behave. The 'e' character might look as a beginning of…
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
7
votes
2 answers

Odd behavior when converting C strings to/from doubles

I'm having trouble understanding C's rules for what precision to assume when printing doubles, or when converting strings to doubles. The following program should illustrate my point: #include #include #include…
Jordan Carlson
  • 393
  • 1
  • 7
7
votes
4 answers

Python equivalent to C strtod

I am working on converting parts of a C++ program to Python, but I have some trouble replacing the C function strtod. The strings I'm working on consists of simple mathmatical-ish equations, such as "KM/1000.0". The problem is that the both…
Waws
  • 203
  • 1
  • 2
  • 15
7
votes
2 answers

strtof = strtod followed by cast?

Suppose you have a string like "0.1" that can be only approximately represented as a binary floating point number, and you want to convert it to single precision floating point. This can be done as strtof(s, 0); or (float)strtod(s,…
rwallace
  • 31,405
  • 40
  • 123
  • 242
7
votes
7 answers

Problem with string conversion to number ( strtod )

I am using strtod( ) function to extract an environment variable as a string, and then changing it to double using strtod: enter code here char strEnv[32]; strncpy(strEnv, getenv("LT_LEAK_START"), 31); // How to make sure before parsing that env…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
6
votes
1 answer

Valgrind: "Invalid read" with c_str and strtod

im using strtod() to convert some inputstrings. while checking my code with valgrind, i came accross a "invalid read of size 8". the message shows up if b starts with "i" or "n", thats what i discovered so far. also, if i create a const char*…
kstudent
  • 121
  • 1
  • 5
5
votes
1 answer

Correct strtod implementation?

Simple question: what is the correct bit-representation of the number 1.15507e-173, in double precision? Full question: how does one determine the correct parsing of this number? Background: my question follows from this answer which shows two…
Gavin Band
  • 71
  • 4
5
votes
1 answer

Why isn't Darwin's strtod thread safe?

The following test always produces failures or bus errors for me on my Intel Mac Mini. Compiler: uname -a: Darwin vogon13 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 g++ -v: Target:…
Matt Chambers
  • 2,229
  • 1
  • 25
  • 43
4
votes
2 answers

Unexpected endptr with strtod()/strtold()

I'd expect the endptr to point to the same value with both strtod() and strtold(). Yet they differ. I suspect strtold() is incorrect. OTOH, this could be a case where the spec is not clear and either result is acceptable. Is this a bug (and with…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
4
votes
2 answers

return value of strtod() if string equals to zero

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…
CaTx
  • 1,421
  • 4
  • 21
  • 42
4
votes
4 answers

strtod with limited string length

If I want to parse the first 3 characters from the char array as a double, ignoring the following characters, do I really need to do this? int main() { const char a[] = "1.23"; char *b = malloc(sizeof(char) * 4); memcpy(b, a,…
Tyilo
  • 28,998
  • 40
  • 113
  • 198
3
votes
4 answers

strtod accepts "e" but also "d" -- why?

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…
Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26
1
2 3 4 5