1STRTOD(3P) POSIX Programmer's Manual STRTOD(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 strtod, strtof, strtold - convert a string to a double-precision number
13
15 #include <stdlib.h>
16
17 double strtod(const char *restrict nptr, char **restrict endptr);
18 float strtof(const char *restrict nptr, char **restrict endptr);
19 long double strtold(const char *restrict nptr, char **restrict endptr);
20
21
23 These functions shall convert the initial portion of the string pointed
24 to by nptr to double, float, and long double representation, respec‐
25 tively. First, they decompose the input string into three parts:
26
27 1. An initial, possibly empty, sequence of white-space characters (as
28 specified by isspace())
29
30 2. A subject sequence interpreted as a floating-point constant or rep‐
31 resenting infinity or NaN
32
33 3. A final string of one or more unrecognized characters, including
34 the terminating null byte of the input string
35
36 Then they shall attempt to convert the subject sequence to a floating-
37 point number, and return the result.
38
39 The expected form of the subject sequence is an optional plus or minus
40 sign, then one of the following:
41
42 * A non-empty sequence of decimal digits optionally containing a radix
43 character, then an optional exponent part
44
45 * A 0x or 0X, then a non-empty sequence of hexadecimal digits option‐
46 ally containing a radix character, then an optional binary exponent
47 part
48
49 * One of INF or INFINITY, ignoring case
50
51 * One of NAN or NAN(n-char-sequence_opt), ignoring case in the NAN
52 part, where:
53
54
55 n-char-sequence:
56 digit
57 nondigit
58 n-char-sequence digit
59 n-char-sequence nondigit
60
61 The subject sequence is defined as the longest initial subsequence of
62 the input string, starting with the first non-white-space character,
63 that is of the expected form. The subject sequence contains no charac‐
64 ters if the input string is not of the expected form.
65
66 If the subject sequence has the expected form for a floating-point num‐
67 ber, the sequence of characters starting with the first digit or the
68 decimal-point character (whichever occurs first) shall be interpreted
69 as a floating constant of the C language, except that the radix charac‐
70 ter shall be used in place of a period, and that if neither an exponent
71 part nor a radix character appears in a decimal floating-point number,
72 or if a binary exponent part does not appear in a hexadecimal floating-
73 point number, an exponent part of the appropriate type with value zero
74 is assumed to follow the last digit in the string. If the subject
75 sequence begins with a minus sign, the sequence shall be interpreted as
76 negated. A character sequence INF or INFINITY shall be interpreted as
77 an infinity, if representable in the return type, else as if it were a
78 floating constant that is too large for the range of the return type. A
79 character sequence NAN or NAN(n-char-sequence_opt) shall be interpreted
80 as a quiet NaN, if supported in the return type, else as if it were a
81 subject sequence part that does not have the expected form; the meaning
82 of the n-char sequences is implementation-defined. A pointer to the
83 final string is stored in the object pointed to by endptr, provided
84 that endptr is not a null pointer.
85
86 If the subject sequence has the hexadecimal form and FLT_RADIX is a
87 power of 2, the value resulting from the conversion is correctly
88 rounded.
89
90 The radix character is defined in the program's locale (category
91 LC_NUMERIC ). In the POSIX locale, or in a locale where the radix char‐
92 acter is not defined, the radix character shall default to a period (
93 '.' ).
94
95 In other than the C or POSIX locales, other implementation-defined
96 subject sequences may be accepted.
97
98 If the subject sequence is empty or does not have the expected form, no
99 conversion shall be performed; the value of str is stored in the object
100 pointed to by endptr, provided that endptr is not a null pointer.
101
102 The strtod() function shall not change the setting of errno if success‐
103 ful.
104
105 Since 0 is returned on error and is also a valid return on success, an
106 application wishing to check for error situations should set errno to
107 0, then call strtod(), strtof(), or strtold(), then check errno.
108
110 Upon successful completion, these functions shall return the converted
111 value. If no conversion could be performed, 0 shall be returned, and
112 errno may be set to [EINVAL].
113
114 If the correct value is outside the range of representable values,
115 ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL shall be returned (according to
116 the sign of the value), and errno shall be set to [ERANGE].
117
118 If the correct value would cause an underflow, a value whose magnitude
119 is no greater than the smallest normalized positive number in the
120 return type shall be returned and errno set to [ERANGE].
121
123 These functions shall fail if:
124
125 ERANGE The value to be returned would cause overflow or underflow.
126
127
128 These functions may fail if:
129
130 EINVAL No conversion could be performed.
131
132
133 The following sections are informative.
134
136 None.
137
139 If the subject sequence has the hexadecimal form and FLT_RADIX is not a
140 power of 2, and the result is not exactly representable, the result
141 should be one of the two numbers in the appropriate internal format
142 that are adjacent to the hexadecimal floating source value, with the
143 extra stipulation that the error should have a correct sign for the
144 current rounding direction.
145
146 If the subject sequence has the decimal form and at most DECIMAL_DIG
147 (defined in <float.h>) significant digits, the result should be cor‐
148 rectly rounded. If the subject sequence D has the decimal form and more
149 than DECIMAL_DIG significant digits, consider the two bounding, adja‐
150 cent decimal strings L and U, both having DECIMAL_DIG significant dig‐
151 its, such that the values of L, D, and U satisfy L <= D <= U. The
152 result should be one of the (equal or adjacent) values that would be
153 obtained by correctly rounding L and U according to the current round‐
154 ing direction, with the extra stipulation that the error with respect
155 to D should have a correct sign for the current rounding direction.
156
157 The changes to strtod() introduced by the ISO/IEC 9899:1999 standard
158 can alter the behavior of well-formed applications complying with the
159 ISO/IEC 9899:1990 standard and thus earlier versions of the base docu‐
160 ments. One such example would be:
161
162
163 int
164 what_kind_of_number (char *s)
165 {
166 char *endp;
167 double d;
168 long l;
169
170
171 d = strtod(s, &endp);
172 if (s != endp && *endp == `\0')
173 printf("It's a float with value %g\n", d);
174 else
175 {
176 l = strtol(s, &endp, 0);
177 if (s != endp && *endp == `\0')
178 printf("It's an integer with value %ld\n", 1);
179 else
180 return 1;
181 }
182 return 0;
183 }
184
185 If the function is called with:
186
187
188 what_kind_of_number ("0x10")
189
190 an ISO/IEC 9899:1990 standard-compliant library will result in the
191 function printing:
192
193
194 It's an integer with value 16
195
196 With the ISO/IEC 9899:1999 standard, the result is:
197
198
199 It's a float with value 16
200
201 The change in behavior is due to the inclusion of floating-point num‐
202 bers in hexadecimal notation without requiring that either a decimal
203 point or the binary exponent be present.
204
206 None.
207
209 None.
210
212 isspace(), localeconv(), scanf(), setlocale(), strtol(), the Base Defi‐
213 nitions volume of IEEE Std 1003.1-2001, Chapter 7, Locale, <float.h>,
214 <stdlib.h>
215
217 Portions of this text are reprinted and reproduced in electronic form
218 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
219 -- Portable Operating System Interface (POSIX), The Open Group Base
220 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
221 Electrical and Electronics Engineers, Inc and The Open Group. In the
222 event of any discrepancy between this version and the original IEEE and
223 The Open Group Standard, the original IEEE and The Open Group Standard
224 is the referee document. The original Standard can be obtained online
225 at http://www.opengroup.org/unix/online.html .
226
227
228
229IEEE/The Open Group 2003 STRTOD(3P)