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
1
vote
1 answer

strtod change last digit

someone can explain to me what is happening? double dbl = stdtod("9999999999999999", NULL); I got the value: 9999999999999998 and not: 9999999999999999 I don't understand why this happen, in the MSDN, is specified that the max double number is…
scifie
  • 9
  • 1
1
vote
2 answers

strtod - Problem in converting string to double

I have a very small program which converts a string to double. The problem is everytime it is printing 0.0000. Please help me. Thanks in advance. enter code here $ export LT_LEAK_START=1.5 $ echo $LT_LEAK_START 1.5 #include int…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
1
vote
1 answer

POSIX: strtod - question

Can someone explain me, how strtod works resp. why I get here 10,2 despite the en_EN-locale? #!/usr/bin/env perl use warnings; use 5.012; use POSIX qw(locale_h strtod); setlocale( LC_NUMERIC, 'en_EN.UTF-8' ); my $str = '5,6'; $! = 0; my ( $num,…
sid_com
  • 24,137
  • 26
  • 96
  • 187
1
vote
2 answers

strtod with base parameter

I don't want to unnecessarily re-invent the wheel, but I have been looking for the functionality of strtod but with a base parameter (2,8,10,16). (I know strtoul allows a base parameter but I'm looking for return type double). Any advice /…
jparanich
  • 8,372
  • 4
  • 26
  • 34
1
vote
2 answers

warning: "assignment discards 'const' qualifier from pointer target type"

I made my own strtod() function. I can printf in console but it is giving warning like this when it runs: myStrTod.c|29|warning: "assignment discards 'const' qualifier from pointer target type"| #include float myStrTod(const char *…
M.said
  • 21
  • 1
  • 3
1
vote
1 answer

Does the Java compilers text-to-float value conversion differ from strtod?

The Java Language Specification point 3.10.2 states that floating point values are converted as specified in the IEEE 754 standard. For strtod, the C standard specifies how the function converts text to float values. Regarding the representation…
maxdev
  • 2,491
  • 1
  • 25
  • 50
1
vote
2 answers

How convert very long string to double in portable C

I want to convert a very long string of numbers to a double in a portable way in C. In my case, portable means that it would work in Linux and Windows. My ultimate goal is to be able to pack a string of numbers into an 8-byte double and…
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
1
vote
1 answer

strtod how to check if 0 is error or good value

I would like to to convert string to double. I do that this way: bool String2ValueType(const std::string & a_str_value_type, double & a_result) { if(a_str_value_type.empty()) return false; char * end; double result =…
user4593532
1
vote
2 answers

strtod definition and type of passed pointer

From definition double strtod ( const char * str, char ** endptr ); C reference sites provide an example for that mighty function: char szOrbits[] = "365.24 29.53"; char * pEnd; double d1, d2; d1 = strtod (szOrbits,&pEnd); d2 = strtod…
Peter Cerba
  • 806
  • 4
  • 14
  • 26
0
votes
0 answers

How do I implement strtod?

For example lets take the value 10.23E-4. What I did was break it up into several variables, whole=10, decimal=23, decimalLength=2, isNegative=1, exponent=4 (is there a better name for these?) First I d = decimal; d*/exp10(decimalLength); d+=whole;.…
Eric Stotch
  • 141
  • 4
  • 19
0
votes
1 answer

strtod returns wrong value when called from Python ctypes and run from Spyder or Pyzo

I need to call from a Python script a C library which parses a string to a double and prints the result. The parsing works or not depending on the IDE I use. My OS is Debian 11. Here is a minimal example. The library (file test.c): #include…
Gnatho
  • 1
  • 1
0
votes
1 answer

Separate 9 numbers of type double from command-line in c with strtok() and strtod()

I have the following command-line interface with a -k parameter that looks like this: -k "0.0:-1.0:0.0:-1.0:4.0:-1.0:0.0:-1.0:0.0". These are 9 double values separated by ":". I've managed to separate the first double value 0.0 with strtok() and…
Lost_Delegate
  • 23
  • 1
  • 7
0
votes
2 answers

How to convert decimal numbers stored as characters in a string to doubles?

New to programming, I'm looping through a postfix string and trying to retrieve the numbers individually, I only need one at a time. Initially I wrote this for integers and just did "- '0'", however, I now need to try and make it compatible for…
lecar888
  • 7
  • 2
0
votes
0 answers

result of convert string to double is inconsistent with strtod

I implemented a function StringToDouble for converting string to double,but when I test my function and standard strtod function with the same string,the result is different,I dont know why this happened,is it a problem with precision when doing…
codesavesworld
  • 587
  • 3
  • 10
0
votes
0 answers

how to handle failure value in strtod?

i have a bit of code consisting of an equation that should substract a specific value in an multidimensional string array off of a double-variable and immediately write it back in the double-variable. It's for a uni-project and i tried searching for…