]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/sh/strtod.c
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / lib / sh / strtod.c
CommitLineData
3185942a
JA
1/* strtod.c - convert string to double-precision floating-point value. */
2
cce855bc
JA
3/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4
3185942a
JA
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
cce855bc 8 it under the terms of the GNU General Public License as published by
3185942a
JA
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
cce855bc 11
3185942a 12 Bash is distributed in the hope that it will be useful,
cce855bc
JA
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
3185942a
JA
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
cce855bc
JA
20
21#if HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#ifndef HAVE_STRTOD
26
27#include <errno.h>
28#ifndef errno
29extern int errno;
30#endif
31
f73dda09 32#include <chartypes.h>
cce855bc
JA
33#include <math.h>
34
35#if HAVE_FLOAT_H
36# include <float.h>
37#else
38# define DBL_MAX 1.7976931348623159e+308
39# define DBL_MIN 2.2250738585072010e-308
40#endif
41
42#include <bashansi.h>
43
44#ifndef NULL
45# define NULL 0
46#endif
47
48#ifndef HUGE_VAL
49# define HUGE_VAL HUGE
50#endif
51
8868edaf
CR
52#ifndef locale_decpoint
53extern int locale_decpoint PARAMS((void));
54#endif
55
cce855bc
JA
56/* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
57 character after the last one used in the number is put in *ENDPTR. */
58double
59strtod (nptr, endptr)
60 const char *nptr;
61 char **endptr;
62{
63 register const char *s;
64 short sign;
65
66 /* The number so far. */
67 double num;
68
8868edaf 69 int radixchar;
cce855bc
JA
70 int got_dot; /* Found a decimal point. */
71 int got_digit; /* Seen any digits. */
72
73 /* The exponent of the number. */
74 long int exponent;
75
76 if (nptr == NULL)
77 {
78 errno = EINVAL;
79 goto noconv;
80 }
81
82 s = nptr;
83
84 /* Eat whitespace. */
f73dda09 85 while (ISSPACE ((unsigned char)*s))
cce855bc
JA
86 ++s;
87
88 /* Get the sign. */
89 sign = *s == '-' ? -1 : 1;
90 if (*s == '-' || *s == '+')
91 ++s;
92
8868edaf 93 radixchar = locale_decpoint ();
cce855bc
JA
94 num = 0.0;
95 got_dot = 0;
96 got_digit = 0;
97 exponent = 0;
98 for (;; ++s)
99 {
f73dda09 100 if (DIGIT (*s))
cce855bc
JA
101 {
102 got_digit = 1;
103
104 /* Make sure that multiplication by 10 will not overflow. */
105 if (num > DBL_MAX * 0.1)
106 /* The value of the digit doesn't matter, since we have already
107 gotten as many digits as can be represented in a `double'.
108 This doesn't necessarily mean the result will overflow.
109 The exponent may reduce it to within range.
110
111 We just need to record that there was another
112 digit so that we can multiply by 10 later. */
113 ++exponent;
114 else
115 num = (num * 10.0) + (*s - '0');
116
117 /* Keep track of the number of digits after the decimal point.
118 If we just divided by 10 here, we would lose precision. */
119 if (got_dot)
120 --exponent;
121 }
8868edaf 122 else if (!got_dot && *s == radixchar)
cce855bc
JA
123 /* Record that we have found the decimal point. */
124 got_dot = 1;
125 else
126 /* Any other character terminates the number. */
127 break;
128 }
129
130 if (!got_digit)
131 goto noconv;
132
f73dda09 133 if (TOLOWER ((unsigned char)*s) == 'e')
cce855bc
JA
134 {
135 /* Get the exponent specified after the `e' or `E'. */
136 int save = errno;
137 char *end;
138 long int exp;
139
140 errno = 0;
141 ++s;
142 exp = strtol (s, &end, 10);
143 if (errno == ERANGE)
144 {
145 /* The exponent overflowed a `long int'. It is probably a safe
146 assumption that an exponent that cannot be represented by
147 a `long int' exceeds the limits of a `double'. */
148 if (endptr != NULL)
149 *endptr = end;
150 if (exp < 0)
151 goto underflow;
152 else
153 goto overflow;
154 }
155 else if (end == s)
156 /* There was no exponent. Reset END to point to
157 the 'e' or 'E', so *ENDPTR will be set there. */
158 end = (char *) s - 1;
159 errno = save;
160 s = end;
161 exponent += exp;
162 }
163
164 if (endptr != NULL)
165 *endptr = (char *) s;
166
167 if (num == 0.0)
168 return 0.0;
169
170 /* Multiply NUM by 10 to the EXPONENT power,
171 checking for overflow and underflow. */
172
173 if (exponent < 0)
174 {
175 if (num < DBL_MIN * pow (10.0, (double) -exponent))
176 goto underflow;
177 }
178 else if (exponent > 0)
179 {
180 if (num > DBL_MAX * pow (10.0, (double) -exponent))
181 goto overflow;
182 }
183
184 num *= pow (10.0, (double) exponent);
185
186 return num * sign;
187
188overflow:
189 /* Return an overflow error. */
190 errno = ERANGE;
191 return HUGE_VAL * sign;
192
193underflow:
194 /* Return an underflow error. */
195 if (endptr != NULL)
196 *endptr = (char *) nptr;
197 errno = ERANGE;
198 return 0.0;
199
200noconv:
201 /* There was no number. */
202 if (endptr != NULL)
203 *endptr = (char *) nptr;
204 return 0.0;
205}
206
207#endif /* !HAVE_STRTOD */