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