1 /* Copyright (C) 1991, 1992, 1997, 1999, 2003, 2006 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
26 # define IN_CTYPE_DOMAIN(c) 1
28 # define IN_CTYPE_DOMAIN(c) isascii(c)
31 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
32 #define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
33 #define TOLOWER(c) (IN_CTYPE_DOMAIN (c) ? tolower(c) : (c))
41 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
42 character after the last one used in the number is put in *ENDPTR. */
44 strtod (const char *nptr, char **endptr)
46 register const char *s;
49 /* The number so far. */
52 int got_dot; /* Found a decimal point. */
53 int got_digit; /* Seen any digits. */
55 /* The exponent of the number. */
71 sign = *s == '-' ? -1 : 1;
72 if (*s == '-' || *s == '+')
85 /* Make sure that multiplication by 10 will not overflow. */
86 if (num > DBL_MAX * 0.1)
87 /* The value of the digit doesn't matter, since we have already
88 gotten as many digits as can be represented in a `double'.
89 This doesn't necessarily mean the result will overflow.
90 The exponent may reduce it to within range.
92 We just need to record that there was another
93 digit so that we can multiply by 10 later. */
96 num = (num * 10.0) + (*s - '0');
98 /* Keep track of the number of digits after the decimal point.
99 If we just divided by 10 here, we would lose precision. */
103 else if (!got_dot && *s == '.')
104 /* Record that we have found the decimal point. */
107 /* Any other character terminates the number. */
114 if (TOLOWER (*s) == 'e')
116 /* Get the exponent specified after the `e' or `E'. */
123 exp = strtol (s, &end, 10);
126 /* The exponent overflowed a `long int'. It is probably a safe
127 assumption that an exponent that cannot be represented by
128 a `long int' exceeds the limits of a `double'. */
137 /* There was no exponent. Reset END to point to
138 the 'e' or 'E', so *ENDPTR will be set there. */
139 end = (char *) s - 1;
146 *endptr = (char *) s;
151 /* Multiply NUM by 10 to the EXPONENT power,
152 checking for overflow and underflow. */
156 if (num < DBL_MIN * pow (10.0, (double) -exponent))
159 else if (exponent > 0)
161 if (num > DBL_MAX * pow (10.0, (double) -exponent))
165 num *= pow (10.0, (double) exponent);
170 /* Return an overflow error. */
172 return HUGE_VAL * sign;
175 /* Return an underflow error. */
177 *endptr = (char *) nptr;
182 /* There was no number. */
184 *endptr = (char *) nptr;