1 /* strtod.c - convert string to double-precision floating-point value. */
3 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
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.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
32 #include <chartypes.h>
38 # define DBL_MAX 1.7976931348623159e+308
39 # define DBL_MIN 2.2250738585072010e-308
49 # define HUGE_VAL HUGE
52 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
53 character after the last one used in the number is put in *ENDPTR. */
59 register const char *s;
62 /* The number so far. */
65 int got_dot; /* Found a decimal point. */
66 int got_digit; /* Seen any digits. */
68 /* The exponent of the number. */
80 while (ISSPACE ((unsigned char)*s))
84 sign = *s == '-' ? -1 : 1;
85 if (*s == '-' || *s == '+')
98 /* Make sure that multiplication by 10 will not overflow. */
99 if (num > DBL_MAX * 0.1)
100 /* The value of the digit doesn't matter, since we have already
101 gotten as many digits as can be represented in a `double'.
102 This doesn't necessarily mean the result will overflow.
103 The exponent may reduce it to within range.
105 We just need to record that there was another
106 digit so that we can multiply by 10 later. */
109 num = (num * 10.0) + (*s - '0');
111 /* Keep track of the number of digits after the decimal point.
112 If we just divided by 10 here, we would lose precision. */
116 else if (!got_dot && *s == '.')
117 /* Record that we have found the decimal point. */
120 /* Any other character terminates the number. */
127 if (TOLOWER ((unsigned char)*s) == 'e')
129 /* Get the exponent specified after the `e' or `E'. */
136 exp = strtol (s, &end, 10);
139 /* The exponent overflowed a `long int'. It is probably a safe
140 assumption that an exponent that cannot be represented by
141 a `long int' exceeds the limits of a `double'. */
150 /* There was no exponent. Reset END to point to
151 the 'e' or 'E', so *ENDPTR will be set there. */
152 end = (char *) s - 1;
159 *endptr = (char *) s;
164 /* Multiply NUM by 10 to the EXPONENT power,
165 checking for overflow and underflow. */
169 if (num < DBL_MIN * pow (10.0, (double) -exponent))
172 else if (exponent > 0)
174 if (num > DBL_MAX * pow (10.0, (double) -exponent))
178 num *= pow (10.0, (double) exponent);
183 /* Return an overflow error. */
185 return HUGE_VAL * sign;
188 /* Return an underflow error. */
190 *endptr = (char *) nptr;
195 /* There was no number. */
197 *endptr = (char *) nptr;
201 #endif /* !HAVE_STRTOD */