*** empty log message ***
[coreutils.git] / lib / strtod.c
blob10edb5cfe5a67c6c99a0d4b3aaa1a405072ba3e7
1 /* Copyright (C) 1991, 1992, 1997, 1999 Free Software Foundation, Inc.
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.
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.
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. */
17 #if HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <errno.h>
22 #ifndef errno
23 extern int errno;
24 #endif
26 #include <ctype.h>
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
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))
38 #include <math.h>
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
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
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 (const char *nptr, char **endptr)
62 register const char *s;
63 short int sign;
65 /* The number so far. */
66 double num;
68 int got_dot; /* Found a decimal point. */
69 int got_digit; /* Seen any digits. */
71 /* The exponent of the number. */
72 long int exponent;
74 if (nptr == NULL)
76 errno = EINVAL;
77 goto noconv;
80 s = nptr;
82 /* Eat whitespace. */
83 while (ISSPACE (*s))
84 ++s;
86 /* Get the sign. */
87 sign = *s == '-' ? -1 : 1;
88 if (*s == '-' || *s == '+')
89 ++s;
91 num = 0.0;
92 got_dot = 0;
93 got_digit = 0;
94 exponent = 0;
95 for (;; ++s)
97 if (ISDIGIT (*s))
99 got_digit = 1;
101 /* Make sure that multiplication by 10 will not overflow. */
102 if (num > DBL_MAX * 0.1)
103 /* The value of the digit doesn't matter, since we have already
104 gotten as many digits as can be represented in a `double'.
105 This doesn't necessarily mean the result will overflow.
106 The exponent may reduce it to within range.
108 We just need to record that there was another
109 digit so that we can multiply by 10 later. */
110 ++exponent;
111 else
112 num = (num * 10.0) + (*s - '0');
114 /* Keep track of the number of digits after the decimal point.
115 If we just divided by 10 here, we would lose precision. */
116 if (got_dot)
117 --exponent;
119 else if (!got_dot && *s == '.')
120 /* Record that we have found the decimal point. */
121 got_dot = 1;
122 else
123 /* Any other character terminates the number. */
124 break;
127 if (!got_digit)
128 goto noconv;
130 if (TOLOWER (*s) == 'e')
132 /* Get the exponent specified after the `e' or `E'. */
133 int save = errno;
134 char *end;
135 long int exp;
137 errno = 0;
138 ++s;
139 exp = strtol (s, &end, 10);
140 if (errno == ERANGE)
142 /* The exponent overflowed a `long int'. It is probably a safe
143 assumption that an exponent that cannot be represented by
144 a `long int' exceeds the limits of a `double'. */
145 if (endptr != NULL)
146 *endptr = end;
147 if (exp < 0)
148 goto underflow;
149 else
150 goto overflow;
152 else if (end == s)
153 /* There was no exponent. Reset END to point to
154 the 'e' or 'E', so *ENDPTR will be set there. */
155 end = (char *) s - 1;
156 errno = save;
157 s = end;
158 exponent += exp;
161 if (endptr != NULL)
162 *endptr = (char *) s;
164 if (num == 0.0)
165 return 0.0;
167 /* Multiply NUM by 10 to the EXPONENT power,
168 checking for overflow and underflow. */
170 if (exponent < 0)
172 if (num < DBL_MIN * pow (10.0, (double) -exponent))
173 goto underflow;
175 else if (exponent > 0)
177 if (num > DBL_MAX * pow (10.0, (double) -exponent))
178 goto overflow;
181 num *= pow (10.0, (double) exponent);
183 return num * sign;
185 overflow:
186 /* Return an overflow error. */
187 errno = ERANGE;
188 return HUGE_VAL * sign;
190 underflow:
191 /* Return an underflow error. */
192 if (endptr != NULL)
193 *endptr = (char *) nptr;
194 errno = ERANGE;
195 return 0.0;
197 noconv:
198 /* There was no number. */
199 if (endptr != NULL)
200 *endptr = (char *) nptr;
201 return 0.0;