1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2019 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/>. */
17 #if ! defined USE_LONG_DOUBLE
24 #include <ctype.h> /* isspace() */
26 #include <float.h> /* {DBL,LDBL}_{MIN,MAX} */
27 #include <limits.h> /* LONG_{MIN,MAX} */
28 #include <locale.h> /* localeconv() */
29 #include <math.h> /* NAN */
31 #include <stdio.h> /* sprintf() */
32 #include <string.h> /* strdup() */
34 # include <langinfo.h>
41 #ifdef USE_LONG_DOUBLE
42 # define STRTOD strtold
44 # if defined __hpux && defined __hppa
45 /* We cannot call strtold on HP-UX/hppa, because its return type is a struct,
46 not a 'long double'. */
47 # define HAVE_UNDERLYING_STRTOD 0
49 # define HAVE_UNDERLYING_STRTOD HAVE_STRTOLD
51 # define DOUBLE long double
54 # define L_(literal) literal##L
56 # define STRTOD strtod
58 # define HAVE_UNDERLYING_STRTOD 1
59 # define DOUBLE double
62 # define L_(literal) literal
65 #if (defined USE_LONG_DOUBLE ? HAVE_LDEXPM_IN_LIBC : HAVE_LDEXP_IN_LIBC)
71 /* Return true if C is a space in the current locale, avoiding
72 problems with signed char and isspace. */
74 locale_isspace (char c
)
77 return isspace (uc
) != 0;
80 /* Determine the decimal-point character according to the current locale. */
82 decimal_point_char (void)
85 /* Determine it in a multithread-safe way. We know nl_langinfo is
86 multithread-safe on glibc systems and Mac OS X systems, but is not required
87 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
88 localeconv() is rarely multithread-safe. */
89 #if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
90 point
= nl_langinfo (RADIXCHAR
);
93 sprintf (pointbuf
, "%#.0f", 1.0);
96 point
= localeconv () -> decimal_point
;
98 /* The decimal point is always a single byte: either '.' or ','. */
99 return (point
[0] != '\0' ? point
[0] : '.');
104 #define LDEXP dummy_ldexp
105 /* A dummy definition that will never be invoked. */
106 static DOUBLE
LDEXP (DOUBLE x _GL_UNUSED
, int exponent _GL_UNUSED
)
113 /* Return X * BASE**EXPONENT. Return an extreme value and set errno
114 to ERANGE if underflow or overflow occurs. */
116 scale_radix_exp (DOUBLE x
, int radix
, long int exponent
)
118 /* If RADIX == 10, this code is neither precise nor fast; it is
119 merely a straightforward and relatively portable approximation.
120 If N == 2, this code is precise on a radix-2 implementation,
121 albeit perhaps not fast if ldexp is not in libc. */
123 long int e
= exponent
;
125 if (USE_LDEXP
&& radix
== 2)
126 return LDEXP (x
, e
< INT_MIN
? INT_MIN
: INT_MAX
< e
? INT_MAX
: e
);
138 if (r
== 0 && x
!= 0)
149 if (r
< -MAX
/ radix
)
154 else if (MAX
/ radix
< r
)
169 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR)
170 except there are no leading spaces or signs or "0x", and ENDPTR is
171 nonnull. The number uses a base BASE (either 10 or 16) fraction, a
172 radix RADIX (either 10 or 2) exponent, and exponent character
173 EXPCHAR. BASE is RADIX**RADIX_MULTIPLIER. */
175 parse_number (const char *nptr
,
176 int base
, int radix
, int radix_multiplier
, char radixchar
,
180 const char *s
= nptr
;
181 const char *digits_start
;
182 const char *digits_end
;
183 const char *radixchar_ptr
;
187 /* First, determine the start and end of the digit sequence. */
189 radixchar_ptr
= NULL
;
192 if (base
== 16 ? c_isxdigit (*s
) : c_isdigit (*s
))
194 else if (radixchar_ptr
== NULL
&& *s
== radixchar
)
196 /* Record that we have found the decimal point. */
200 /* Any other character terminates the digit sequence. */
204 /* Now radixchar_ptr == NULL or
205 digits_start <= radixchar_ptr < digits_end. */
210 (radixchar_ptr
!= NULL
211 ? - (long int) (digits_end
- radixchar_ptr
- 1)
215 { /* Remove trailing zero digits. This reduces rounding errors for
216 inputs such as 1.0000000000 or 10000000000e-10. */
217 while (digits_end
> digits_start
)
219 if (digits_end
- 1 == radixchar_ptr
|| *(digits_end
- 1) == '0')
225 (radixchar_ptr
!= NULL
226 ? (digits_end
> radixchar_ptr
227 ? - (long int) (digits_end
- radixchar_ptr
- 1)
228 : (long int) (radixchar_ptr
- digits_end
))
229 : (long int) (s
- digits_end
));
232 /* Then, convert the digit sequence to a number. */
236 for (dp
= digits_start
; dp
< digits_end
; dp
++)
237 if (dp
!= radixchar_ptr
)
241 /* Make sure that multiplication by BASE will not overflow. */
242 if (!(num
<= MAX
/ base
))
244 /* The value of the digit and all subsequent digits don't matter,
245 since we have already gotten as many digits as can be
246 represented in a 'DOUBLE'. This doesn't necessarily mean that
247 the result will overflow: The exponent may reduce it to within
251 - (radixchar_ptr
>= dp
&& radixchar_ptr
< digits_end
? 1 : 0);
255 /* Eat the next digit. */
258 else if (base
== 16 && c_isxdigit (*dp
))
259 digit
= c_tolower (*dp
) - ('a' - 10);
262 num
= num
* base
+ digit
;
266 exponent
= exponent
* radix_multiplier
;
268 /* Finally, parse the exponent. */
269 if (c_tolower (*s
) == expchar
&& ! locale_isspace (s
[1]))
271 /* Add any given exponent to the implicit one. */
272 int saved_errno
= errno
;
274 long int value
= strtol (s
+ 1, &end
, 10);
279 /* Skip past the exponent, and add in the implicit exponent,
280 resulting in an extreme value on overflow. */
284 ? (value
< LONG_MIN
- exponent
? LONG_MIN
: exponent
+ value
)
285 : (LONG_MAX
- exponent
< value
? LONG_MAX
: exponent
+ value
));
289 *endptr
= (char *) s
;
290 return scale_radix_exp (num
, radix
, exponent
);
293 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
294 ICC 10.0 has a bug when optimizing the expression -zero.
295 The expression -MIN * MIN does not work when cross-compiling
296 to PowerPC on Mac OS X 10.5. */
297 #if defined __hpux || defined __sgi || defined __ICC
299 compute_minus_zero (void)
303 # define minus_zero compute_minus_zero ()
305 DOUBLE minus_zero
= -0.0;
308 /* Convert NPTR to a DOUBLE. If ENDPTR is not NULL, a pointer to the
309 character after the last one used in the number is put in *ENDPTR. */
311 STRTOD (const char *nptr
, char **endptr
)
312 #if HAVE_UNDERLYING_STRTOD
313 # ifdef USE_LONG_DOUBLE
320 # define STRTOD(NPTR,ENDPTR) \
321 parse_number (NPTR, 10, 10, 1, radixchar, 'e', ENDPTR)
323 /* From here on, STRTOD refers to the underlying implementation. It needs
324 to handle only finite unsigned decimal numbers with non-null ENDPTR. */
327 bool negative
= false;
329 /* The number so far. */
332 const char *s
= nptr
;
335 int saved_errno
= errno
;
337 radixchar
= decimal_point_char ();
339 /* Eat whitespace. */
340 while (locale_isspace (*s
))
344 negative
= *s
== '-';
345 if (*s
== '-' || *s
== '+')
348 num
= STRTOD (s
, &endbuf
);
351 if (c_isdigit (s
[*s
== radixchar
]))
353 /* If a hex float was converted incorrectly, do it ourselves.
354 If the string starts with "0x" but does not contain digits,
355 consume the "0" ourselves. If a hex float is followed by a
356 'p' but no exponent, then adjust the end pointer. */
357 if (*s
== '0' && c_tolower (s
[1]) == 'x')
359 if (! c_isxdigit (s
[2 + (s
[2] == radixchar
)]))
363 /* strtod() on z/OS returns ERANGE for "0x". */
366 else if (end
<= s
+ 2)
368 num
= parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
373 const char *p
= s
+ 2;
374 while (p
< end
&& c_tolower (*p
) != 'p')
376 if (p
< end
&& ! c_isdigit (p
[1 + (p
[1] == '-' || p
[1] == '+')]))
378 char *dup
= strdup (s
);
382 /* Not really our day, is it. Rounding errors are
383 better than outright failure. */
385 parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
390 num
= STRTOD (dup
, &endbuf
);
401 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
402 underlying STRTOD on a copy of the original string
403 truncated to avoid the bug. */
404 const char *e
= s
+ 1;
405 while (e
< end
&& c_tolower (*e
) != 'e')
407 if (e
< end
&& ! c_isdigit (e
[1 + (e
[1] == '-' || e
[1] == '+')]))
409 char *dup
= strdup (s
);
413 /* Not really our day, is it. Rounding errors are
414 better than outright failure. */
415 num
= parse_number (s
, 10, 10, 1, radixchar
, 'e', &endbuf
);
420 num
= STRTOD (dup
, &endbuf
);
432 /* Check for infinities and NaNs. */
433 else if (c_tolower (*s
) == 'i'
434 && c_tolower (s
[1]) == 'n'
435 && c_tolower (s
[2]) == 'f')
438 if (c_tolower (*s
) == 'i'
439 && c_tolower (s
[1]) == 'n'
440 && c_tolower (s
[2]) == 'i'
441 && c_tolower (s
[3]) == 't'
442 && c_tolower (s
[4]) == 'y')
447 else if (c_tolower (*s
) == 'n'
448 && c_tolower (s
[1]) == 'a'
449 && c_tolower (s
[2]) == 'n')
454 const char *p
= s
+ 1;
455 while (c_isalnum (*p
))
461 /* If the underlying implementation misparsed the NaN, assume
462 its result is incorrect, and return a NaN. Normally it's
463 better to use the underlying implementation's result, since a
464 nice implementation populates the bits of the NaN according
465 to interpreting n-char-sequence as a hexadecimal number. */
466 if (s
!= end
|| num
== num
)
472 /* No conversion could be performed. */
478 *endptr
= (char *) s
;
479 /* Special case -0.0, since at least ICC miscompiles negation. We
480 can't use copysign(), as that drags in -lm on some platforms. */
481 if (!num
&& negative
)
483 return negative
? -num
: num
;