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