1 /* Copyright (C) 1991, 1992, 1997, 1999, 2003, 2006, 2008 Free
2 Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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. */
33 strtod (const char *nptr, char **endptr)
35 const unsigned char *s;
36 bool negative = false;
38 /* The number so far. */
41 bool got_dot; /* Found a decimal point. */
42 bool got_digit; /* Seen any digits. */
43 bool hex = false; /* Look for hex float exponent. */
45 /* The exponent of the number. */
62 if (*s == '-' || *s == '+')
70 /* Check for hex float. */
71 if (*s == '0' && c_tolower (s[1]) == 'x'
72 && (c_isxdigit (s[2]) || ('.' == s[2] && c_isxdigit (s[3]))))
82 /* Make sure that multiplication by 16 will not overflow. */
83 if (num > DBL_MAX / 16)
84 /* The value of the digit doesn't matter, since we have already
85 gotten as many digits as can be represented in a `double'.
86 This doesn't necessarily mean the result will overflow.
87 The exponent may reduce it to within range.
89 We just need to record that there was another
90 digit so that we can multiply by 16 later. */
94 + (c_tolower (*s) - (c_isdigit (*s) ? '0' : 'a' - 10)));
96 /* Keep track of the number of digits after the decimal point.
97 If we just divided by 16 here, we would lose precision. */
101 else if (!got_dot && *s == '.')
102 /* Record that we have found the decimal point. */
105 /* Any other character terminates the number. */
110 /* Not a hex float. */
119 /* Make sure that multiplication by 10 will not overflow. */
120 if (num > DBL_MAX * 0.1)
121 /* The value of the digit doesn't matter, since we have already
122 gotten as many digits as can be represented in a `double'.
123 This doesn't necessarily mean the result will overflow.
124 The exponent may reduce it to within range.
126 We just need to record that there was another
127 digit so that we can multiply by 10 later. */
130 num = (num * 10.0) + (*s - '0');
132 /* Keep track of the number of digits after the decimal point.
133 If we just divided by 10 here, we would lose precision. */
137 else if (!got_dot && *s == '.')
138 /* Record that we have found the decimal point. */
141 /* Any other character terminates the number. */
148 /* Check for infinities and NaNs. */
149 if (c_tolower (*s) == 'i'
150 && c_tolower (s[1]) == 'n'
151 && c_tolower (s[2]) == 'f')
155 if (c_tolower (*s) == 'i'
156 && c_tolower (s[1]) == 'n'
157 && c_tolower (s[2]) == 'i'
158 && c_tolower (s[3]) == 't'
159 && c_tolower (s[4]) == 'y')
164 else if (c_tolower (*s) == 'n'
165 && c_tolower (s[1]) == 'a'
166 && c_tolower (s[2]) == 'n')
170 /* Since nan(<n-char-sequence>) is implementation-defined,
171 we define it by ignoring <n-char-sequence>. A nicer
172 implementation would populate the bits of the NaN
173 according to interpreting n-char-sequence as a
174 hexadecimal number, but the result is still a NaN. */
177 const unsigned char *p = s + 1;
178 while (c_isalnum (*p))
189 if (c_tolower (*s) == (hex ? 'p' : 'e') && !isspace (s[1]))
191 /* Get the exponent specified after the `e' or `E'. */
198 exp = strtol (s, &end, 10);
199 if (errno == ERANGE && num)
201 /* The exponent overflowed a `long int'. It is probably a safe
202 assumption that an exponent that cannot be represented by
203 a `long int' exceeds the limits of a `double'. */
211 else if (end == (char *) s)
212 /* There was no exponent. Reset END to point to
213 the 'e' or 'E', so *ENDPTR will be set there. */
214 end = (char *) s - 1;
225 /* ldexp takes care of range errors. */
226 num = ldexp (num, exponent);
230 /* Multiply NUM by 10 to the EXPONENT power,
231 checking for overflow and underflow. */
235 if (num < DBL_MIN * pow (10.0, (double) -exponent))
238 else if (exponent > 0)
240 if (num > DBL_MAX * pow (10.0, (double) -exponent))
244 num *= pow (10.0, (double) exponent);
248 *endptr = (char *) s;
249 return negative ? -num : num;
252 /* Return an overflow error. */
254 *endptr = (char *) s;
256 return negative ? -HUGE_VAL : HUGE_VAL;
259 /* Return an underflow error. */
261 *endptr = (char *) s;
263 return negative ? -0.0 : 0.0;
266 /* There was no number. */
268 *endptr = (char *) nptr;