Mercurial > gnulib
annotate lib/strtod.c @ 2807:807294ed0f4f
back out Copyright date changes for files with no changes year
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Mon, 07 Aug 2000 15:48:18 +0000 |
parents | 5994c6f939c5 |
children | ce37d22a271f |
rev | line source |
---|---|
2807
807294ed0f4f
back out Copyright date changes for files with no changes year
Jim Meyering <jim@meyering.net>
parents:
2718
diff
changeset
|
1 /* Copyright (C) 1991, 1992, 1997, 1999 Free Software Foundation, Inc. |
9 | 2 |
311 | 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. | |
9 | 7 |
311 | 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 | |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
373
diff
changeset
|
14 along with this program; if not, write to the Free Software Foundation, |
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
373
diff
changeset
|
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
309 | 16 |
728 | 17 #if HAVE_CONFIG_H |
18 # include <config.h> | |
311 | 19 #endif |
20 | |
21 #include <errno.h> | |
373 | 22 #ifndef errno |
23 extern int errno; | |
24 #endif | |
25 | |
311 | 26 #include <ctype.h> |
1042 | 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 | |
311 | 38 #include <math.h> |
9 | 39 |
728 | 40 #if HAVE_FLOAT_H |
41 # include <float.h> | |
311 | 42 #else |
728 | 43 # define DBL_MAX 1.7976931348623159e+308 |
44 # define DBL_MIN 2.2250738585072010e-308 | |
311 | 45 #endif |
309 | 46 |
311 | 47 #if STDC_HEADERS |
728 | 48 # include <stdlib.h> |
49 # include <string.h> | |
311 | 50 #else |
728 | 51 # define NULL 0 |
52 # ifndef HUGE_VAL | |
53 # define HUGE_VAL HUGE | |
54 # endif | |
311 | 55 #endif |
9 | 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 | |
1691 | 60 strtod (const char *nptr, char **endptr) |
9 | 61 { |
311 | 62 register const char *s; |
9 | 63 short int sign; |
64 | |
65 /* The number so far. */ | |
66 double num; | |
67 | |
311 | 68 int got_dot; /* Found a decimal point. */ |
69 int got_digit; /* Seen any digits. */ | |
9 | 70 |
71 /* The exponent of the number. */ | |
72 long int exponent; | |
73 | |
74 if (nptr == NULL) | |
75 { | |
76 errno = EINVAL; | |
77 goto noconv; | |
78 } | |
79 | |
80 s = nptr; | |
81 | |
82 /* Eat whitespace. */ | |
1042 | 83 while (ISSPACE (*s)) |
9 | 84 ++s; |
85 | |
86 /* Get the sign. */ | |
87 sign = *s == '-' ? -1 : 1; | |
88 if (*s == '-' || *s == '+') | |
89 ++s; | |
90 | |
91 num = 0.0; | |
92 got_dot = 0; | |
93 got_digit = 0; | |
94 exponent = 0; | |
95 for (;; ++s) | |
96 { | |
1042 | 97 if (ISDIGIT (*s)) |
9 | 98 { |
99 got_digit = 1; | |
100 | |
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. | |
107 | |
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'); | |
113 | |
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; | |
118 } | |
311 | 119 else if (!got_dot && *s == '.') |
9 | 120 /* Record that we have found the decimal point. */ |
121 got_dot = 1; | |
122 else | |
123 /* Any other character terminates the number. */ | |
124 break; | |
125 } | |
126 | |
127 if (!got_digit) | |
128 goto noconv; | |
129 | |
1042 | 130 if (TOLOWER (*s) == 'e') |
9 | 131 { |
132 /* Get the exponent specified after the `e' or `E'. */ | |
133 int save = errno; | |
134 char *end; | |
135 long int exp; | |
136 | |
137 errno = 0; | |
138 ++s; | |
311 | 139 exp = strtol (s, &end, 10); |
9 | 140 if (errno == ERANGE) |
141 { | |
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; | |
151 } | |
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; | |
159 } | |
160 | |
161 if (endptr != NULL) | |
162 *endptr = (char *) s; | |
163 | |
164 if (num == 0.0) | |
165 return 0.0; | |
166 | |
167 /* Multiply NUM by 10 to the EXPONENT power, | |
168 checking for overflow and underflow. */ | |
169 | |
170 if (exponent < 0) | |
171 { | |
311 | 172 if (num < DBL_MIN * pow (10.0, (double) -exponent)) |
9 | 173 goto underflow; |
174 } | |
175 else if (exponent > 0) | |
176 { | |
311 | 177 if (num > DBL_MAX * pow (10.0, (double) -exponent)) |
9 | 178 goto overflow; |
179 } | |
180 | |
311 | 181 num *= pow (10.0, (double) exponent); |
9 | 182 |
183 return num * sign; | |
184 | |
311 | 185 overflow: |
9 | 186 /* Return an overflow error. */ |
187 errno = ERANGE; | |
188 return HUGE_VAL * sign; | |
189 | |
311 | 190 underflow: |
9 | 191 /* Return an underflow error. */ |
192 if (endptr != NULL) | |
193 *endptr = (char *) nptr; | |
194 errno = ERANGE; | |
195 return 0.0; | |
196 | |
311 | 197 noconv: |
9 | 198 /* There was no number. */ |
199 if (endptr != NULL) | |
200 *endptr = (char *) nptr; | |
201 return 0.0; | |
202 } |