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