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