1STRTOD(P)                  POSIX Programmer's Manual                 STRTOD(P)
2
3
4

NAME

6       strtod, strtof, strtold - convert a string to a double-precision number
7

SYNOPSIS

9       #include <stdlib.h>
10
11       double strtod(const char *restrict nptr, char **restrict endptr);
12       float strtof(const char *restrict nptr, char **restrict endptr);
13       long double strtold(const char *restrict nptr, char **restrict endptr);
14
15

DESCRIPTION

17       These functions shall convert the initial portion of the string pointed
18       to by nptr to double, float, and long  double  representation,  respec‐
19       tively. First, they decompose the input string into three parts:
20
21        1. An  initial, possibly empty, sequence of white-space characters (as
22           specified by isspace())
23
24        2. A subject sequence interpreted as a floating-point constant or rep‐
25           resenting infinity or NaN
26
27        3. A  final  string  of one or more unrecognized characters, including
28           the terminating null byte of the input string
29
30       Then they shall attempt to convert the subject sequence to a  floating-
31       point number, and return the result.
32
33       The  expected form of the subject sequence is an optional plus or minus
34       sign, then one of the following:
35
36        * A non-empty sequence of decimal digits optionally containing a radix
37          character, then an optional exponent part
38
39        * A  0x or 0X, then a non-empty sequence of hexadecimal digits option‐
40          ally containing a radix character, then an optional binary  exponent
41          part
42
43        * One of INF or INFINITY, ignoring case
44
45        * One  of  NAN  or  NAN(n-char-sequence_opt), ignoring case in the NAN
46          part, where:
47
48
49          n-char-sequence:
50              digit
51              nondigit
52              n-char-sequence digit
53              n-char-sequence nondigit
54
55       The subject sequence is defined as the longest initial  subsequence  of
56       the  input  string,  starting with the first non-white-space character,
57       that is of the expected form. The subject sequence contains no  charac‐
58       ters if the input string is not of the expected form.
59
60       If the subject sequence has the expected form for a floating-point num‐
61       ber, the sequence of characters starting with the first  digit  or  the
62       decimal-point  character  (whichever occurs first) shall be interpreted
63       as a floating constant of the C language, except that the radix charac‐
64       ter shall be used in place of a period, and that if neither an exponent
65       part nor a radix character appears in a decimal floating-point  number,
66       or if a binary exponent part does not appear in a hexadecimal floating-
67       point number, an exponent part of the appropriate type with value  zero
68       is  assumed  to  follow  the  last  digit in the string. If the subject
69       sequence begins with a minus sign, the sequence shall be interpreted as
70       negated.  A  character sequence INF or INFINITY shall be interpreted as
71       an infinity, if representable in the return type, else as if it were  a
72       floating constant that is too large for the range of the return type. A
73       character sequence NAN or NAN(n-char-sequence_opt) shall be interpreted
74       as  a  quiet NaN, if supported in the return type, else as if it were a
75       subject sequence part that does not have the expected form; the meaning
76       of  the  n-char  sequences  is implementation-defined. A pointer to the
77       final string is stored in the object pointed  to  by  endptr,  provided
78       that endptr is not a null pointer.
79
80       If  the  subject  sequence  has the hexadecimal form and FLT_RADIX is a
81       power of 2, the  value  resulting  from  the  conversion  is  correctly
82       rounded.
83
84       The  radix  character  is  defined  in  the  program's locale (category
85       LC_NUMERIC ). In the POSIX locale, or in a locale where the radix char‐
86       acter  is  not defined, the radix character shall default to a period (
87       '.' ).
88
89       In other than the C    or POSIX  locales, other  implementation-defined
90       subject sequences may be accepted.
91
92       If the subject sequence is empty or does not have the expected form, no
93       conversion shall be performed; the value of str is stored in the object
94       pointed to by endptr, provided that endptr is not a null pointer.
95
96       The strtod() function shall not change the setting of errno if success‐
97       ful.
98
99       Since 0 is returned on error and is also a valid return on success,  an
100       application  wishing  to check for error situations should set errno to
101       0, then call strtod(), strtof(), or strtold(), then check errno.
102

