Mercurial > gnulib
annotate lib/strtod.c @ 17334:892b14640382
* lib/strtod.c (HAVE_RAW_DECL_STRTOD): Remove; no longer used.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 19 Feb 2013 23:40:06 -0800 |
parents | 201622d6d12f |
children | 344018b6e5d7 |
rev | line source |
---|---|
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16935
diff
changeset
|
1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2013 Free Software |
12559
c2cbabec01dd
update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents:
12422
diff
changeset
|
2 Foundation, Inc. |
9 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
311 | 5 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 (at your option) any later version. |
9 | 8 |
311 | 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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
309 | 16 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6930
diff
changeset
|
17 #include <config.h> |
311 | 18 |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
19 #include <stdlib.h> |
373 | 20 |
311 | 21 #include <ctype.h> |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
22 #include <errno.h> |
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
23 #include <float.h> |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
24 #include <limits.h> |
311 | 25 #include <math.h> |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
26 #include <stdbool.h> |
13655
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
27 #include <string.h> |
9 | 28 |
9822 | 29 #include "c-ctype.h" |
30 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
31 #ifndef HAVE_LDEXP_IN_LIBC |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
32 #define HAVE_LDEXP_IN_LIBC 0 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
33 #endif |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
34 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
35 /* Return true if C is a space in the current locale, avoiding |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
36 problems with signed char and isspace. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
37 static bool |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
38 locale_isspace (char c) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
39 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
40 unsigned char uc = c; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
41 return isspace (uc) != 0; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
42 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
43 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
44 #if !HAVE_LDEXP_IN_LIBC |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
45 #define ldexp dummy_ldexp |
13464 | 46 /* A dummy definition that will never be invoked. */ |
47 static double ldexp (double x _GL_UNUSED, int exponent _GL_UNUSED) | |
48 { | |
49 abort (); | |
50 return 0.0; | |
51 } | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
52 #endif |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
53 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
54 /* Return X * BASE**EXPONENT. Return an extreme value and set errno |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
55 to ERANGE if underflow or overflow occurs. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
56 static double |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
57 scale_radix_exp (double x, int radix, long int exponent) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
58 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
59 /* If RADIX == 10, this code is neither precise nor fast; it is |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
60 merely a straightforward and relatively portable approximation. |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
61 If N == 2, this code is precise on a radix-2 implementation, |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
62 albeit perhaps not fast if ldexp is not in libc. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
63 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
64 long int e = exponent; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
65 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
66 if (HAVE_LDEXP_IN_LIBC && radix == 2) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
67 return ldexp (x, e < INT_MIN ? INT_MIN : INT_MAX < e ? INT_MAX : e); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
68 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
69 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
70 double r = x; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
71 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
72 if (r != 0) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
73 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
74 if (e < 0) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
75 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
76 while (e++ != 0) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
77 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
78 r /= radix; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
79 if (r == 0 && x != 0) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
80 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
81 errno = ERANGE; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
82 break; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
83 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
84 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
85 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
86 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
87 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
88 while (e-- != 0) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
89 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
90 if (r < -DBL_MAX / radix) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
91 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
92 errno = ERANGE; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
93 return -HUGE_VAL; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
94 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
95 else if (DBL_MAX / radix < r) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
96 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
97 errno = ERANGE; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
98 return HUGE_VAL; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
99 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
100 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
101 r *= radix; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
102 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
103 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
104 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
105 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
106 return r; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
107 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
108 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
109 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
110 /* Parse a number at NPTR; this is a bit like strtol (NPTR, ENDPTR) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
111 except there are no leading spaces or signs or "0x", and ENDPTR is |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
112 nonnull. The number uses a base BASE (either 10 or 16) fraction, a |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
113 radix RADIX (either 10 or 2) exponent, and exponent character |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
114 EXPCHAR. To convert from a number of digits to a radix exponent, |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
115 multiply by RADIX_MULTIPLIER (either 1 or 4). */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
116 static double |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
117 parse_number (const char *nptr, |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
118 int base, int radix, int radix_multiplier, char expchar, |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
119 char **endptr) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
120 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
121 const char *s = nptr; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
122 bool got_dot = false; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
123 long int exponent = 0; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
124 double num = 0; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
125 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
126 for (;; ++s) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
127 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
128 int digit; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
129 if (c_isdigit (*s)) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
130 digit = *s - '0'; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
131 else if (base == 16 && c_isxdigit (*s)) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
132 digit = c_tolower (*s) - ('a' - 10); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
133 else if (! got_dot && *s == '.') |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
134 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
135 /* Record that we have found the decimal point. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
136 got_dot = true; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
137 continue; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
138 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
139 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
140 /* Any other character terminates the number. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
141 break; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
142 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
143 /* Make sure that multiplication by base will not overflow. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
144 if (num <= DBL_MAX / base) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
145 num = num * base + digit; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
146 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
147 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
148 /* The value of the digit doesn't matter, since we have already |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
149 gotten as many digits as can be represented in a 'double'. |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
150 This doesn't necessarily mean the result will overflow. |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
151 The exponent may reduce it to within range. |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
152 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
153 We just need to record that there was another |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
154 digit so that we can multiply by 10 later. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
155 exponent += radix_multiplier; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
156 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
157 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
158 /* Keep track of the number of digits after the decimal point. |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
159 If we just divided by base here, we might lose precision. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
160 if (got_dot) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
161 exponent -= radix_multiplier; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
162 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
163 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
164 if (c_tolower (*s) == expchar && ! locale_isspace (s[1])) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
165 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
166 /* Add any given exponent to the implicit one. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
167 int save = errno; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
168 char *end; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
169 long int value = strtol (s + 1, &end, 10); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
170 errno = save; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
171 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
172 if (s + 1 != end) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
173 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
174 /* Skip past the exponent, and add in the implicit exponent, |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
175 resulting in an extreme value on overflow. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
176 s = end; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
177 exponent = |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
178 (exponent < 0 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
179 ? (value < LONG_MIN - exponent ? LONG_MIN : exponent + value) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
180 : (LONG_MAX - exponent < value ? LONG_MAX : exponent + value)); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
181 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
182 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
183 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
184 *endptr = (char *) s; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
185 return scale_radix_exp (num, radix, exponent); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
186 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
187 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
188 static double underlying_strtod (const char *, char **); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
189 |
13837
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
190 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0. |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
191 ICC 10.0 has a bug when optimizing the expression -zero. |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
192 The expression -DBL_MIN * DBL_MIN does not work when cross-compiling |
16935
498a2211d839
Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents:
16235
diff
changeset
|
193 to PowerPC on Mac OS X 10.5. */ |
13837
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
194 #if defined __hpux || defined __sgi || defined __ICC |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
195 static double |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
196 compute_minus_zero (void) |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
197 { |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
198 return -DBL_MIN * DBL_MIN; |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
199 } |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
200 # define minus_zero compute_minus_zero () |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
201 #else |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
202 double minus_zero = -0.0; |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
203 #endif |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
204 |
9 | 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. */ | |
207 double | |
1691 | 208 strtod (const char *nptr, char **endptr) |
9 | 209 { |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
210 bool negative = false; |
9 | 211 |
212 /* The number so far. */ | |
213 double num; | |
214 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
215 const char *s = nptr; |
13545
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
216 const char *end; |
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
217 char *endbuf; |
13655
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
218 int saved_errno; |
9 | 219 |
220 /* Eat whitespace. */ | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
221 while (locale_isspace (*s)) |
9 | 222 ++s; |
223 | |
224 /* Get the sign. */ | |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
225 negative = *s == '-'; |
9 | 226 if (*s == '-' || *s == '+') |
227 ++s; | |
228 | |
13655
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
229 saved_errno = errno; |
13545
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
230 num = underlying_strtod (s, &endbuf); |
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
231 end = endbuf; |
9 | 232 |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
233 if (c_isdigit (s[*s == '.'])) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
234 { |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
235 /* If a hex float was converted incorrectly, do it ourselves. |
13507
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
236 If the string starts with "0x" but does not contain digits, |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
237 consume the "0" ourselves. If a hex float is followed by a |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
238 'p' but no exponent, then adjust the end pointer. */ |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
239 if (*s == '0' && c_tolower (s[1]) == 'x') |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
240 { |
13507
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
241 if (! c_isxdigit (s[2 + (s[2] == '.')])) |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
242 end = s + 1; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
243 else if (end <= s + 2) |
13545
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
244 { |
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
245 num = parse_number (s + 2, 16, 2, 4, 'p', &endbuf); |
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
246 end = endbuf; |
1531149632e8
strtod: fix const diagnostic
Paul Eggert <eggert@cs.ucla.edu>
parents:
13507
diff
changeset
|
247 } |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
248 else |
13507
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
249 { |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
250 const char *p = s + 2; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
251 while (p < end && c_tolower (*p) != 'p') |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
252 p++; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
253 if (p < end && ! c_isdigit (p[1 + (p[1] == '-' || p[1] == '+')])) |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
254 end = p; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
255 } |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
256 } |
13655
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
257 else |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
258 { |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
259 /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
260 underlying strtod on a copy of the original string |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
261 truncated to avoid the bug. */ |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
262 const char *e = s + 1; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
263 while (e < end && c_tolower (*e) != 'e') |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
264 e++; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
265 if (e < end && ! c_isdigit (e[1 + (e[1] == '-' || e[1] == '+')])) |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
266 { |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
267 char *dup = strdup (s); |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
268 errno = saved_errno; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
269 if (!dup) |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
270 { |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
271 /* Not really our day, is it. Rounding errors are |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
272 better than outright failure. */ |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
273 num = parse_number (s, 10, 10, 1, 'e', &endbuf); |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
274 } |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
275 else |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
276 { |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
277 dup[e - s] = '\0'; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
278 num = underlying_strtod (dup, &endbuf); |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
279 saved_errno = errno; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
280 free (dup); |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
281 errno = saved_errno; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
282 } |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
283 end = e; |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
284 } |
d813f5516d9a
strtod: work around IRIX 6.5 bug
Eric Blake <eblake@redhat.com>
parents:
13545
diff
changeset
|
285 } |
9822 | 286 |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
287 s = end; |
9822 | 288 } |
289 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
290 /* Check for infinities and NaNs. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
291 else if (c_tolower (*s) == 'i' |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
292 && c_tolower (s[1]) == 'n' |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
293 && c_tolower (s[2]) == 'f') |
9822 | 294 { |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
295 s += 3; |
9822 | 296 if (c_tolower (*s) == 'i' |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9833
diff
changeset
|
297 && c_tolower (s[1]) == 'n' |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
298 && c_tolower (s[2]) == 'i' |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
299 && c_tolower (s[3]) == 't' |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
300 && c_tolower (s[4]) == 'y') |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
301 s += 5; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
302 num = HUGE_VAL; |
14145
a94d9a7b4aaa
strtod: Restore errno when successfully parsing Infinity or NaN.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
303 errno = saved_errno; |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
304 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
305 else if (c_tolower (*s) == 'n' |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
306 && c_tolower (s[1]) == 'a' |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
307 && c_tolower (s[2]) == 'n') |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
308 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
309 s += 3; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
310 if (*s == '(') |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9833
diff
changeset
|
311 { |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
312 const char *p = s + 1; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
313 while (c_isalnum (*p)) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
314 p++; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
315 if (*p == ')') |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
316 s = p + 1; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9833
diff
changeset
|
317 } |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
318 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
319 /* If the underlying implementation misparsed the NaN, assume |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
320 its result is incorrect, and return a NaN. Normally it's |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
321 better to use the underlying implementation's result, since a |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
322 nice implementation populates the bits of the NaN according |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
323 to interpreting n-char-sequence as a hexadecimal number. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
324 if (s != end) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
325 num = NAN; |
14145
a94d9a7b4aaa
strtod: Restore errno when successfully parsing Infinity or NaN.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
326 errno = saved_errno; |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
327 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
328 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
329 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
330 /* No conversion could be performed. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
331 errno = EINVAL; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
332 s = nptr; |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
333 } |
9 | 334 |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
335 if (endptr != NULL) |
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
336 *endptr = (char *) s; |
13837
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
337 /* Special case -0.0, since at least ICC miscompiles negation. We |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
338 can't use copysign(), as that drags in -lm on some platforms. */ |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
339 if (!num && negative) |
d7a891a411c1
strtod: work around icc bug
Eric Blake <eblake@redhat.com>
parents:
13655
diff
changeset
|
340 return minus_zero; |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
341 return negative ? -num : num; |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
342 } |
9 | 343 |
17333
201622d6d12f
strtod: support coreutils better
Paul Eggert <eggert@cs.ucla.edu>
parents:
17249
diff
changeset
|
344 /* The underlying strtod implementation. This must be defined |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
345 after strtod because it #undefs strtod. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
346 static double |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
347 underlying_strtod (const char *nptr, char **endptr) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
348 { |
17333
201622d6d12f
strtod: support coreutils better
Paul Eggert <eggert@cs.ucla.edu>
parents:
17249
diff
changeset
|
349 #undef strtod |
201622d6d12f
strtod: support coreutils better
Paul Eggert <eggert@cs.ucla.edu>
parents:
17249
diff
changeset
|
350 return strtod (nptr, endptr); |
9 | 351 } |