1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2018 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 <https://www.gnu.org/licenses/>. */
31 #ifndef HAVE_LDEXP_IN_LIBC
32 #define HAVE_LDEXP_IN_LIBC 0
35 /* Return true if C is a space in the current locale, avoiding
36 problems with signed char and isspace. */
38 locale_isspace (char c
)
41 return isspace (uc
) != 0;
44 #if !HAVE_LDEXP_IN_LIBC
45 #define ldexp dummy_ldexp
46 /* A dummy definition that will never be invoked. */
47 static double ldexp (double x _GL_UNUSED
, int exponent _GL_UNUSED
)
54 /* Return X * BASE**EXPONENT. Return an extreme value and set errno
55 to ERANGE if underflow or overflow occurs. */
57 scale_radix_exp (double x
, int radix
, long int exponent
)
59 /* If RADIX == 10, this code is neither precise nor fast; it is
60 merely a straightforward and relatively portable approximation.
61 If N == 2, this code is precise on a radix-2 implementation,
62 albeit perhaps not fast if ldexp is not in libc. */
64 long int e
= exponent
;
66 if (HAVE_LDEXP_IN_LIBC
&& radix
== 2)
67 return ldexp (x
, e
< INT_MIN
? INT_MIN
: INT_MAX
< e
? INT_MAX
: e
);
90 if (r
< -DBL_MAX
/ radix
)
95 else if (DBL_MAX
/ radix
< r
)
110 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR)
111 except there are no leading spaces or signs or "0x", and ENDPTR is
112 nonnull. The number uses a base BASE (either 10 or 16) fraction, a
113 radix RADIX (either 10 or 2) exponent, and exponent character
114 EXPCHAR. To convert from a number of digits to a radix exponent,
115 multiply by RADIX_MULTIPLIER (either 1 or 4). */
117 parse_number (const char *nptr
,
118 int base
, int radix
, int radix_multiplier
, char expchar
,
121 const char *s
= nptr
;
122 bool got_dot
= false;
123 long int exponent
= 0;
131 else if (base
== 16 && c_isxdigit (*s
))
132 digit
= c_tolower (*s
) - ('a' - 10);
133 else if (! got_dot
&& *s
== '.')
135 /* Record that we have found the decimal point. */
140 /* Any other character terminates the number. */
143 /* Make sure that multiplication by base will not overflow. */
144 if (num
<= DBL_MAX
/ base
)
145 num
= num
* base
+ digit
;
148 /* The value of the digit doesn't matter, since we have already
149 gotten as many digits as can be represented in a 'double'.
150 This doesn't necessarily mean the result will overflow.
151 The exponent may reduce it to within range.
153 We just need to record that there was another
154 digit so that we can multiply by 10 later. */
155 exponent
+= radix_multiplier
;
158 /* Keep track of the number of digits after the decimal point.
159 If we just divided by base here, we might lose precision. */
161 exponent
-= radix_multiplier
;
164 if (c_tolower (*s
) == expchar
&& ! locale_isspace (s
[1]))
166 /* Add any given exponent to the implicit one. */
169 long int value
= strtol (s
+ 1, &end
, 10);
174 /* Skip past the exponent, and add in the implicit exponent,
175 resulting in an extreme value on overflow. */
179 ? (value
< LONG_MIN
- exponent
? LONG_MIN
: exponent
+ value
)
180 : (LONG_MAX
- exponent
< value
? LONG_MAX
: exponent
+ value
));
184 *endptr
= (char *) s
;
185 return scale_radix_exp (num
, radix
, exponent
);
188 static double underlying_strtod (const char *, char **);
190 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
191 ICC 10.0 has a bug when optimizing the expression -zero.
192 The expression -DBL_MIN * DBL_MIN does not work when cross-compiling
193 to PowerPC on Mac OS X 10.5. */
194 #if defined __hpux || defined __sgi || defined __ICC
196 compute_minus_zero (void)
198 return -DBL_MIN
* DBL_MIN
;
200 # define minus_zero compute_minus_zero ()
202 double minus_zero
= -0.0;
205 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
206 character after the last one used in the number is put in *ENDPTR. */
208 strtod (const char *nptr
, char **endptr
)
210 bool negative
= false;
212 /* The number so far. */
215 const char *s
= nptr
;
218 int saved_errno
= errno
;
220 /* Eat whitespace. */
221 while (locale_isspace (*s
))
225 negative
= *s
== '-';
226 if (*s
== '-' || *s
== '+')
229 num
= underlying_strtod (s
, &endbuf
);
232 if (c_isdigit (s
[*s
== '.']))
234 /* If a hex float was converted incorrectly, do it ourselves.
235 If the string starts with "0x" but does not contain digits,
236 consume the "0" ourselves. If a hex float is followed by a
237 'p' but no exponent, then adjust the end pointer. */
238 if (*s
== '0' && c_tolower (s
[1]) == 'x')
240 if (! c_isxdigit (s
[2 + (s
[2] == '.')]))
244 /* strtod() on z/OS returns ERANGE for "0x". */
247 else if (end
<= s
+ 2)
249 num
= parse_number (s
+ 2, 16, 2, 4, 'p', &endbuf
);
254 const char *p
= s
+ 2;
255 while (p
< end
&& c_tolower (*p
) != 'p')
257 if (p
< end
&& ! c_isdigit (p
[1 + (p
[1] == '-' || p
[1] == '+')]))
263 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
264 underlying strtod on a copy of the original string
265 truncated to avoid the bug. */
266 const char *e
= s
+ 1;
267 while (e
< end
&& c_tolower (*e
) != 'e')
269 if (e
< end
&& ! c_isdigit (e
[1 + (e
[1] == '-' || e
[1] == '+')]))
271 char *dup
= strdup (s
);
275 /* Not really our day, is it. Rounding errors are
276 better than outright failure. */
277 num
= parse_number (s
, 10, 10, 1, 'e', &endbuf
);
282 num
= underlying_strtod (dup
, &endbuf
);
294 /* Check for infinities and NaNs. */
295 else if (c_tolower (*s
) == 'i'
296 && c_tolower (s
[1]) == 'n'
297 && c_tolower (s
[2]) == 'f')
300 if (c_tolower (*s
) == 'i'
301 && c_tolower (s
[1]) == 'n'
302 && c_tolower (s
[2]) == 'i'
303 && c_tolower (s
[3]) == 't'
304 && c_tolower (s
[4]) == 'y')
309 else if (c_tolower (*s
) == 'n'
310 && c_tolower (s
[1]) == 'a'
311 && c_tolower (s
[2]) == 'n')
316 const char *p
= s
+ 1;
317 while (c_isalnum (*p
))
323 /* If the underlying implementation misparsed the NaN, assume
324 its result is incorrect, and return a NaN. Normally it's
325 better to use the underlying implementation's result, since a
326 nice implementation populates the bits of the NaN according
327 to interpreting n-char-sequence as a hexadecimal number. */
328 if (s
!= end
|| num
== num
)
334 /* No conversion could be performed. */
340 *endptr
= (char *) s
;
341 /* Special case -0.0, since at least ICC miscompiles negation. We
342 can't use copysign(), as that drags in -lm on some platforms. */
343 if (!num
&& negative
)
345 return negative
? -num
: num
;
348 /* The underlying strtod implementation. This must be defined
349 after strtod because it #undefs strtod. */
351 underlying_strtod (const char *nptr
, char **endptr
)
354 return strtod (nptr
, endptr
);