* strtod.c [!defined errno]: Assume errno.h declares errno.
[gnulib.git] / lib / strtod.c
1 /* Copyright (C) 1991, 1992, 1997, 1999, 2003, 2006 Free Software Foundation, Inc.
2
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)
6    any later version.
7
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.
12
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.  */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #include <errno.h>
22
23 #include <ctype.h>
24
25 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
26 # define IN_CTYPE_DOMAIN(c) 1
27 #else
28 # define IN_CTYPE_DOMAIN(c) isascii(c)
29 #endif
30
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))
34
35 #include <math.h>
36
37 #include <float.h>
38 #include <stdlib.h>
39 #include <string.h>
40
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.  */
43 double
44 strtod (const char *nptr, char **endptr)
45 {
46   register const char *s;
47   short int sign;
48
49   /* The number so far.  */
50   double num;
51
52   int got_dot;                  /* Found a decimal point.  */
53   int got_digit;                /* Seen any digits.  */
54
55   /* The exponent of the number.  */
56   long int exponent;
57
58   if (nptr == NULL)
59     {
60       errno = EINVAL;
61       goto noconv;
62     }
63
64   s = nptr;
65
66   /* Eat whitespace.  */
67   while (ISSPACE (*s))
68     ++s;
69
70   /* Get the sign.  */
71   sign = *s == '-' ? -1 : 1;
72   if (*s == '-' || *s == '+')
73     ++s;
74
75   num = 0.0;
76   got_dot = 0;
77   got_digit = 0;
78   exponent = 0;
79   for (;; ++s)
80     {
81       if (ISDIGIT (*s))
82         {
83           got_digit = 1;
84
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.
91
92                We just need to record that there was another
93                digit so that we can multiply by 10 later.  */
94             ++exponent;
95           else
96             num = (num * 10.0) + (*s - '0');
97
98           /* Keep track of the number of digits after the decimal point.
99              If we just divided by 10 here, we would lose precision.  */
100           if (got_dot)
101             --exponent;
102         }
103       else if (!got_dot && *s == '.')
104         /* Record that we have found the decimal point.  */
105         got_dot = 1;
106       else
107         /* Any other character terminates the number.  */
108         break;
109     }
110
111   if (!got_digit)
112     goto noconv;
113
114   if (TOLOWER (*s) == 'e')
115     {
116       /* Get the exponent specified after the `e' or `E'.  */
117       int save = errno;
118       char *end;
119       long int exp;
120
121       errno = 0;
122       ++s;
123       exp = strtol (s, &end, 10);
124       if (errno == ERANGE)
125         {
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'.  */
129           if (endptr != NULL)
130             *endptr = end;
131           if (exp < 0)
132             goto underflow;
133           else
134             goto overflow;
135         }
136       else if (end == s)
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;
140       errno = save;
141       s = end;
142       exponent += exp;
143     }
144
145   if (endptr != NULL)
146     *endptr = (char *) s;
147
148   if (num == 0.0)
149     return 0.0;
150
151   /* Multiply NUM by 10 to the EXPONENT power,
152      checking for overflow and underflow.  */
153
154   if (exponent < 0)
155     {
156       if (num < DBL_MIN * pow (10.0, (double) -exponent))
157         goto underflow;
158     }
159   else if (exponent > 0)
160     {
161       if (num > DBL_MAX * pow (10.0, (double) -exponent))
162         goto overflow;
163     }
164
165   num *= pow (10.0, (double) exponent);
166
167   return num * sign;
168
169 overflow:
170   /* Return an overflow error.  */
171   errno = ERANGE;
172   return HUGE_VAL * sign;
173
174 underflow:
175   /* Return an underflow error.  */
176   if (endptr != NULL)
177     *endptr = (char *) nptr;
178   errno = ERANGE;
179   return 0.0;
180
181 noconv:
182   /* There was no number.  */
183   if (endptr != NULL)
184     *endptr = (char *) nptr;
185   return 0.0;
186 }