1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2020 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
48 # elif STRTOLD_HAS_UNDERFLOW_BUG
49 /* strtold would not set errno=ERANGE upon underflow. */
50 # define HAVE_UNDERLYING_STRTOD 0
52 # define HAVE_UNDERLYING_STRTOD HAVE_STRTOLD
54 # define DOUBLE long double
57 # define L_(literal) literal##L
59 # define STRTOD strtod
61 # define HAVE_UNDERLYING_STRTOD 1
62 # define DOUBLE double
65 # define L_(literal) literal
68 #if (defined USE_LONG_DOUBLE ? HAVE_LDEXPM_IN_LIBC : HAVE_LDEXP_IN_LIBC)
74 /* Return true if C is a space in the current locale, avoiding
75 problems with signed char and isspace. */
77 locale_isspace (char c
)
80 return isspace (uc
) != 0;
83 /* Determine the decimal-point character according to the current locale. */
85 decimal_point_char (void)
88 /* Determine it in a multithread-safe way. We know nl_langinfo is
89 multithread-safe on glibc systems and Mac OS X systems, but is not required
90 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
91 localeconv() is rarely multithread-safe. */
92 #if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
93 point
= nl_langinfo (RADIXCHAR
);
96 sprintf (pointbuf
, "%#.0f", 1.0);
99 point
= localeconv () -> decimal_point
;
101 /* The decimal point is always a single byte: either '.' or ','. */
102 return (point
[0] != '\0' ? point
[0] : '.');
107 #define LDEXP dummy_ldexp
108 /* A dummy definition that will never be invoked. */
109 static DOUBLE
LDEXP (DOUBLE x _GL_UNUSED
, int exponent _GL_UNUSED
)
116 /* Return X * BASE**EXPONENT. Return an extreme value and set errno
117 to ERANGE if underflow or overflow occurs. */
119 scale_radix_exp (DOUBLE x
, int radix
, long int exponent
)
121 /* If RADIX == 10, this code is neither precise nor fast; it is
122 merely a straightforward and relatively portable approximation.
123 If N == 2, this code is precise on a radix-2 implementation,
124 albeit perhaps not fast if ldexp is not in libc. */
126 long int e
= exponent
;
128 if (USE_LDEXP
&& radix
== 2)
129 return LDEXP (x
, e
< INT_MIN
? INT_MIN
: INT_MAX
< e
? INT_MAX
: e
);
141 if (r
== 0 && x
!= 0)
152 if (r
< -MAX
/ radix
)
157 else if (MAX
/ radix
< r
)
172 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR)
173 except there are no leading spaces or signs or "0x", and ENDPTR is
174 nonnull. The number uses a base BASE (either 10 or 16) fraction, a
175 radix RADIX (either 10 or 2) exponent, and exponent character
176 EXPCHAR. BASE is RADIX**RADIX_MULTIPLIER. */
178 parse_number (const char *nptr
,
179 int base
, int radix
, int radix_multiplier
, char radixchar
,
183 const char *s
= nptr
;
184 const char *digits_start
;
185 const char *digits_end
;
186 const char *radixchar_ptr
;
190 /* First, determine the start and end of the digit sequence. */
192 radixchar_ptr
= NULL
;
195 if (base
== 16 ? c_isxdigit (*s
) : c_isdigit (*s
))
197 else if (radixchar_ptr
== NULL
&& *s
== radixchar
)
199 /* Record that we have found the decimal point. */
203 /* Any other character terminates the digit sequence. */
207 /* Now radixchar_ptr == NULL or
208 digits_start <= radixchar_ptr < digits_end. */
213 (radixchar_ptr
!= NULL
214 ? - (long int) (digits_end
- radixchar_ptr
- 1)
218 { /* Remove trailing zero digits. This reduces rounding errors for
219 inputs such as 1.0000000000 or 10000000000e-10. */
220 while (digits_end
> digits_start
)
222 if (digits_end
- 1 == radixchar_ptr
|| *(digits_end
- 1) == '0')
228 (radixchar_ptr
!= NULL
229 ? (digits_end
> radixchar_ptr
230 ? - (long int) (digits_end
- radixchar_ptr
- 1)
231 : (long int) (radixchar_ptr
- digits_end
))
232 : (long int) (s
- digits_end
));
235 /* Then, convert the digit sequence to a number. */
239 for (dp
= digits_start
; dp
< digits_end
; dp
++)
240 if (dp
!= radixchar_ptr
)
244 /* Make sure that multiplication by BASE will not overflow. */
245 if (!(num
<= MAX
/ base
))
247 /* The value of the digit and all subsequent digits don't matter,
248 since we have already gotten as many digits as can be
249 represented in a 'DOUBLE'. This doesn't necessarily mean that
250 the result will overflow: The exponent may reduce it to within
254 - (radixchar_ptr
>= dp
&& radixchar_ptr
< digits_end
? 1 : 0);
258 /* Eat the next digit. */
261 else if (base
== 16 && c_isxdigit (*dp
))
262 digit
= c_tolower (*dp
) - ('a' - 10);
265 num
= num
* base
+ digit
;
269 exponent
= exponent
* radix_multiplier
;
271 /* Finally, parse the exponent. */
272 if (c_tolower (*s
) == expchar
&& ! locale_isspace (s
[1]))
274 /* Add any given exponent to the implicit one. */
275 int saved_errno
= errno
;
277 long int value
= strtol (s
+ 1, &end
, 10);
282 /* Skip past the exponent, and add in the implicit exponent,
283 resulting in an extreme value on overflow. */
287 ? (value
< LONG_MIN
- exponent
? LONG_MIN
: exponent
+ value
)
288 : (LONG_MAX
- exponent
< value
? LONG_MAX
: exponent
+ value
));
292 *endptr
= (char *) s
;
293 return scale_radix_exp (num
, radix
, exponent
);
296 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
297 ICC 10.0 has a bug when optimizing the expression -zero.
298 The expression -MIN * MIN does not work when cross-compiling
299 to PowerPC on Mac OS X 10.5. */
303 #if defined __hpux || defined __sgi || defined __ICC
310 /* Convert NPTR to a DOUBLE. If ENDPTR is not NULL, a pointer to the
311 character after the last one used in the number is put in *ENDPTR. */
313 STRTOD (const char *nptr
, char **endptr
)
314 #if HAVE_UNDERLYING_STRTOD
315 # ifdef USE_LONG_DOUBLE
322 # define STRTOD(NPTR,ENDPTR) \
323 parse_number (NPTR, 10, 10, 1, radixchar, 'e', ENDPTR)
325 /* From here on, STRTOD refers to the underlying implementation. It needs
326 to handle only finite unsigned decimal numbers with non-null ENDPTR. */
329 bool negative
= false;
331 /* The number so far. */
334 const char *s
= nptr
;
337 int saved_errno
= errno
;
339 radixchar
= decimal_point_char ();
341 /* Eat whitespace. */
342 while (locale_isspace (*s
))
346 negative
= *s
== '-';
347 if (*s
== '-' || *s
== '+')
350 num
= STRTOD (s
, &endbuf
);
353 if (c_isdigit (s
[*s
== radixchar
]))
355 /* If a hex float was converted incorrectly, do it ourselves.
356 If the string starts with "0x" but does not contain digits,
357 consume the "0" ourselves. If a hex float is followed by a
358 'p' but no exponent, then adjust the end pointer. */
359 if (*s
== '0' && c_tolower (s
[1]) == 'x')
361 if (! c_isxdigit (s
[2 + (s
[2] == radixchar
)]))
365 /* strtod() on z/OS returns ERANGE for "0x". */
368 else if (end
<= s
+ 2)
370 num
= parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
375 const char *p
= s
+ 2;
376 while (p
< end
&& c_tolower (*p
) != 'p')
378 if (p
< end
&& ! c_isdigit (p
[1 + (p
[1] == '-' || p
[1] == '+')]))
380 char *dup
= strdup (s
);
384 /* Not really our day, is it. Rounding errors are
385 better than outright failure. */
387 parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
392 num
= STRTOD (dup
, &endbuf
);
403 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
404 underlying STRTOD on a copy of the original string
405 truncated to avoid the bug. */
406 const char *e
= s
+ 1;
407 while (e
< end
&& c_tolower (*e
) != 'e')
409 if (e
< end
&& ! c_isdigit (e
[1 + (e
[1] == '-' || e
[1] == '+')]))
411 char *dup
= strdup (s
);
415 /* Not really our day, is it. Rounding errors are
416 better than outright failure. */
417 num
= parse_number (s
, 10, 10, 1, radixchar
, 'e', &endbuf
);
422 num
= STRTOD (dup
, &endbuf
);
434 /* Check for infinities and NaNs. */
435 else if (c_tolower (*s
) == 'i'
436 && c_tolower (s
[1]) == 'n'
437 && c_tolower (s
[2]) == 'f')
440 if (c_tolower (*s
) == 'i'
441 && c_tolower (s
[1]) == 'n'
442 && c_tolower (s
[2]) == 'i'
443 && c_tolower (s
[3]) == 't'
444 && c_tolower (s
[4]) == 'y')
449 else if (c_tolower (*s
) == 'n'
450 && c_tolower (s
[1]) == 'a'
451 && c_tolower (s
[2]) == 'n')
456 const char *p
= s
+ 1;
457 while (c_isalnum (*p
))
463 /* If the underlying implementation misparsed the NaN, assume
464 its result is incorrect, and return a NaN. Normally it's
465 better to use the underlying implementation's result, since a
466 nice implementation populates the bits of the NaN according
467 to interpreting n-char-sequence as a hexadecimal number. */
468 if (s
!= end
|| num
== num
)
474 /* No conversion could be performed. */
480 *endptr
= (char *) s
;
481 /* Special case -0.0, since at least ICC miscompiles negation. We
482 can't use copysign(), as that drags in -lm on some platforms. */
483 if (!num
&& negative
)
484 return minus_zero ();
485 return negative
? -num
: num
;