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