xref: /openbsd/lib/libc/stdlib/strtod.3 (revision 274d7c50)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" 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.\"	$OpenBSD: strtod.3,v 1.22 2019/01/16 12:55:49 schwarze Exp $
33.\"
34.Dd $Mdocdate: January 16 2019 $
35.Dt STRTOD 3
36.Os
37.Sh NAME
38.Nm strtod ,
39.Nm strtof ,
40.Nm strtold
41.Nd convert ASCII string to double, float or long double
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft double
45.Fn strtod "const char *nptr" "char **endptr"
46.Pp
47.Ft float
48.Fn strtof "const char *nptr" "char **endptr"
49.Pp
50.Ft long double
51.Fn strtold "const char *nptr" "char **endptr"
52.Sh DESCRIPTION
53The
54.Fn strtod
55function converts the initial portion of the string pointed to by
56.Fa nptr
57to
58.Li double
59representation.
60The
61.Fn strtof
62function converts the initial portion of the string pointed to by
63.Fa nptr
64to
65.Li float
66representation.
67The
68.Fn strtold
69function converts the initial portion of the string pointed to by
70.Fa nptr
71to
72.Li long double
73representation.
74.Pp
75The expected form of the string is an optional plus
76.Pq Ql +
77or minus sign
78.Pq Ql -
79followed by a sequence of digits optionally containing
80a decimal-point character, optionally followed by an exponent.
81An exponent consists of an
82.Sq E
83or
84.Sq e ,
85followed by an optional plus or minus sign, followed by a sequence of digits.
86.Pp
87Alternatively, if the portion of the string following the optional
88plus or minus sign begins with
89.Dq INF
90or
91.Dq NAN ,
92ignoring case, it is interpreted as an infinity or a quiet \*(Na,
93respectively.
94The syntax
95.Dq NAN Ns Pq Ar s ,
96where
97.Ar s
98is an alphanumeric string, produces the same value as the call
99.Fo nan
100.Qq Ar s Ns
101.Fc
102(respectively,
103.Fo nanf
104.Qq Ar s Ns
105.Fc
106and
107.Fo nanl
108.Qq Ar s Ns
109.Fc ) .
110.Pp
111In any of the above cases, leading whitespace characters in the
112string (as defined by the
113.Xr isspace 3
114function) are skipped.
115.Sh RETURN VALUES
116The
117.Fn strtod ,
118.Fn strtof
119and
120.Fn strtold
121functions return the converted value, if any.
122.Pp
123If
124.Fa endptr
125is not
126.Dv NULL ,
127a pointer to the character after the last character used
128in the conversion is stored in the location referenced by
129.Fa endptr .
130.Pp
131If no conversion is performed, zero is returned and the value of
132.Fa nptr
133is stored in the location referenced by
134.Fa endptr .
135.Pp
136If the correct value would cause overflow, plus or minus
137.Dv HUGE_VAL
138is returned (according to the sign of the value), and
139.Er ERANGE
140is stored in
141.Va errno .
142If the correct value would cause underflow, zero is returned and
143.Er ERANGE
144is stored in
145.Va errno .
146.Sh ERRORS
147.Bl -tag -width Er
148.It Bq Er ERANGE
149Overflow or underflow occurred.
150.El
151.Sh SEE ALSO
152.Xr atof 3 ,
153.Xr atoi 3 ,
154.Xr atol 3 ,
155.Xr strtol 3 ,
156.Xr strtoul 3
157.Sh STANDARDS
158The
159.Fn strtod
160function conforms to
161.St -ansiC-89 .
162The
163.Fn strtof
164and
165.Fn strtold
166functions conform to
167.St -isoC-99 .
168.Sh CAVEATS
169On systems other than
170.Ox ,
171the
172.Dv LC_NUMERIC
173.Xr locale 1
174category can cause parsing failures; see CAVEATS in
175.Xr setlocale 3
176for details.
177