xref: /freebsd/lib/libc/stdlib/strtod.3 (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd May 11, 2010
33.Dt STRTOD 3
34.Os
35.Sh NAME
36.Nm strtod , strtof , strtold
37.Nd convert
38.Tn ASCII
39string to floating point
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft double
45.Fn strtod "const char * restrict nptr" "char ** restrict endptr"
46.Ft float
47.Fn strtof "const char * restrict nptr" "char ** restrict endptr"
48.Ft "long double"
49.Fn strtold "const char * restrict nptr" "char ** restrict endptr"
50.Sh DESCRIPTION
51These conversion
52functions convert the initial portion of the string
53pointed to by
54.Fa nptr
55to
56.Vt double ,
57.Vt float ,
58and
59.Vt "long double"
60representation, respectively.
61.Pp
62The expected form of the string is an optional plus (``+'') or minus
63sign (``\-'') followed by either:
64.Bl -bullet
65.It
66a decimal significand consisting of a sequence of decimal digits
67optionally containing a decimal-point character, or
68.It
69a hexadecimal significand consisting of a ``0X'' or ``0x'' followed
70by a sequence of hexadecimal digits optionally containing a
71decimal-point character.
72.El
73.Pp
74In both cases, the significand may be optionally followed by an
75exponent.
76An exponent consists of an ``E'' or ``e'' (for decimal
77constants) or a ``P'' or ``p'' (for hexadecimal constants),
78followed by an optional plus or minus sign, followed by a
79sequence of decimal digits.
80For decimal constants, the exponent indicates the power of 10 by
81which the significand should be scaled.
82For hexadecimal constants, the scaling is instead done by powers
83of 2.
84.Pp
85Alternatively, if the portion of the string following the optional
86plus or minus sign begins with
87.Dq INFINITY
88or
89.Dq NAN ,
90ignoring case, it is interpreted as an infinity or a quiet \*(Na,
91respectively.
92The syntax
93.Dq Xo Pf NAN( Ar "s" ) Xc ,
94where
95.Ar s
96is an alphanumeric string, produces the same value as the call
97.Fo nan
98.Qq Ar s Ns
99.Fc
100(respectively,
101.Fo nanf
102.Qq Ar s Ns
103.Fc
104and
105.Fo nanl
106.Qq Ar s Ns
107.Fc . )
108.Pp
109In any of the above cases, leading white-space characters in the
110string (as defined by the
111.Xr isspace 3
112function) are skipped.
113The decimal point
114character is defined in the program's locale (category
115.Dv LC_NUMERIC ) .
116.Sh RETURN VALUES
117The
118.Fn strtod ,
119.Fn strtof ,
120and
121.Fn strtold
122functions return the converted value, if any.
123.Pp
124If
125.Fa endptr
126is not
127.Dv NULL ,
128a pointer to the character after the last character used
129in the conversion is stored in the location referenced by
130.Fa endptr .
131.Pp
132If no conversion is performed, zero is returned and the value of
133.Fa nptr
134is stored in the location referenced by
135.Fa endptr .
136.Pp
137If the correct value would cause overflow, plus or minus
138.Dv HUGE_VAL ,
139.Dv HUGE_VALF ,
140or
141.Dv HUGE_VALL
142is returned (according to the sign and type of the return value), and
143.Er ERANGE
144is stored in
145.Va errno .
146If the correct value would cause underflow, zero is
147returned and
148.Er ERANGE
149is stored in
150.Va errno .
151.Sh ERRORS
152.Bl -tag -width Er
153.It Bq Er ERANGE
154Overflow or underflow occurred.
155.El
156.Sh SEE ALSO
157.Xr atof 3 ,
158.Xr atoi 3 ,
159.Xr atol 3 ,
160.Xr nan 3 ,
161.Xr strtol 3 ,
162.Xr strtoul 3 ,
163.Xr wcstod 3
164.Sh STANDARDS
165The
166.Fn strtod
167function
168conforms to
169.St -isoC-99 .
170.Sh AUTHORS
171The author of this software is
172.An David M. Gay .
173.Bd -literal
174Copyright (c) 1998 by Lucent Technologies
175All Rights Reserved
176
177Permission to use, copy, modify, and distribute this software and
178its documentation for any purpose and without fee is hereby
179granted, provided that the above copyright notice appear in all
180copies and that both that the copyright notice and this
181permission notice and warranty disclaimer appear in supporting
182documentation, and that the name of Lucent or any of its entities
183not be used in advertising or publicity pertaining to
184distribution of the software without specific, written prior
185permission.
186
187LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
188INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
189IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
190SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
191WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
192IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
193ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
194THIS SOFTWARE.
195.Ed
196