Mercurial > gnulib
annotate lib/strtod.c @ 13507:be8a41115d44
strtod: next round of AIX fixes
* lib/strtod.c (strtod): Work around AIX bug of parsing p with no
exponent.
* tests/test-strtod.c (main): Enhance tests.
* doc/posix-functions/strtod.texi (strtod): Document next bug.
Reported by Rainer Tammer.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Fri, 30 Jul 2010 16:01:41 -0600 |
parents | 4ccc551d5cb6 |
children | 1531149632e8 |
rev | line source |
---|---|
12559
c2cbabec01dd
update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents:
12422
diff
changeset
|
1 /* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2010 Free Software |
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> |
9 | 27 |
9822 | 28 #include "c-ctype.h" |
29 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
30 #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
|
31 #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
|
32 #endif |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
33 #ifndef HAVE_RAW_DECL_STRTOD |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
34 #define HAVE_RAW_DECL_STRTOD 0 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
35 #endif |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
36 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
37 /* 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
|
38 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
|
39 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
|
40 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
|
41 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
42 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
|
43 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
|
44 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
45 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
46 #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
|
47 #define ldexp dummy_ldexp |
13464 | 48 /* A dummy definition that will never be invoked. */ |
49 static double ldexp (double x _GL_UNUSED, int exponent _GL_UNUSED) | |
50 { | |
51 abort (); | |
52 return 0.0; | |
53 } | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
54 #endif |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
55 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
56 /* 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
|
57 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
|
58 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
|
59 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
|
60 { |
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 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
|
62 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
|
63 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
|
64 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
|
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 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
|
67 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
68 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
|
69 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
|
70 else |
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 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
|
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 (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
|
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 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
|
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 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
|
79 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
80 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
|
81 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
|
82 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
83 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
|
84 break; |
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 } |
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 else |
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 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
|
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 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
|
93 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
94 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
|
95 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
|
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 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
|
98 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
99 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
|
100 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
|
101 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
102 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
103 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
|
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 } |
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 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
|
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 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
111 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
112 /* 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 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
|
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 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
|
129 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
137 /* 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
|
138 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
|
139 continue; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
140 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
141 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
142 /* 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
|
143 break; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
144 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
145 /* 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
|
146 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
|
147 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
|
148 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
149 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
150 /* The value of the digit doesn't matter, since we have already |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
151 gotten as many digits as can be represented in a `double'. |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
152 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
|
153 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
|
154 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
155 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
|
156 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
|
157 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
|
158 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
159 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
160 /* 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
|
161 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
|
162 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
|
163 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
|
164 } |
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 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
|
167 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
168 /* 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
|
169 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
|
170 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
|
171 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
|
172 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
|
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 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
|
175 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
176 /* 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
|
177 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
|
178 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
|
179 exponent = |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
180 (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
|
181 ? (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
|
182 : (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
|
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 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
185 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
186 *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
|
187 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
|
188 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
189 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
190 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
|
191 |
9 | 192 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the |
193 character after the last one used in the number is put in *ENDPTR. */ | |
194 double | |
1691 | 195 strtod (const char *nptr, char **endptr) |
9 | 196 { |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
197 bool negative = false; |
9 | 198 |
199 /* The number so far. */ | |
200 double num; | |
201 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
202 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
|
203 char *end; |
9 | 204 |
205 /* 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
|
206 while (locale_isspace (*s)) |
9 | 207 ++s; |
208 | |
209 /* Get the sign. */ | |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
210 negative = *s == '-'; |
9 | 211 if (*s == '-' || *s == '+') |
212 ++s; | |
213 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
214 num = underlying_strtod (s, &end); |
9 | 215 |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
216 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
|
217 { |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
218 /* 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
|
219 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
|
220 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
|
221 '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
|
222 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
|
223 { |
13507
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
224 if (! c_isxdigit (s[2 + (s[2] == '.')])) |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
225 end = s + 1; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
226 else if (end <= s + 2) |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
227 num = parse_number (s + 2, 16, 2, 4, 'p', &end); |
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
228 else |
13507
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
229 { |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
230 const char *p = s + 2; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
231 while (p < end && c_tolower (*p) != 'p') |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
232 p++; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
233 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
|
234 end = p; |
be8a41115d44
strtod: next round of AIX fixes
Eric Blake <eblake@redhat.com>
parents:
13487
diff
changeset
|
235 } |
13487
4ccc551d5cb6
strtod: fix bug in replacement function on AIX
Eric Blake <eblake@redhat.com>
parents:
13464
diff
changeset
|
236 } |
9822 | 237 |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
238 s = end; |
9822 | 239 } |
240 | |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
241 /* 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
|
242 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
|
243 && 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
|
244 && c_tolower (s[2]) == 'f') |
9822 | 245 { |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
246 s += 3; |
9822 | 247 if (c_tolower (*s) == 'i' |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9833
diff
changeset
|
248 && 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
|
249 && 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
|
250 && 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
|
251 && 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
|
252 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
|
253 num = 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
|
254 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
255 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
|
256 && 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
|
257 && 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
|
258 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
259 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
|
260 if (*s == '(') |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9833
diff
changeset
|
261 { |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
262 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
|
263 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
|
264 p++; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
265 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
|
266 s = p + 1; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9833
diff
changeset
|
267 } |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
268 |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
269 /* 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 num = NAN; |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
276 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
277 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
278 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
279 /* 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
|
280 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
|
281 s = nptr; |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
282 } |
9 | 283 |
9821
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
284 if (endptr != NULL) |
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
285 *endptr = (char *) s; |
18bab9955c43
Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
286 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
|
287 } |
9 | 288 |
13456
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
289 /* The "underlying" strtod implementation. This must be defined |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
294 if (HAVE_RAW_DECL_STRTOD) |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
295 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
296 /* Prefer the native strtod if available. Usually it should |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
297 work and it should give more-accurate results than our |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
298 approximation. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
299 #undef strtod |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
300 return strtod (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
|
301 } |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
302 else |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
303 { |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
304 /* Approximate strtod well enough for this module. There's no |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
305 need to handle anything but finite unsigned decimal |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
306 numbers with nonnull ENDPTR. */ |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
307 return parse_number (nptr, 10, 10, 1, 'e', endptr); |
fd291325b6d5
strtod: make it more-accurate typically, and don't require libm
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
308 } |
9 | 309 } |