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