RETURN VALUE

104       Upon successful completion, these functions shall return the  converted
105       value.  If  no  conversion could be performed, 0 shall be returned, and
106       errno may be set to [EINVAL].
107
108       If the correct value is outside  the  range  of  representable  values,
109       ±HUGE_VAL,  ±HUGE_VALF,  or  ±HUGE_VALL shall be returned (according to
110       the sign of the value), and errno shall be set to [ERANGE].
111
112       If the correct value would cause an underflow, a value whose  magnitude
113       is  no  greater  than  the  smallest  normalized positive number in the
114       return type shall be returned and errno set to [ERANGE].
115

ERRORS

117       These functions shall fail if:
118
119       ERANGE The value to be returned would cause overflow    or underflow.
120
121
122       These functions may fail if:
123
124       EINVAL No conversion could be performed.
125
126
127       The following sections are informative.
128

EXAMPLES

130       None.
131

APPLICATION USAGE

133       If the subject sequence has the hexadecimal form and FLT_RADIX is not a
134       power  of  2,  and  the result is not exactly representable, the result
135       should be one of the two numbers in  the  appropriate  internal  format
136       that  are  adjacent  to the hexadecimal floating source value, with the
137       extra stipulation that the error should have a  correct  sign  for  the
138       current rounding direction.
139
140       If  the  subject  sequence has the decimal form and at most DECIMAL_DIG
141       (defined in <float.h>) significant digits, the result  should  be  cor‐
142       rectly rounded. If the subject sequence D has the decimal form and more
143       than DECIMAL_DIG significant digits, consider the two  bounding,  adja‐
144       cent  decimal strings L and U, both having DECIMAL_DIG significant dig‐
145       its, such that the values of L, D, and U satisfy  L  <=  D  <=  U.  The
146       result  should  be  one of the (equal or adjacent) values that would be
147       obtained by correctly rounding L and U according to the current  round‐
148       ing  direction,  with the extra stipulation that the error with respect
149       to D should have a correct sign for the current rounding direction.
150
151       The changes to strtod() introduced by  the  ISO/IEC 9899:1999  standard
152       can  alter  the behavior of well-formed applications complying with the
153       ISO/IEC 9899:1990 standard and thus earlier versions of the base  docu‐
154       ments. One such example would be:
155
156
157              int
158              what_kind_of_number (char *s)
159              {
160                  char *endp;
161                  double d;
162                  long l;
163
164
165                  d = strtod(s, &endp);
166                  if (s != endp && *endp == `\0')
167                      printf("It's a float with value %g\n", d);
168                  else
169                  {
170                      l = strtol(s, &endp, 0);
171                      if (s != endp && *endp == `\0')
172                          printf("It's an integer with value %ld\n", 1);
173                      else
174                          return 1;
175                  }
176                  return 0;
177              }
178
179       If the function is called with:
180
181
182              what_kind_of_number ("0x10")
183
184       an  ISO/IEC 9899:1990  standard-compliant  library  will  result in the
185       function printing:
186
187
188              It's an integer with value 16
189
190       With the ISO/IEC 9899:1999 standard, the result is:
191
192
193              It's a float with value 16
194
195       The change in behavior is due to the inclusion of  floating-point  num‐
196       bers  in  hexadecimal  notation without requiring that either a decimal
197       point or the binary exponent be present.
198

RATIONALE

200       None.
201

FUTURE DIRECTIONS

203       None.
204

SEE ALSO

206       isspace() , localeconv() , scanf() , setlocale() , strtol() , the  Base
207       Definitions   volume   of   IEEE Std 1003.1-2001,  Chapter  7,  Locale,
208       <float.h>, <stdlib.h>
209
211       Portions of this text are reprinted and reproduced in  electronic  form
212       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
213       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
214       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
215       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
216       event of any discrepancy between this version and the original IEEE and
217       The Open Group Standard, the original IEEE and The Open Group  Standard
218       is  the  referee document. The original Standard can be obtained online
219       at http://www.opengroup.org/unix/online.html .
220
221
222
223IEEE/The Open Group                  2003                            STRTOD(P)
Impressum