* strtod.c (strtod): cast the argument of tolower to unsigned char.
[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 #include <math.h>
26
27 #include <float.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 /* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
32    character after the last one used in the number is put in *ENDPTR.  */
33 double
34 strtod (const char *nptr, char **endptr)
35 {
36   register const char *s;
37   short int sign;
38
39   /* The number so far.  */
40   double num;
41
42   int got_dot;                  /* Found a decimal point.  */
43   int got_digit;                /* Seen any digits.  */
44
45   /* The exponent of the number.  */
46   long int exponent;
47
48   if (nptr == NULL)
49     {
50       errno = EINVAL;
51       goto noconv;
52     }
53
54   s = nptr;
55
56   /* Eat whitespace.  */
57   while (isspace ((unsigned char) *s))
58     ++s;
59
60   /* Get the sign.  */
61   sign = *s == '-' ? -1 : 1;
62   if (*s == '-' || *s == '+')
63     ++s;
64
65   num = 0.0;
66   got_dot = 0;
67   got_digit = 0;
68   exponent = 0;
69   for (;; ++s)
70     {
71       if ('0' <= *s && *s <= '9')
72         {
73           got_digit = 1;
74
75           /* Make sure that multiplication by 10 will not overflow.  */
76           if (num > DBL_MAX * 0.1)
77             /* The value of the digit doesn't matter, since we have already
78                gotten as many digits as can be represented in a `double'.
79                This doesn't necessarily mean the result will overflow.
80                The exponent may reduce it to within range.
81
82                We just need to record that there was another
83                digit so that we can multiply by 10 later.  */
84             ++exponent;
85           else
86             num = (num * 10.0) + (*s - '0');
87
88           /* Keep track of the number of digits after the decimal point.
89              If we just divided by 10 here, we would lose precision.  */
90           if (got_dot)
91             --exponent;
92         }
93       else if (!got_dot && *s == '.')
94         /* Record that we have found the decimal point.  */
95         got_dot = 1;
96       else
97         /* Any other character terminates the number.  */
98         break;
99     }
100
101   if (!got_digit)
102     goto noconv;
103
104   if (tolower ((unsigned char) *s) == 'e')
105     {
106       /* Get the exponent specified after the `e' or `E'.  */
107       int save = errno;
108       char *end;
109       long int exp;
110
111       errno = 0;
112       ++s;
113       exp = strtol (s, &end, 10);
114       if (errno == ERANGE)
115         {
116           /* The exponent overflowed a `long int'.  It is probably a safe
117              assumption that an exponent that cannot be represented by
118              a `long int' exceeds the limits of a `double'.  */
119           if (endptr != NULL)
120             *endptr = end;
121           if (exp < 0)
122             goto underflow;
123           else
124             goto overflow;
125         }
126       else if (end == s)
127         /* There was no exponent.  Reset END to point to
128            the 'e' or 'E', so *ENDPTR will be set there.  */
129         end = (char *) s - 1;
130       errno = save;
131       s = end;
132       exponent += exp;
133     }
134
135   if (endptr != NULL)
136     *endptr = (char *) s;
137
138   if (num == 0.0)
139     return 0.0;
140
141   /* Multiply NUM by 10 to the EXPONENT power,
142      checking for overflow and underflow.  */
143
144   if (exponent < 0)
145     {
146       if (num < DBL_MIN * pow (10.0, (double) -exponent))
147         goto underflow;
148     }
149   else if (exponent > 0)
150     {
151       if (num > DBL_MAX * pow (10.0, (double) -exponent))
152         goto overflow;
153     }
154
155   num *= pow (10.0, (double) exponent);
156
157   return num * sign;
158
159 overflow:
160   /* Return an overflow error.  */
161   errno = ERANGE;
162   return HUGE_VAL * sign;
163
164 underflow:
165   /* Return an underflow error.  */
166   if (endptr != NULL)
167     *endptr = (char *) nptr;
168   errno = ERANGE;
169   return 0.0;
170
171 noconv:
172   /* There was no number.  */
173   if (endptr != NULL)
174     *endptr = (char *) nptr;
175   return 0.0;
176 }