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 # if STRTOF_HAS_UNDERFLOW_BUG
44 /* strtof would not set errno=ERANGE upon flush-to-zero underflow. */
45 # define HAVE_UNDERLYING_STRTOD 0
47 # define HAVE_UNDERLYING_STRTOD HAVE_STRTOF
49 # define HAS_GRADUAL_UNDERFLOW_PROBLEM STRTOF_HAS_GRADUAL_UNDERFLOW_PROBLEM
53 # define L_(literal) literal##f
54 # if HAVE_LDEXPF_IN_LIBC
59 #elif defined USE_LONG_DOUBLE
60 # define STRTOD strtold
62 # if defined __hpux && defined __hppa
63 /* We cannot call strtold on HP-UX/hppa, because its return type is a struct,
64 not a 'long double'. */
65 # define HAVE_UNDERLYING_STRTOD 0
66 # elif STRTOLD_HAS_UNDERFLOW_BUG
67 /* strtold would not set errno=ERANGE upon flush-to-zero underflow. */
68 # define HAVE_UNDERLYING_STRTOD 0
69 # elif defined __MINGW32__ && __MINGW64_VERSION_MAJOR < 10
70 /* strtold is broken in mingw versions before 10.0:
71 - Up to mingw 5.0.x, it leaks memory at every invocation.
72 - Up to mingw 9.0.x, it allocates an unbounded amount of stack.
73 See <https://github.com/mingw-w64/mingw-w64/commit/450309b97b2e839ea02887dfaf0f1d10fb5d40cc>
74 and <https://github.com/mingw-w64/mingw-w64/commit/73806c0709b7e6c0f6587f11a955743670e85470>. */
75 # define HAVE_UNDERLYING_STRTOD 0
76 # elif defined __HAIKU__
77 /* Haiku's strtold maps denormalized numbers to zero.
78 <https://dev.haiku-os.org/ticket/19040> */
79 # define HAVE_UNDERLYING_STRTOD 0
81 # define HAVE_UNDERLYING_STRTOD HAVE_STRTOLD
83 # define HAS_GRADUAL_UNDERFLOW_PROBLEM STRTOLD_HAS_GRADUAL_UNDERFLOW_PROBLEM
84 # define DOUBLE long double
87 # define L_(literal) literal##L
88 # if HAVE_LDEXPL_IN_LIBC
94 # define STRTOD strtod
96 # if STRTOD_HAS_UNDERFLOW_BUG
97 /* strtod would not set errno=ERANGE upon flush-to-zero underflow. */
98 # define HAVE_UNDERLYING_STRTOD 0
100 # define HAVE_UNDERLYING_STRTOD 1
102 # define HAS_GRADUAL_UNDERFLOW_PROBLEM STRTOD_HAS_GRADUAL_UNDERFLOW_PROBLEM
103 # define DOUBLE double
106 # define L_(literal) literal
107 # if HAVE_LDEXP_IN_LIBC
114 /* Return true if C is a space in the current locale, avoiding
115 problems with signed char and isspace. */
117 locale_isspace (char c
)
119 unsigned char uc
= c
;
120 return isspace (uc
) != 0;
123 /* Determine the decimal-point character according to the current locale. */
125 decimal_point_char (void)
128 /* Determine it in a multithread-safe way. We know nl_langinfo is
129 multithread-safe on glibc systems and Mac OS X systems, but is not required
130 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
131 localeconv() is rarely multithread-safe. */
132 #if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
133 point
= nl_langinfo (RADIXCHAR
);
136 sprintf (pointbuf
, "%#.0f", 1.0);
137 point
= &pointbuf
[1];
139 point
= localeconv () -> decimal_point
;
141 /* The decimal point is always a single byte: either '.' or ','. */
142 return (point
[0] != '\0' ? point
[0] : '.');
147 #define LDEXP dummy_ldexp
148 /* A dummy definition that will never be invoked. */
149 static DOUBLE
LDEXP (_GL_UNUSED DOUBLE x
, _GL_UNUSED
int exponent
)
156 /* Return X * BASE**EXPONENT. Return an extreme value and set errno
157 to ERANGE if underflow or overflow occurs. */
159 scale_radix_exp (DOUBLE x
, int radix
, long int exponent
)
161 /* If RADIX == 10, this code is neither precise nor fast; it is
162 merely a straightforward and relatively portable approximation.
163 If N == 2, this code is precise on a radix-2 implementation,
164 albeit perhaps not fast if ldexp is not in libc. */
166 long int e
= exponent
;
168 if (USE_LDEXP
&& radix
== 2)
169 return LDEXP (x
, e
< INT_MIN
? INT_MIN
: INT_MAX
< e
? INT_MAX
: e
);
182 if (r
< MIN
&& r
> -MIN
)
183 /* Gradual underflow, resulting in a denormalized
191 /* Flush-to-zero underflow. */
201 if (r
< -MAX
/ radix
)
206 else if (MAX
/ radix
< r
)
221 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR)
222 except there are no leading spaces or signs or "0x", and ENDPTR is
223 nonnull. The number uses a base BASE (either 10 or 16) fraction, a
224 radix RADIX (either 10 or 2) exponent, and exponent character
225 EXPCHAR. BASE is RADIX**RADIX_MULTIPLIER. */
227 parse_number (const char *nptr
,
228 int base
, int radix
, int radix_multiplier
, char radixchar
,
232 const char *s
= nptr
;
233 const char *digits_start
;
234 const char *digits_end
;
235 const char *radixchar_ptr
;
239 /* First, determine the start and end of the digit sequence. */
241 radixchar_ptr
= NULL
;
244 if (base
== 16 ? c_isxdigit (*s
) : c_isdigit (*s
))
246 else if (radixchar_ptr
== NULL
&& *s
== radixchar
)
248 /* Record that we have found the decimal point. */
252 /* Any other character terminates the digit sequence. */
256 /* Now radixchar_ptr == NULL or
257 digits_start <= radixchar_ptr < digits_end. */
262 (radixchar_ptr
!= NULL
263 ? - (long int) (digits_end
- radixchar_ptr
- 1)
267 { /* Remove trailing zero digits. This reduces rounding errors for
268 inputs such as 1.0000000000 or 10000000000e-10. */
269 while (digits_end
> digits_start
)
271 if (digits_end
- 1 == radixchar_ptr
|| *(digits_end
- 1) == '0')
277 (radixchar_ptr
!= NULL
278 ? (digits_end
> radixchar_ptr
279 ? - (long int) (digits_end
- radixchar_ptr
- 1)
280 : (long int) (radixchar_ptr
- digits_end
))
281 : (long int) (s
- digits_end
));
284 /* Then, convert the digit sequence to a number. */
288 for (dp
= digits_start
; dp
< digits_end
; dp
++)
289 if (dp
!= radixchar_ptr
)
293 /* Make sure that multiplication by BASE will not overflow. */
294 if (!(num
<= MAX
/ base
))
296 /* The value of the digit and all subsequent digits don't matter,
297 since we have already gotten as many digits as can be
298 represented in a 'DOUBLE'. This doesn't necessarily mean that
299 the result will overflow: The exponent may reduce it to within
303 - (radixchar_ptr
>= dp
&& radixchar_ptr
< digits_end
? 1 : 0);
307 /* Eat the next digit. */
310 else if (base
== 16 && c_isxdigit (*dp
))
311 digit
= c_tolower (*dp
) - ('a' - 10);
314 num
= num
* base
+ digit
;
318 exponent
= exponent
* radix_multiplier
;
320 /* Finally, parse the exponent. */
321 if (c_tolower (*s
) == expchar
&& ! locale_isspace (s
[1]))
323 /* Add any given exponent to the implicit one. */
324 int saved_errno
= errno
;
326 long int value
= strtol (s
+ 1, &end
, 10);
331 /* Skip past the exponent, and add in the implicit exponent,
332 resulting in an extreme value on overflow. */
336 ? (value
< LONG_MIN
- exponent
? LONG_MIN
: exponent
+ value
)
337 : (LONG_MAX
- exponent
< value
? LONG_MAX
: exponent
+ value
));
341 *endptr
= (char *) s
;
342 return scale_radix_exp (num
, radix
, exponent
);
345 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
346 ICC 10.0 has a bug when optimizing the expression -zero.
347 The expression -MIN * MIN does not work when cross-compiling
348 to PowerPC on Mac OS X 10.5. */
352 #if defined __hpux || defined __sgi || defined __ICC
359 /* Convert NPTR to a DOUBLE. If ENDPTR is not NULL, a pointer to the
360 character after the last one used in the number is put in *ENDPTR. */
362 STRTOD (const char *nptr
, char **endptr
)
363 #if HAVE_UNDERLYING_STRTOD
364 # if defined USE_FLOAT
366 # elif defined USE_LONG_DOUBLE
371 # if HAS_GRADUAL_UNDERFLOW_PROBLEM
372 # define SET_ERRNO_UPON_GRADUAL_UNDERFLOW(RESULT) \
375 if ((RESULT) != 0 && (RESULT) < MIN && (RESULT) > -MIN) \
380 # define SET_ERRNO_UPON_GRADUAL_UNDERFLOW(RESULT) (void)0
384 # define STRTOD(NPTR,ENDPTR) \
385 parse_number (NPTR, 10, 10, 1, radixchar, 'e', ENDPTR)
386 # define SET_ERRNO_UPON_GRADUAL_UNDERFLOW(RESULT) (void)0
388 /* From here on, STRTOD refers to the underlying implementation. It needs
389 to handle only finite unsigned decimal numbers with non-null ENDPTR. */
392 bool negative
= false;
394 /* The number so far. */
397 const char *s
= nptr
;
400 int saved_errno
= errno
;
402 radixchar
= decimal_point_char ();
404 /* Eat whitespace. */
405 while (locale_isspace (*s
))
409 negative
= *s
== '-';
410 if (*s
== '-' || *s
== '+')
413 num
= STRTOD (s
, &endbuf
);
414 SET_ERRNO_UPON_GRADUAL_UNDERFLOW (num
);
417 if (c_isdigit (s
[*s
== radixchar
]))
419 /* If a hex float was converted incorrectly, do it ourselves.
420 If the string starts with "0x" but does not contain digits,
421 consume the "0" ourselves. If a hex float is followed by a
422 'p' but no exponent, then adjust the end pointer. */
423 if (*s
== '0' && c_tolower (s
[1]) == 'x')
425 if (! c_isxdigit (s
[2 + (s
[2] == radixchar
)]))
429 /* strtod() on z/OS returns ERANGE for "0x". */
432 else if (end
<= s
+ 2)
434 num
= parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
439 const char *p
= s
+ 2;
440 while (p
< end
&& c_tolower (*p
) != 'p')
442 if (p
< end
&& ! c_isdigit (p
[1 + (p
[1] == '-' || p
[1] == '+')]))
444 char *dup
= strdup (s
);
448 /* Not really our day, is it. Rounding errors are
449 better than outright failure. */
451 parse_number (s
+ 2, 16, 2, 4, radixchar
, 'p', &endbuf
);
456 num
= STRTOD (dup
, &endbuf
);
457 SET_ERRNO_UPON_GRADUAL_UNDERFLOW (num
);
468 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
469 underlying STRTOD on a copy of the original string
470 truncated to avoid the bug. */
471 const char *e
= s
+ 1;
472 while (e
< end
&& c_tolower (*e
) != 'e')
474 if (e
< end
&& ! c_isdigit (e
[1 + (e
[1] == '-' || e
[1] == '+')]))
476 char *dup
= strdup (s
);
480 /* Not really our day, is it. Rounding errors are
481 better than outright failure. */
482 num
= parse_number (s
, 10, 10, 1, radixchar
, 'e', &endbuf
);
487 num
= STRTOD (dup
, &endbuf
);
488 SET_ERRNO_UPON_GRADUAL_UNDERFLOW (num
);
500 /* Check for infinities and NaNs. */
501 else if (c_tolower (*s
) == 'i'
502 && c_tolower (s
[1]) == 'n'
503 && c_tolower (s
[2]) == 'f')
506 if (c_tolower (*s
) == 'i'
507 && c_tolower (s
[1]) == 'n'
508 && c_tolower (s
[2]) == 'i'
509 && c_tolower (s
[3]) == 't'
510 && c_tolower (s
[4]) == 'y')
515 else if (c_tolower (*s
) == 'n'
516 && c_tolower (s
[1]) == 'a'
517 && c_tolower (s
[2]) == 'n')
522 const char *p
= s
+ 1;
523 while (c_isalnum (*p
))
529 /* If the underlying implementation misparsed the NaN, assume
530 its result is incorrect, and return a NaN. Normally it's
531 better to use the underlying implementation's result, since a
532 nice implementation populates the bits of the NaN according
533 to interpreting n-char-sequence as a hexadecimal number. */
534 if (s
!= end
|| num
== num
)
540 /* No conversion could be performed. */
546 *endptr
= (char *) s
;
547 /* Special case -0.0, since at least ICC miscompiles negation. We
548 can't use copysign(), as that drags in -lm on some platforms. */
549 if (!num
&& negative
)
550 return minus_zero ();
551 return negative
? -num
: num
;