1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2010 Free Software
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/>. */
31 #ifndef HAVE_LDEXP_IN_LIBC
32 #define HAVE_LDEXP_IN_LIBC 0
34 #ifndef HAVE_RAW_DECL_STRTOD
35 #define HAVE_RAW_DECL_STRTOD 0
38 /* Return true if C is a space in the current locale, avoiding
39 problems with signed char and isspace. */
41 locale_isspace (char c)
44 return isspace (uc) != 0;
47 #if !HAVE_LDEXP_IN_LIBC
48 #define ldexp dummy_ldexp
49 /* A dummy definition that will never be invoked. */
50 static double ldexp (double x _GL_UNUSED, int exponent _GL_UNUSED)
57 /* Return X * BASE**EXPONENT. Return an extreme value and set errno
58 to ERANGE if underflow or overflow occurs. */
60 scale_radix_exp (double x, int radix, long int exponent)
62 /* If RADIX == 10, this code is neither precise nor fast; it is
63 merely a straightforward and relatively portable approximation.
64 If N == 2, this code is precise on a radix-2 implementation,
65 albeit perhaps not fast if ldexp is not in libc. */
67 long int e = exponent;
69 if (HAVE_LDEXP_IN_LIBC && radix == 2)
70 return ldexp (x, e < INT_MIN ? INT_MIN : INT_MAX < e ? INT_MAX : e);
93 if (r < -DBL_MAX / radix)
98 else if (DBL_MAX / radix < r)
113 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR)
114 except there are no leading spaces or signs or "0x", and ENDPTR is
115 nonnull. The number uses a base BASE (either 10 or 16) fraction, a
116 radix RADIX (either 10 or 2) exponent, and exponent character
117 EXPCHAR. To convert from a number of digits to a radix exponent,
118 multiply by RADIX_MULTIPLIER (either 1 or 4). */
120 parse_number (const char *nptr,
121 int base, int radix, int radix_multiplier, char expchar,
124 const char *s = nptr;
125 bool got_dot = false;
126 long int exponent = 0;
134 else if (base == 16 && c_isxdigit (*s))
135 digit = c_tolower (*s) - ('a' - 10);
136 else if (! got_dot && *s == '.')
138 /* Record that we have found the decimal point. */
143 /* Any other character terminates the number. */
146 /* Make sure that multiplication by base will not overflow. */
147 if (num <= DBL_MAX / base)
148 num = num * base + digit;
151 /* The value of the digit doesn't matter, since we have already
152 gotten as many digits as can be represented in a `double'.
153 This doesn't necessarily mean the result will overflow.
154 The exponent may reduce it to within range.
156 We just need to record that there was another
157 digit so that we can multiply by 10 later. */
158 exponent += radix_multiplier;
161 /* Keep track of the number of digits after the decimal point.
162 If we just divided by base here, we might lose precision. */
164 exponent -= radix_multiplier;
167 if (c_tolower (*s) == expchar && ! locale_isspace (s[1]))
169 /* Add any given exponent to the implicit one. */
172 long int value = strtol (s + 1, &end, 10);
177 /* Skip past the exponent, and add in the implicit exponent,
178 resulting in an extreme value on overflow. */
182 ? (value < LONG_MIN - exponent ? LONG_MIN : exponent + value)
183 : (LONG_MAX - exponent < value ? LONG_MAX : exponent + value));
187 *endptr = (char *) s;
188 return scale_radix_exp (num, radix, exponent);
191 static double underlying_strtod (const char *, char **);
193 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
194 character after the last one used in the number is put in *ENDPTR. */
196 strtod (const char *nptr, char **endptr)
198 bool negative = false;
200 /* The number so far. */
203 const char *s = nptr;
208 /* Eat whitespace. */
209 while (locale_isspace (*s))
213 negative = *s == '-';
214 if (*s == '-' || *s == '+')
218 num = underlying_strtod (s, &endbuf);
221 if (c_isdigit (s[*s == '.']))
223 /* If a hex float was converted incorrectly, do it ourselves.
224 If the string starts with "0x" but does not contain digits,
225 consume the "0" ourselves. If a hex float is followed by a
226 'p' but no exponent, then adjust the end pointer. */
227 if (*s == '0' && c_tolower (s[1]) == 'x')
229 if (! c_isxdigit (s[2 + (s[2] == '.')]))
231 else if (end <= s + 2)
233 num = parse_number (s + 2, 16, 2, 4, 'p', &endbuf);
238 const char *p = s + 2;
239 while (p < end && c_tolower (*p) != 'p')
241 if (p < end && ! c_isdigit (p[1 + (p[1] == '-' || p[1] == '+')]))
247 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
248 underlying strtod on a copy of the original string
249 truncated to avoid the bug. */
250 const char *e = s + 1;
251 while (e < end && c_tolower (*e) != 'e')
253 if (e < end && ! c_isdigit (e[1 + (e[1] == '-' || e[1] == '+')]))
255 char *dup = strdup (s);
259 /* Not really our day, is it. Rounding errors are
260 better than outright failure. */
261 num = parse_number (s, 10, 10, 1, 'e', &endbuf);
266 num = underlying_strtod (dup, &endbuf);
278 /* Check for infinities and NaNs. */
279 else if (c_tolower (*s) == 'i'
280 && c_tolower (s[1]) == 'n'
281 && c_tolower (s[2]) == 'f')
284 if (c_tolower (*s) == 'i'
285 && c_tolower (s[1]) == 'n'
286 && c_tolower (s[2]) == 'i'
287 && c_tolower (s[3]) == 't'
288 && c_tolower (s[4]) == 'y')
292 else if (c_tolower (*s) == 'n'
293 && c_tolower (s[1]) == 'a'
294 && c_tolower (s[2]) == 'n')
299 const char *p = s + 1;
300 while (c_isalnum (*p))
306 /* If the underlying implementation misparsed the NaN, assume
307 its result is incorrect, and return a NaN. Normally it's
308 better to use the underlying implementation's result, since a
309 nice implementation populates the bits of the NaN according
310 to interpreting n-char-sequence as a hexadecimal number. */
316 /* No conversion could be performed. */
322 *endptr = (char *) s;
323 return negative ? -num : num;
326 /* The "underlying" strtod implementation. This must be defined
327 after strtod because it #undefs strtod. */
329 underlying_strtod (const char *nptr, char **endptr)
331 if (HAVE_RAW_DECL_STRTOD)
333 /* Prefer the native strtod if available. Usually it should
334 work and it should give more-accurate results than our
337 return strtod (nptr, endptr);
341 /* Approximate strtod well enough for this module. There's no
342 need to handle anything but finite unsigned decimal
343 numbers with nonnull ENDPTR. */
344 return parse_number (nptr, 10, 10, 1, 'e', endptr);