1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2011 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 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
194 ICC 10.0 has a bug when optimizing the expression -zero.
195 The expression -DBL_MIN * DBL_MIN does not work when cross-compiling
196 to PowerPC on MacOS X 10.5. */
197 #if defined __hpux || defined __sgi || defined __ICC
199 compute_minus_zero (void)
201 return -DBL_MIN * DBL_MIN;
203 # define minus_zero compute_minus_zero ()
205 double minus_zero = -0.0;
208 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
209 character after the last one used in the number is put in *ENDPTR. */
211 strtod (const char *nptr, char **endptr)
213 bool negative = false;
215 /* The number so far. */
218 const char *s = nptr;
223 /* Eat whitespace. */
224 while (locale_isspace (*s))
228 negative = *s == '-';
229 if (*s == '-' || *s == '+')
233 num = underlying_strtod (s, &endbuf);
236 if (c_isdigit (s[*s == '.']))
238 /* If a hex float was converted incorrectly, do it ourselves.
239 If the string starts with "0x" but does not contain digits,
240 consume the "0" ourselves. If a hex float is followed by a
241 'p' but no exponent, then adjust the end pointer. */
242 if (*s == '0' && c_tolower (s[1]) == 'x')
244 if (! c_isxdigit (s[2 + (s[2] == '.')]))
246 else if (end <= s + 2)
248 num = parse_number (s + 2, 16, 2, 4, 'p', &endbuf);
253 const char *p = s + 2;
254 while (p < end && c_tolower (*p) != 'p')
256 if (p < end && ! c_isdigit (p[1 + (p[1] == '-' || p[1] == '+')]))
262 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
263 underlying strtod on a copy of the original string
264 truncated to avoid the bug. */
265 const char *e = s + 1;
266 while (e < end && c_tolower (*e) != 'e')
268 if (e < end && ! c_isdigit (e[1 + (e[1] == '-' || e[1] == '+')]))
270 char *dup = strdup (s);
274 /* Not really our day, is it. Rounding errors are
275 better than outright failure. */
276 num = parse_number (s, 10, 10, 1, 'e', &endbuf);
281 num = underlying_strtod (dup, &endbuf);
293 /* Check for infinities and NaNs. */
294 else if (c_tolower (*s) == 'i'
295 && c_tolower (s[1]) == 'n'
296 && c_tolower (s[2]) == 'f')
299 if (c_tolower (*s) == 'i'
300 && c_tolower (s[1]) == 'n'
301 && c_tolower (s[2]) == 'i'
302 && c_tolower (s[3]) == 't'
303 && c_tolower (s[4]) == 'y')
308 else if (c_tolower (*s) == 'n'
309 && c_tolower (s[1]) == 'a'
310 && c_tolower (s[2]) == 'n')
315 const char *p = s + 1;
316 while (c_isalnum (*p))
322 /* If the underlying implementation misparsed the NaN, assume
323 its result is incorrect, and return a NaN. Normally it's
324 better to use the underlying implementation's result, since a
325 nice implementation populates the bits of the NaN according
326 to interpreting n-char-sequence as a hexadecimal number. */
333 /* No conversion could be performed. */
339 *endptr = (char *) s;
340 /* Special case -0.0, since at least ICC miscompiles negation. We
341 can't use copysign(), as that drags in -lm on some platforms. */
342 if (!num && negative)
344 return negative ? -num : num;
347 /* The "underlying" strtod implementation. This must be defined
348 after strtod because it #undefs strtod. */
350 underlying_strtod (const char *nptr, char **endptr)
352 if (HAVE_RAW_DECL_STRTOD)
354 /* Prefer the native strtod if available. Usually it should
355 work and it should give more-accurate results than our
358 return strtod (nptr, endptr);
362 /* Approximate strtod well enough for this module. There's no
363 need to handle anything but finite unsigned decimal
364 numbers with nonnull ENDPTR. */
365 return parse_number (nptr, 10, 10, 1, 'e', endptr);