1 /* Copyright (C) 1991, 1992, 1997, 1999, 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
29 # define IN_CTYPE_DOMAIN(c) 1
31 # define IN_CTYPE_DOMAIN(c) isascii(c)
34 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
35 #define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
36 #define TOLOWER(c) (IN_CTYPE_DOMAIN (c) ? tolower(c) : (c))
44 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
45 character after the last one used in the number is put in *ENDPTR. */
47 strtod (const char *nptr
, char **endptr
)
49 register const char *s
;
52 /* The number so far. */
55 int got_dot
; /* Found a decimal point. */
56 int got_digit
; /* Seen any digits. */
58 /* The exponent of the number. */
74 sign
= *s
== '-' ? -1 : 1;
75 if (*s
== '-' || *s
== '+')
88 /* Make sure that multiplication by 10 will not overflow. */
89 if (num
> DBL_MAX
* 0.1)
90 /* The value of the digit doesn't matter, since we have already
91 gotten as many digits as can be represented in a `double'.
92 This doesn't necessarily mean the result will overflow.
93 The exponent may reduce it to within range.
95 We just need to record that there was another
96 digit so that we can multiply by 10 later. */
99 num
= (num
* 10.0) + (*s
- '0');
101 /* Keep track of the number of digits after the decimal point.
102 If we just divided by 10 here, we would lose precision. */
106 else if (!got_dot
&& *s
== '.')
107 /* Record that we have found the decimal point. */
110 /* Any other character terminates the number. */
117 if (TOLOWER (*s
) == 'e')
119 /* Get the exponent specified after the `e' or `E'. */
126 exp
= strtol (s
, &end
, 10);
129 /* The exponent overflowed a `long int'. It is probably a safe
130 assumption that an exponent that cannot be represented by
131 a `long int' exceeds the limits of a `double'. */
140 /* There was no exponent. Reset END to point to
141 the 'e' or 'E', so *ENDPTR will be set there. */
142 end
= (char *) s
- 1;
149 *endptr
= (char *) s
;
154 /* Multiply NUM by 10 to the EXPONENT power,
155 checking for overflow and underflow. */
159 if (num
< DBL_MIN
* pow (10.0, (double) -exponent
))
162 else if (exponent
> 0)
164 if (num
> DBL_MAX
* pow (10.0, (double) -exponent
))
168 num
*= pow (10.0, (double) exponent
);
173 /* Return an overflow error. */
175 return HUGE_VAL
* sign
;
178 /* Return an underflow error. */
180 *endptr
= (char *) nptr
;
185 /* There was no number. */
187 *endptr
= (char *) nptr
;