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