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