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
3
votes
3 answers

Confused how to convert from a string to double using strtod() in C++

If someone could explain how to use the function, that would be great. I don't understand the parameters. Thanks
Sam
  • 2,309
  • 9
  • 38
  • 53
3
votes
1 answer

Is there a Python equivalent to the endptr parameter to C's strtod?

I am trying to write a function that splits a string containing a floating-point number and some units. The string may or may not have spaces between the number and the units. In C, the function strtod has a very handy parameter, named endptr that…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
3
votes
1 answer

strtod not recognizing inf

I have the following code: int check_for_non_number(char *input, int lineNum) { errno = 0; char *endptr; printf("%s\n",input); double xnum = strtod(input, &endptr); // IF endptr FOUND A NON-VALID ENTRY AND THAT ENTRY IS NOT THE…
sdepot
  • 41
  • 6
3
votes
2 answers

Why does strtod() with DBL_MIN give ERANGE?

This program calls strtod() on the first command line argument and prints the returned value: #include #include #include #include int main(int argc, char **argv) { errno = 0; double d = strtod(argv[1],…
Ben C
  • 658
  • 6
  • 18
2
votes
2 answers

strtod doesnt set errno on wrong input

g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 #include ... cin >> str; errno = 0 ; double d = strtod(str.c_str(), NULL); if (errno) { cout << "Please, enter number."; } on wrong input errno stay 0. EDITED: Next works fine: char…
Yola
  • 18,496
  • 11
  • 65
  • 106
2
votes
1 answer

Can't get a NaN from the MSVCRT strtod/sscanf/atof functions

Is there any way to get NaNs from the Windows CRT string to float functions? Why: I'm writing an IEEE float to string converter in C with no information loss (strtod, sscanf or atof return the original float) provided the rounding mode doesn't…
Řrřola
  • 6,007
  • 1
  • 17
  • 10
2
votes
2 answers

Why do strtod() and strtof() of the Newlib C Standard Library implementation uses dynamic memory allocation?

Newlib is a C standard library implementation (largely inspired by BSD libc) intended for use on embedded systems. Apparently, the string to floating point conversion functions (strtod, strtof) use dynamic memory allocation by calling a function…
Cerike
  • 354
  • 1
  • 15
2
votes
2 answers

decimal error in std::strtod() with Visual Studio 2015, 64 bit

std::strod() converts string to double. But in one particular case, there is a tiny decimal error. This error can also be seen with atof() and sscanf(). The error only occurs if all the following conditions are met: Visual Studio 2015 (or VS 2015…
Johan
  • 31
  • 3
2
votes
2 answers

can't get string conversion strtod in c

Could somebody help me(sorry for the english), Im trying to convert a string to a double but when I can't get it here's my code(thank you, I'll appreciate help so much): #include #include #include #define…
user2461687
  • 173
  • 1
  • 13
2
votes
0 answers

GNU C: atof(), strtof() and strtod() fail (Debian for BeagleBoard)

I have some C code which converts an ASCII string to a double with strtod(...). The program gets compiled for x86 (debugging), ARM and PowerPC (embedded target systems). The ARM board is actually a BeagleBoard xM running the Debian which is…
Jeremy
  • 1,083
  • 3
  • 13
  • 25
1
vote
1 answer

Implementation of strtof(), floating-point multiplication and mantissa rounding issues

This question is not so much about the C as about the algorithm. I need to implement strtof() function, which would behave exactly the same as GCC one - and do it from scratch (no GNU MPL etc.). Let's skip checks, consider only correct inputs and…
trexxet
  • 176
  • 3
  • 14
1
vote
1 answer

Is there any difference in the way atof and strtod work?

I know that strtod() and atof() functions are used for conversion from string to double. But I can't figure out the difference between these two functions. Is there any difference between these two functions, if yes then please let me know... Thanks…
Abhishek Jaiswal
  • 288
  • 2
  • 14
1
vote
3 answers

strtof() function misplacing decimal place

I have a string "1613894376.500012077" and I want to use strtof in order to convert to floating point 1613894376.500012077. The problem is when I use strtof I get the following result with the decimal misplaced 1.61389e+09. Please help me…
GigaWatts
  • 101
  • 8
1
vote
2 answers

C++: strod() and atof() are not returning double as expected

I am trying to make strings into doubles using the values that I obtained from a .txt file. The doubles I am obtaining have no decimals. I believe that this is because, in the .txt, the decimals of the numbers are separated by a comma instead of a…
ao_martinv
  • 55
  • 6
1
vote
0 answers

How to convert strtod's 'P' binary exponent notation to decimal?

I knew about the standard 'e' exponent for decimal notation, but the linux man page of strtod says about the hexadecimal notation: A hexadecimal number consists of a "0x" or "0X" followed by a nonempty sequence of hexadecimal digits possibly…
theor
  • 1,545
  • 1
  • 9
  • 16