1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2024 Free Software
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #if ! (defined USE_FLOAT || defined USE_LONG_DOUBLE)
24 #include <ctype.h> /* isspace() */
26 #include <float.h> /* {FLT,DBL,LDBL}_{MIN,MAX} */
27 #include <limits.h> /* LONG_{MIN,MAX} */
28 #include <locale.h> /* localeconv() */
29 #include <math.h> /* NAN */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strdup() */
33 # include <langinfo.h>
41 # define STRTOD strtof
43 # define HAVE_UNDERLYING_STRTOD HAVE_STRTOF
47 # define L_(literal) literal##f
48 # if HAVE_LDEXPF_IN_LIBC
53 #elif defined USE_LONG_DOUBLE
54 # define STRTOD strtold
56 # if defined __hpux && defined __hppa
57 /* We cannot call strtold on HP-UX/hppa, because its return type is a struct,
58 not a 'long double'. */
59 # define HAVE_UNDERLYING_STRTOD 0
60 # elif STRTOLD_HAS_UNDERFLOW_BUG
61 /* strtold would not set errno=ERANGE upon underflow. */
62 # define HAVE_UNDERLYING_STRTOD 0
64 # define HAVE_UNDERLYING_STRTOD HAVE_STRTOLD
66 # define DOUBLE long double
69 # define L_(literal) literal##L
70 # if HAVE_LDEXPL_IN_LIBC
76 # define STRTOD strtod
78 # define HAVE_UNDERLYING_STRTOD 1
79 # define DOUBLE double
82 # define L_(literal) literal
83 # if HAVE_LDEXP_IN_LIBC
90 /* Return true if C is a space in the current locale, avoiding
91 problems with signed char and isspace. */
93 locale_isspace (char c
)
96 return isspace (uc
) != 0;
99 /* Determine the decimal-point character according to the current locale. */
101 decimal_point_char (void)
104 /* Determine it in a multithread-safe way. We know nl_langinfo is
105 multithread-safe on glibc systems and Mac OS X systems, but is not required
106 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
107 localeconv() is rarely multithread-safe. */
108 #if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
109 point
= nl_langinfo (RADIXCHAR
);
112 sprintf (pointbuf
, "%#.0f", 1.0);
113 point
= &pointbuf
[1];
115 point
= localeconv () -> decimal_point
;
117 /* The decimal point is always a single byte: either '.' or ','. */
118 return (point
[0] != '\0' ? point
[0] : '.');
123 #define LDEXP dummy_ldexp
124 /* A dummy definition that will never be invoked. */
125 static DOUBLE
LDEXP (_GL_UNUSED DOUBLE x
, _GL_UNUSED
int exponent
)
132 /* Return X * BASE**EXPONENT. Return an extreme value and set errno
133 to ERANGE if underflow or overflow occurs. */
135 scale_radix_exp (DOUBLE x
, int radix
, long int exponent
)
137 /* If RADIX == 10, this code is neither precise nor fast; it is
138 merely a straightforward and relatively portable approximation.
139 If N == 2, this code is precise on a radix-2 implementation,
140 albeit perhaps not fast if ldexp is not in libc. */
142 long int e
= exponent
;
144 if (USE_LDEXP
&& radix
== 2)
145 return LDEXP (x
, e
< INT_MIN
? INT_MIN
: INT_MAX
< e
? INT_MAX
: e
);
157 if (r
== 0 && x
!= 0)
168 if (r
< -MAX
/ radix
)
173 else if (MAX
/ radix
< r
)
188 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR)
189 except there are no leading spaces or signs or "0x", and ENDPTR is
190 nonnull. The number uses a base BASE (either 10 or 16) fraction, a
191 radix RADIX (either 10 or 2) exponent, and exponent character
192 EXPCHAR. BASE is RADIX**RADIX_MULTIPLIER. */
194 parse_number (const char *nptr
,
195 int base
, int radix
, int radix_multiplier
, char radixchar
,
199 const char *s
= nptr
;
200 const char *digits_start
;
201 const char *digits_end
;
202 const char *radixchar_ptr
;
206 /* First, determine the start and end of the digit sequence. */
208 radixchar_ptr
= NULL
;
211 if (base
== 16 ? c_isxdigit (*s
) : c_isdigit (*s
))
213 else if (radixchar_ptr
== NULL
&& *s
== radixchar
)
215 /* Record that we have found the decimal point. */
219 /* Any other character terminates the digit sequence. */
223 /* Now radixchar_ptr == NULL or
224 digits_start <= radixchar_ptr < digits_end. */
229 (radixchar_ptr
!= NULL
230 ? - (long int) (digits_end
- radixchar_ptr
- 1)
234 { /* Remove trailing zero digits. This reduces rounding errors for
235 inputs such as 1.0000000000 or 10000000000e-10. */
236 while (digits_end
> digits_start
)
238 if (digits_end
- 1 == radixchar_ptr
|| *(digits_end
- 1) == '0')
244 (radixchar_ptr
!= NULL
245 ? (digits_end
> radixchar_ptr
246 ? - (long int) (digits_end
- radixchar_ptr
- 1)
247 : (long int) (radixchar_ptr
- digits_end
))
248 : (long int) (s
- digits_end
));
251 /* Then, convert the digit sequence to a number. */
255 for (dp
= digits_start
; dp
< digits_end
; dp
++)
256 if (dp
!= radixchar_ptr
)
260 /* Make sure that multiplication by BASE will not overflow. */
261 if (!(num
<= MAX
/ base
))
263 /* The value of the digit and all subsequent digits don't matter,
264 since we have already gotten as many digits as can be
265 represented in a 'DOUBLE'. This doesn't necessarily mean that
266 the result will overflow: The exponent may reduce it to within
270 - (radixchar_ptr
>= dp
&& radixchar_ptr
< digits_end
? 1 : 0);
274 /* Eat the next digit. */
277 else if (base
== 16 && c_isxdigit (*dp
))
278 digit
= c_tolower (*dp
) - ('a' - 10);
281 num
= num
* base
+ digit
;
285 exponent
= exponent
* radix_multiplier
;
287 /* Finally, parse the exponent. */
288 if (c_tolower (*s
) == expchar
&& ! locale_isspace (s
[1]))
290 /* Add any given exponent to the implicit one. */
291 int saved_errno
= errno
;
293 long int value
= strtol (s
+ 1, &end
, 10);
298 /* Skip past the exponent, and add in the implicit exponent,
299 resulting in an extreme value on overflow. */
303 ? (value
< LONG_MIN
- exponent
? LONG_MIN
: exponent
+ value
)
304 : (LONG_MAX
- exponent
< value
? LONG_MAX
: exponent
+ value
));
308 *endptr
= (char *) s
;
309 return scale_radix_exp (num
, radix
, exponent
);
312 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
313 ICC 10.0 has a bug when optimizing the expression -zero.
314 The expression -MIN * MIN does not work when cross-compiling
315 to PowerPC on Mac OS X 10.5. */
319 #if defined __hpux || defined __sgi || defined __ICC
326 /* Convert NPTR to a DOUBLE. If ENDPTR is not NULL, a pointer to the
327 character after the last one used in the number is put in *ENDPTR. */
329 STRTOD (const char *nptr
, char **endptr
)
330 #if HAVE_UNDERLYING_STRTOD
331 # if defined USE_FLOAT
333 # elif defined USE_LONG_DOUBLE
340 # define STRTOD(NPTR,ENDPTR) \
341 parse_number (NPTR, 10, 10, 1, radixchar, 'e', ENDPTR)
343 /* From here on, STRTOD refers to the underlying implementation. It needs
344 to handle only finite unsigned decimal numbers with non-null ENDPTR. */
347 bool negative
= false;
349 /* The number so far. */
352 const char *s
= nptr
;
355 int saved_errno
= errno
;
357 radixchar
= decimal_point_char ();
359 /* Eat whitespace. */
360 while (locale_isspace (*s
))
364 negative
= *s
== '-';
365 if (*s
== '-' || *s
== '+')
368 num
= STRTOD (s
, &endbuf
);
371 if (c_isdigit (s
[*s
== radixchar
]))
373 /* If a hex float was converted incorrectly, do it ourselves.
374 If the string starts with "0x" but does not contain digits,
375 consume the "0" ourselves. If a hex float is followed by a
376 'p' but no exponent, then adjust the end pointer. */
377 if (*s
== '0' && c_tolower (s
[1]) == 'x')
379 if (! c_isxdigit (s
[2 + (s
[2] == radixchar
)]))
383 /* strtod() on z/OS returns ERANGE for "0x". */
386 else if (end
<= s
+ 2)
388 num
= parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
393 const char *p
= s
+ 2;
394 while (p
< end
&& c_tolower (*p
) != 'p')
396 if (p
< end
&& ! c_isdigit (p
[1 + (p
[1] == '-' || p
[1] == '+')]))
398 char *dup
= strdup (s
);
402 /* Not really our day, is it. Rounding errors are
403 better than outright failure. */
405 parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
410 num
= STRTOD (dup
, &endbuf
);
421 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
422 underlying STRTOD on a copy of the original string
423 truncated to avoid the bug. */
424 const char *e
= s
+ 1;
425 while (e
< end
&& c_tolower (*e
) != 'e')
427 if (e
< end
&& ! c_isdigit (e
[1 + (e
[1] == '-' || e
[1] == '+')]))
429 char *dup
= strdup (s
);
433 /* Not really our day, is it. Rounding errors are
434 better than outright failure. */
435 num
= parse_number (s
, 10, 10, 1, radixchar
, 'e', &endbuf
);
440 num
= STRTOD (dup
, &endbuf
);
452 /* Check for infinities and NaNs. */
453 else if (c_tolower (*s
) == 'i'
454 && c_tolower (s
[1]) == 'n'
455 && c_tolower (s
[2]) == 'f')
458 if (c_tolower (*s
) == 'i'
459 && c_tolower (s
[1]) == 'n'
460 && c_tolower (s
[2]) == 'i'
461 && c_tolower (s
[3]) == 't'
462 && c_tolower (s
[4]) == 'y')
467 else if (c_tolower (*s
) == 'n'
468 && c_tolower (s
[1]) == 'a'
469 && c_tolower (s
[2]) == 'n')
474 const char *p
= s
+ 1;
475 while (c_isalnum (*p
))
481 /* If the underlying implementation misparsed the NaN, assume
482 its result is incorrect, and return a NaN. Normally it's
483 better to use the underlying implementation's result, since a
484 nice implementation populates the bits of the NaN according
485 to interpreting n-char-sequence as a hexadecimal number. */
486 if (s
!= end
|| num
== num
)
492 /* No conversion could be performed. */
498 *endptr
= (char *) s
;
499 /* Special case -0.0, since at least ICC miscompiles negation. We
500 can't use copysign(), as that drags in -lm on some platforms. */
501 if (!num
&& negative
)
502 return minus_zero ();
503 return negative
? -num
: num
;