1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
45 .\" Portions Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
46 .\"
47 .TH STRTOD 3C "Nov 1, 2003"
48 .SH NAME
49 strtod, strtof, strtold, atof \- convert string to floating-point number
50 .SH SYNOPSIS
51 .LP
52 .nf
53 #include <stdlib.h>
54
55 \fBdouble\fR \fBstrtod\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
56 .fi
57
58 .LP
59 .nf
60 \fBfloat\fR \fBstrtof\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
61 .fi
62
63 .LP
64 .nf
65 \fBlong double\fR \fBstrtold\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
66 .fi
67
68 .LP
69 .nf
70 \fBdouble\fR \fBatof\fR(\fBconst char *\fR\fIstr\fR);
71 .fi
72
73 .SH DESCRIPTION
74 .sp
75 .LP
76 The \fBstrtod()\fR, \fBstrtof()\fR, and \fBstrtold()\fR functions convert the
77 initial portion of the string pointed to by \fInptr\fR to \fBdouble\fR,
78 \fBfloat\fR, and \fBlong double\fR representation, respectively. First they
79 decompose the input string into three parts:
80 .RS +4
81 .TP
82 1.
83 An initial, possibly empty, sequence of white-space characters (as specified
84 by \fBisspace\fR(3C))
85 .RE
86 .RS +4
87 .TP
88 2.
89 A subject sequence interpreted as a floating-point constant or representing
90 infinity or NaN
91 .RE
92 .RS +4
93 .TP
94 3.
95 A final string of one or more unrecognized characters, including the
96 terminating null byte of the input string.
97 .RE
98 .sp
99 .LP
100 Then they attempt to convert the subject sequence to a floating-point number,
101 and return the result.
102 .sp
103 .LP
104 The expected form of the subject sequence is an optional plus or minus sign,
105 then one of the following:
106 .RS +4
107 .TP
108 .ie t \(bu
109 .el o
110 A non-empty sequence of digits optionally containing a radix character, then an
111 optional exponent part
112 .RE
113 .RS +4
114 .TP
115 .ie t \(bu
116 .el o
117 A 0x or 0X, then a non-empty sequence of hexadecimal digits optionally
118 containing a radix character, then an optional binary exponent part
119 .RE
120 .RS +4
121 .TP
122 .ie t \(bu
123 .el o
124 One of INF or INFINITY, ignoring case
125 .RE
126 .RS +4
127 .TP
128 .ie t \(bu
129 .el o
130 One of NAN or NAN(\fIn-char-sequence\fR(\fIopt\fR)), ignoring case in the NAN
131 part, where:
132 .sp
133 .in +2
134 .nf
135 n-char-sequence:
136 digit
137 nondigit
138 n-char-sequence digit
139 n-char-sequence nondigit
140 .fi
141 .in -2
142
143 .RE
144 .sp
145 .LP
146 In default mode for \fBstrtod()\fR, only decimal, INF/INFINITY, and
147 NAN/NAN(\fIn-char-sequence\fR) forms are recognized. In C99/SUSv3 mode,
148 hexadecimal strings are also recognized.
149 .sp
150 .LP
151 In default mode for \fBstrtod()\fR, the \fIn-char-sequence\fR in the
152 NAN(\fIn-char-equence\fR) form can contain any character except ')' (right
153 parenthesis) or '\e0' (null). In C99/SUSv3 mode, the \fIn-char-sequence\fR can
154 contain only upper and lower case letters, digits, and '_' (underscore).
155 .sp
156 .LP
157 The \fBstrtof()\fR and \fBstrtold()\fR functions always function in
158 C99/SUSv3-conformant mode.
159 .sp
160 .LP
161 The subject sequence is defined as the longest initial subsequence of the input
162 string, starting with the first non-white-space character, that is of the
163 expected form. The subject sequence contains no characters if the input string
164 is not of the expected form.
165 .sp
166 .LP
167 If the subject sequence has the expected form for a floating-point number, the
168 sequence of characters starting with the first digit or the decimal-point
169 character (whichever occurs first) is interpreted as a floating constant of the
170 C language, except that the radix character is used in place of a period, and
171 that if neither an exponent part nor a radix character appears in a decimal
172 floating-point number, or if a binary exponent part does not appear in a
173 hexadecimal floating-point number, an exponent part of the appropriate type
174 with value zero is assumed to follow the last digit in the string. If the
175 subject sequence begins with a minus sign, the sequence is interpreted as
176 negated. A character sequence INF or INFINITY is interpreted as an infinity. A
177 character sequence NAN or NAN(\fIn-char-sequence\fR(\fIopt\fR)) is interpreted
178 as a quiet NaN. A pointer to the final string is stored in the object pointed
179 to by \fIendptr\fR, provided that \fIendptr\fR is not a null pointer.
180 .sp
181 .LP
182 If the subject sequence has either the decimal or hexadecimal form, the value
183 resulting from the conversion is rounded correctly according to the prevailing
184 floating point rounding direction mode. The conversion also raises floating
185 point inexact, underflow, or overflow exceptions as appropriate.
186 .sp
187 .LP
188 The radix character is defined in the program's locale (category
189 \fBLC_NUMERIC\fR). In the POSIX locale, or in a locale where the radix
190 character is not defined, the radix character defaults to a period ('.').
191 .sp
192 .LP
193 If the subject sequence is empty or does not have the expected form, no
194 conversion is performed; the value of \fInptr\fR is stored in the object
195 pointed to by \fIendptr\fR, provided that \fIendptr\fR is not a null pointer.
196 .sp
197 .LP
198 The \fBstrtod()\fR function does not change the setting of \fBerrno\fR if
199 successful.
200 .sp
201 .LP
202 The \fBatof(\fR\fIstr\fR\fB)\fR function call is equivalent to
203 \fBstrtod(\fR\fInptr\fR\fB, (char **)NULL)\fR.
204 .SH RETURN VALUES
205 .sp
206 .LP
207 Upon successful completion, these functions return the converted value. If no
208 conversion could be performed, \fB0\fR is returned.
209 .sp
210 .LP
211 If the correct value is outside the range of representable values,
212 \fB\(+-HUGE_VAL\fR, \fB\(+-HUGE_VALF\fR, or \fB\(+-HUGE_VALL\fR is returned
213 (according to the sign of the value), a floating point overflow exception is
214 raised, and \fBerrno\fR is set to \fBERANGE\fR.
215 .sp
216 .LP
217 If the correct value would cause an underflow, the correctly rounded result
218 (which may be normal, subnormal, or zero) is returned, a floating point
219 underflow exception is raised, and \fBerrno\fR is set to \fBERANGE\fR.
220 .SH ERRORS
221 .sp
222 .LP
223 These functions will fail if:
224 .sp
225 .ne 2
226 .na
227 \fB\fBERANGE\fR\fR
228 .ad
229 .RS 10n
230 The value to be returned would cause overflow or underflow
231 .RE
232
233 .sp
234 .LP
235 These functions may fail if:
236 .sp
237 .ne 2
238 .na
239 \fB\fBEINVAL\fR\fR
240 .ad
241 .RS 10n
242 No conversion could be performed.
243 .RE
244
245 .SH USAGE
246 .sp
247 .LP
248 Since 0 is returned on error and is also a valid return on success, an
249 application wishing to check for error situations should set \fBerrno\fR to 0,
250 then call \fBstrtod()\fR, \fBstrtof()\fR, or \fBstrtold()\fR, then check
251 \fBerrno\fR.
252 .sp
253 .LP
254 The changes to \fBstrtod()\fR introduced by the ISO/IEC 9899: 1999 standard can
255 alter the behavior of well-formed applications complying with the ISO/IEC 9899:
256 1990 standard and thus earlier versions of IEEE Std 1003.1-200x. One such
257 example would be:
258 .sp
259 .in +2
260 .nf
261 int
262 what_kind_of_number (char *s)
263 {
264 char *endp;
265 double d;
266 long l;
267 d = strtod(s, &endp);
268 if (s != endp && *endp == '\e0')
269 printf("It's a float with value %g\en", d);
270 else
271 {
272 l = strtol(s, &endp, 0);
273 if (s != endp && *endp == '\e0')
274 printf("It's an integer with value %ld\en", 1);
275 else
276 return 1;
277 }
278 return 0;
279 }
280 .fi
281 .in -2
282
283 .sp
284 .LP
285 If the function is called with:
286 .sp
287 .in +2
288 .nf
289 what_kind_of_number ("0x10")
290 .fi
291 .in -2
292
293 .sp
294 .LP
295 an ISO/IEC 9899: 1990 standard-compliant library will result in the function
296 printing:
297 .sp
298 .in +2
299 .nf
300 It's an integer with value 16
301 .fi
302 .in -2
303
304 .sp
305 .LP
306 With the ISO/IEC 9899: 1999 standard, the result is:
307 .sp
308 .in +2
309 .nf
310 It's a float with value 16
311 .fi
312 .in -2
313
314 .sp
315 .LP
316 The change in behavior is due to the inclusion of floating-point numbers in
317 hexadecimal notation without requiring that either a decimal point or the
318 binary exponent be present.
319 .SH ATTRIBUTES
320 .sp
321 .LP
322 See \fBattributes\fR(5) for descriptions of the following attributes:
323 .sp
324
325 .sp
326 .TS
327 box;
328 c | c
329 l | l .
330 ATTRIBUTE TYPE ATTRIBUTE VALUE
331 _
332 CSI Enabled
333 _
334 Interface Stability Standard
335 _
336 MT-Level MT-Safe with exceptions
337 .TE
338
339 .SH SEE ALSO
340 .sp
341 .LP
342 \fBisspace\fR(3C), \fBlocaleconv\fR(3C), \fBscanf\fR(3C), \fBsetlocale\fR(3C),
343 \fBstrtol\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)
344 .SH NOTES
345 .sp
346 .LP
347 The \fBstrtod()\fR and \fBatof()\fR functions can be used safely in
348 multithreaded applications, as long as \fBsetlocale\fR(3C) is not called to
349 change the locale.
350 .sp
351 .LP
352 The DESCRIPTION and RETURN VALUES sections above are very similar to the
353 wording used by the Single UNIX Specification version 2 (SUSv2) and the 1989 C
354 Standard to describe the behavior of the \fBstrtod()\fR function. Since some
355 users have reported that they find the description confusing, the following
356 notes might be helpful.
357 .RS +4
358 .TP
359 1.
360 The \fBstrtod()\fR function does not modify the string pointed to by
361 \fIstr\fR and does not \fBmalloc()\fR space to hold the decomposed portions of
362 the input string.
363 .RE
364 .RS +4
365 .TP
366 2.
367 If \fIendptr\fR is not \fB(char **)NULL\fR, \fBstrtod()\fR will set the
368 pointer pointed to by \fIendptr\fR to the first byte of the "final string of
369 unrecognized characters". (If all input characters were processed, the pointer
370 pointed to by \fIendptr\fR will be set to point to the null character at the
371 end of the input string.)
372 .RE
373 .RS +4
374 .TP
375 3.
376 If \fBstrtod()\fR returns 0.0, one of the following occurred:
377 .RS +4
378 .TP
379 a.
380 The "subject sequence" was not an empty string, but evaluated to 0.0. (In
381 this case, \fBerrno\fR will be left unchanged.)
382 .RE
383 .RS +4
384 .TP
385 b.
386 The "subject sequence" was an empty string . In this case, \fBerrno\fR will
387 be left unchanged. (The Single UNIX Specification version 2 allows \fBerrno\fR
388 to be set to \fBEINVAL\fR or to be left unchanged. The C Standard does not
389 specify any specific behavior in this case.)
390 .RE
391 .RS +4
392 .TP
393 c.
394 The "subject sequence" specified a numeric value whose conversion resulted
395 in a floating point underflow. In this case, an underflow exception is raised
396 and \fBerrno\fR is set to \fBERANGE\fR.
397 .RE
398 Note that the standards do not require that implementations distinguish between
399 these three cases. An application can determine case (b) by making sure that
400 there are no leading white-space characters in the string pointed to by
401 \fIstr\fR and giving \fBstrtod()\fR an \fIendptr\fR that is not \fB(char
402 **)NULL\fR. If \fIendptr\fR points to the first character of \fIstr\fR when
403 \fBstrtod()\fR returns, you have detected case (b). Case (c) can be detected
404 by examining the underflow flag or by looking for a non-zero digit before the
405 exponent part of the "subject sequence". Note, however, that the decimal-point
406 character is locale-dependent.
407 .RE
408 .RS +4
409 .TP
410 4.
411 If \fBstrtod()\fR returns \fB+HUGE_VAL\fR or \fB\(miHUGE_VAL\fR, one of the
412 following occurred:
413 .RS +4
414 .TP
415 a.
416 If \fB+HUGE_VAL\fR is returned and \fBerrno\fR is set to \fBERANGE\fR, a
417 floating point overflow occurred while processing a positive value, causing a
418 floating point overflow exception to be raised.
419 .RE
420 .RS +4
421 .TP
422 b.
423 If \fB\(miHUGE_VAL\fR is returned and \fBerrno\fR is set to \fBERANGE\fR, a
424 floating point overflow occurred while processing a negative value, causing a
425 floating point overflow exception to be raised.
426 .RE
427 .RS +4
428 .TP
429 c.
430 If \fBstrtod()\fR does not set \fBerrno\fR to \fBERANGE\fR, the value
431 specified by the "subject string" converted to \fB+HUGE_VAL\fR or
432 \fB\(miHUGE_VAL\fR, respectively.
433 .RE
434 Note that if \fBerrno\fR is set to \fBERANGE\fR when \fBstrtod()\fR is called,
435 case (c) can be distinguished from cases (a) and (b) by examining either
436 \fBERANGE\fR or the overflow flag.
437 .RE