GNU shell utilities
[gnulib.git] / lib / strtod.c
1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB.  If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.  */
18
19 #include <errno.h>
20 #include <ctype.h>
21 #include <math.h>
22
23 #if STDC_HEADERS
24 #include <float.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #else
28 #define NULL 0
29 #define DBL_MAX 1.7976931348623159e+308
30 #define DBL_MIN 2.2250738585072010e-308
31 extern int errno;
32 #endif
33 #ifndef HUGE_VAL
34 #define HUGE_VAL HUGE
35 #endif
36
37 /* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
38    character after the last one used in the number is put in *ENDPTR.  */
39 double
40 strtod (nptr, endptr)
41      const char *nptr;
42      char **endptr;
43 {
44   register const char *s;
45   short int sign;
46
47   /* The number so far.  */
48   double num;
49
50   int got_dot;                  /* Found a decimal point.  */
51   int got_digit;                /* Seen any digits.  */
52
53   /* The exponent of the number.  */
54   long int exponent;
55
56   if (nptr == NULL)
57     {
58       errno = EINVAL;
59       goto noconv;
60     }
61
62   s = nptr;
63
64   /* Eat whitespace.  */
65   while (isspace (*s))
66     ++s;
67
68   /* Get the sign.  */
69   sign = *s == '-' ? -1 : 1;
70   if (*s == '-' || *s == '+')
71     ++s;
72
73   num = 0.0;
74   got_dot = 0;
75   got_digit = 0;
76   exponent = 0;
77   for (;; ++s)
78     {
79       if (isdigit (*s))
80         {
81           got_digit = 1;
82
83           /* Make sure that multiplication by 10 will not overflow.  */
84           if (num > DBL_MAX * 0.1)
85             /* The value of the digit doesn't matter, since we have already
86                gotten as many digits as can be represented in a `double'.
87                This doesn't necessarily mean the result will overflow.
88                The exponent may reduce it to within range.
89
90                We just need to record that there was another
91                digit so that we can multiply by 10 later.  */
92             ++exponent;
93           else
94             num = (num * 10.0) + (*s - '0');
95
96           /* Keep track of the number of digits after the decimal point.
97              If we just divided by 10 here, we would lose precision.  */
98           if (got_dot)
99             --exponent;
100         }
101       else if (!got_dot && *s == '.')
102         /* Record that we have found the decimal point.  */
103         got_dot = 1;
104       else
105         /* Any other character terminates the number.  */
106         break;
107     }
108
109   if (!got_digit)
110     goto noconv;
111
112   if (tolower (*s) == 'e')
113     {
114       /* Get the exponent specified after the `e' or `E'.  */
115       int save = errno;
116       char *end;
117       long int exp;
118
119       errno = 0;
120       ++s;
121       exp = strtol (s, &end, 10);
122       if (errno == ERANGE)
123         {
124           /* The exponent overflowed a `long int'.  It is probably a safe
125              assumption that an exponent that cannot be represented by
126              a `long int' exceeds the limits of a `double'.  */
127           if (endptr != NULL)
128             *endptr = end;
129           if (exp < 0)
130             goto underflow;
131           else
132             goto overflow;
133         }
134       else if (end == s)
135         /* There was no exponent.  Reset END to point to
136            the 'e' or 'E', so *ENDPTR will be set there.  */
137         end = (char *) s - 1;
138       errno = save;
139       s = end;
140       exponent += exp;
141     }
142
143   if (endptr != NULL)
144     *endptr = (char *) s;
145
146   if (num == 0.0)
147     return 0.0;
148
149   /* Multiply NUM by 10 to the EXPONENT power,
150      checking for overflow and underflow.  */
151
152   if (exponent < 0)
153     {
154       if (num < DBL_MIN * pow (10.0, (double) -exponent))
155         goto underflow;
156     }
157   else if (exponent > 0)
158     {
159       if (num > DBL_MAX * pow (10.0, (double) -exponent))
160         goto overflow;
161     }
162
163   num *= pow (10.0, (double) exponent);
164
165   return num * sign;
166
167 overflow:
168   /* Return an overflow error.  */
169   errno = ERANGE;
170   return HUGE_VAL * sign;
171
172 underflow:
173   /* Return an underflow error.  */
174   if (endptr != NULL)
175     *endptr = (char *) nptr;
176   errno = ERANGE;
177   return 0.0;
178
179 noconv:
180   /* There was no number.  */
181   if (endptr != NULL)
182     *endptr = (char *) nptr;
183   return 0.0;
184 }