xref: /freebsd/lib/libc/stdlib/strtod.3 (revision 6ae1554a5d9b318f8ad53ccc39fa5a961403da73)
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.\"     @(#)strtod.3	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD$
34.\"
35.Dd May 11, 2010
36.Dt STRTOD 3
37.Os
38.Sh NAME
39.Nm strtod , strtof , strtold
40.Nd convert
41.Tn ASCII
42string to floating point
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdlib.h
47.Ft double
48.Fn strtod "const char * restrict nptr" "char ** restrict endptr"
49.Ft float
50.Fn strtof "const char * restrict nptr" "char ** restrict endptr"
51.Ft "long double"
52.Fn strtold "const char * restrict nptr" "char ** restrict endptr"
53.Sh DESCRIPTION
54These conversion
55functions convert the initial portion of the string
56pointed to by
57.Fa nptr
58to
59.Vt double ,
60.Vt float ,
61and
62.Vt "long double"
63representation, respectively.
64.Pp
65The expected form of the string is an optional plus (``+'') or minus
66sign (``\-'') followed by either:
67.Bl -bullet
68.It
69a decimal significand consisting of a sequence of decimal digits
70optionally containing a decimal-point character, or
71.It
72a hexadecimal significand consisting of a ``0X'' or ``0x'' followed
73by a sequence of hexadecimal digits optionally containing a
74decimal-point character.
75.El
76.Pp
77In both cases, the significand may be optionally followed by an
78exponent.
79An exponent consists of an ``E'' or ``e'' (for decimal
80constants) or a ``P'' or ``p'' (for hexadecimal constants),
81followed by an optional plus or minus sign, followed by a
82sequence of decimal digits.
83For decimal constants, the exponent indicates the power of 10 by
84which the significand should be scaled.
85For hexadecimal constants, the scaling is instead done by powers
86of 2.
87.Pp
88Alternatively, if the portion of the string following the optional
89plus or minus sign begins with
90.Dq INFINITY
91or
92.Dq NAN ,
93ignoring case, it is interpreted as an infinity or a quiet \*(Na,
94respectively.
95The syntax
96.Dq Xo Pf NAN( Ar "s" ) Xc ,
97where
98.Ar s
99is an alphanumeric string, produces the same value as the call
100.Fo nan
101.Qq Ar s Ns
102.Fc
103(respectively,
104.Fo nanf
105.Qq Ar s Ns
106.Fc
107and
108.Fo nanl
109.Qq Ar s Ns
110.Fc . )
111.Pp
112In any of the above cases, leading white-space characters in the
113string (as defined by the
114.Xr isspace 3
115function) are skipped.
116The decimal point
117character is defined in the program's locale (category
118.Dv LC_NUMERIC ) .
119.Sh RETURN VALUES
120The
121.Fn strtod ,
122.Fn strtof ,
123and
124.Fn strtold
125functions return the converted value, if any.
126.Pp
127If
128.Fa endptr
129is not
130.Dv NULL ,
131a pointer to the character after the last character used
132in the conversion is stored in the location referenced by
133.Fa endptr .
134.Pp
135If no conversion is performed, zero is returned and the value of
136.Fa nptr
137is stored in the location referenced by
138.Fa endptr .
139.Pp
140If the correct value would cause overflow, plus or minus
141.Dv HUGE_VAL ,
142.Dv HUGE_VALF ,
143or
144.Dv HUGE_VALL
145is returned (according to the sign and type of the return value), and
146.Er ERANGE
147is stored in
148.Va errno .
149If the correct value would cause underflow, zero is
150returned and
151.Er ERANGE
152is stored in
153.Va errno .
154.Sh ERRORS
155.Bl -tag -width Er
156.It Bq Er ERANGE
157Overflow or underflow occurred.
158.El
159.Sh SEE ALSO
160.Xr atof 3 ,
161.Xr atoi 3 ,
162.Xr atol 3 ,
163.Xr nan 3 ,
164.Xr strtol 3 ,
165.Xr strtoul 3 ,
166.Xr wcstod 3
167.Sh STANDARDS
168The
169.Fn strtod
170function
171conforms to
172.St -isoC-99 .
173.Sh AUTHORS
174The author of this software is
175.An David M. Gay .
176.Bd -literal
177Copyright (c) 1998 by Lucent Technologies
178All Rights Reserved
179
180Permission to use, copy, modify, and distribute this software and
181its documentation for any purpose and without fee is hereby
182granted, provided that the above copyright notice appear in all
183copies and that both that the copyright notice and this
184permission notice and warranty disclaimer appear in supporting
185documentation, and that the name of Lucent or any of its entities
186not be used in advertising or publicity pertaining to
187distribution of the software without specific, written prior
188permission.
189
190LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
191INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
192IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
193SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
194WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
195IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
196ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
197THIS SOFTWARE.
198.Ed
199