annotate lib/strtod.c @ 12421:e8d2c6fc33ad

Use spaces for indentation, not tabs.
author Bruno Haible <bruno@clisp.org>
date Thu, 10 Dec 2009 20:28:30 +0100
parents 48eddbd8edd5
children f7842310a565
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
1 /* Copyright (C) 1991, 1992, 1997, 1999, 2003, 2006, 2008 Free
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
2 Software Foundation, Inc.
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
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
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8
311
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
9 This program is distributed in the hope that it will be useful,
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
12 GNU General Public License for more details.
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
13
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
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
7900f3bbd673 remove ansidecl junk
Jim Meyering <jim@meyering.net>
parents: 308
diff changeset
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
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
18
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
19 #include <stdlib.h>
373
Jim Meyering <jim@meyering.net>
parents: 311
diff changeset
20
311
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
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>
311
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
24 #include <math.h>
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
25 #include <stdbool.h>
4691
ce37d22a271f Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 2807
diff changeset
26 #include <string.h>
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
28 #include "c-ctype.h"
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
29
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
30 /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31 character after the last one used in the number is put in *ENDPTR. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32 double
1691
0eedcf045615 Protoize.
Jim Meyering <jim@meyering.net>
parents: 1042
diff changeset
33 strtod (const char *nptr, char **endptr)
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
34 {
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
35 const unsigned char *s;
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
36 bool negative = false;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
37
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
38 /* The number so far. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
39 double num;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
40
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
41 bool got_dot; /* Found a decimal point. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
42 bool got_digit; /* Seen any digits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
43 bool hex = false; /* Look for hex float exponent. */
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
44
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
45 /* The exponent of the number. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
46 long int exponent;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
47
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
48 if (nptr == NULL)
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
49 {
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 errno = EINVAL;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
51 goto noconv;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
52 }
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53
9833
48eddbd8edd5 strtod touchups.
Eric Blake <ebb9@byu.net>
parents: 9822
diff changeset
54 /* Use unsigned char for the ctype routines. */
48eddbd8edd5 strtod touchups.
Eric Blake <ebb9@byu.net>
parents: 9822
diff changeset
55 s = (unsigned char *) nptr;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
56
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
57 /* Eat whitespace. */
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
58 while (isspace (*s))
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
59 ++s;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
60
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
61 /* Get the sign. */
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
62 negative = *s == '-';
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
63 if (*s == '-' || *s == '+')
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
64 ++s;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
66 num = 0.0;
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
67 got_dot = false;
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
68 got_digit = false;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69 exponent = 0;
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
70
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
71 /* Check for hex float. */
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
72 if (*s == '0' && c_tolower (s[1]) == 'x'
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
73 && (c_isxdigit (s[2]) || ('.' == s[2] && c_isxdigit (s[3]))))
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
74 {
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
75 hex = true;
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
76 s += 2;
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
77 for (;; ++s)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
78 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
79 if (c_isxdigit (*s))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
80 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
81 got_digit = true;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
82
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
83 /* Make sure that multiplication by 16 will not overflow. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
84 if (num > DBL_MAX / 16)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
85 /* The value of the digit doesn't matter, since we have already
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
86 gotten as many digits as can be represented in a `double'.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
87 This doesn't necessarily mean the result will overflow.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
88 The exponent may reduce it to within range.
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
89
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
90 We just need to record that there was another
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
91 digit so that we can multiply by 16 later. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
92 ++exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
93 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
94 num = ((num * 16.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
95 + (c_tolower (*s) - (c_isdigit (*s) ? '0' : 'a' - 10)));
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
96
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
97 /* Keep track of the number of digits after the decimal point.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
98 If we just divided by 16 here, we would lose precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
99 if (got_dot)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
100 --exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
101 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
102 else if (!got_dot && *s == '.')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
103 /* Record that we have found the decimal point. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
104 got_dot = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
105 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
106 /* Any other character terminates the number. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
107 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
108 }
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
109 }
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
110
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
111 /* Not a hex float. */
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
112 else
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
113 {
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
114 for (;; ++s)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
115 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
116 if (c_isdigit (*s))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
117 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
118 got_digit = true;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
119
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
120 /* Make sure that multiplication by 10 will not overflow. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
121 if (num > DBL_MAX * 0.1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
122 /* The value of the digit doesn't matter, since we have already
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
123 gotten as many digits as can be represented in a `double'.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
124 This doesn't necessarily mean the result will overflow.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
125 The exponent may reduce it to within range.
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
126
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
127 We just need to record that there was another
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
128 digit so that we can multiply by 10 later. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
129 ++exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
130 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
131 num = (num * 10.0) + (*s - '0');
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
132
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
133 /* Keep track of the number of digits after the decimal point.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
134 If we just divided by 10 here, we would lose precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
135 if (got_dot)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
136 --exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
137 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
138 else if (!got_dot && *s == '.')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
139 /* Record that we have found the decimal point. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
140 got_dot = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
141 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
142 /* Any other character terminates the number. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
143 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
144 }
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
145 }
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
146
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
147 if (!got_digit)
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
148 {
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
149 /* Check for infinities and NaNs. */
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
150 if (c_tolower (*s) == 'i'
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
151 && c_tolower (s[1]) == 'n'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
152 && c_tolower (s[2]) == 'f')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
153 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
154 s += 3;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
155 num = HUGE_VAL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
156 if (c_tolower (*s) == 'i'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
157 && c_tolower (s[1]) == 'n'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
158 && c_tolower (s[2]) == 'i'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
159 && c_tolower (s[3]) == 't'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
160 && c_tolower (s[4]) == 'y')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
161 s += 5;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
162 goto valid;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
163 }
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
164 #ifdef NAN
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
165 else if (c_tolower (*s) == 'n'
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
166 && c_tolower (s[1]) == 'a'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
167 && c_tolower (s[2]) == 'n')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
168 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
169 s += 3;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
170 num = NAN;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
171 /* Since nan(<n-char-sequence>) is implementation-defined,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
172 we define it by ignoring <n-char-sequence>. A nicer
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
173 implementation would populate the bits of the NaN
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
174 according to interpreting n-char-sequence as a
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
175 hexadecimal number, but the result is still a NaN. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
176 if (*s == '(')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
177 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
178 const unsigned char *p = s + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
179 while (c_isalnum (*p))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
180 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
181 if (*p == ')')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
182 s = p + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
183 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
184 goto valid;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
185 }
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
186 #endif
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
187 goto noconv;
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
188 }
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
189
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
190 if (c_tolower (*s) == (hex ? 'p' : 'e') && !isspace (s[1]))
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
191 {
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
192 /* Get the exponent specified after the `e' or `E'. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
193 int save = errno;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
194 char *end;
9833
48eddbd8edd5 strtod touchups.
Eric Blake <ebb9@byu.net>
parents: 9822
diff changeset
195 long int value;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
196
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
197 errno = 0;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
198 ++s;
9833
48eddbd8edd5 strtod touchups.
Eric Blake <ebb9@byu.net>
parents: 9822
diff changeset
199 value = strtol ((char *) s, &end, 10);
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
200 if (errno == ERANGE && num)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
201 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
202 /* The exponent overflowed a `long int'. It is probably a safe
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
203 assumption that an exponent that cannot be represented by
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
204 a `long int' exceeds the limits of a `double'. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
205 if (endptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
206 *endptr = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
207 if (value < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
208 goto underflow;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
209 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
210 goto overflow;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
211 }
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
212 else if (end == (char *) s)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
213 /* There was no exponent. Reset END to point to
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
214 the 'e' or 'E', so *ENDPTR will be set there. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
215 end = (char *) s - 1;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
216 errno = save;
9833
48eddbd8edd5 strtod touchups.
Eric Blake <ebb9@byu.net>
parents: 9822
diff changeset
217 s = (unsigned char *) end;
48eddbd8edd5 strtod touchups.
Eric Blake <ebb9@byu.net>
parents: 9822
diff changeset
218 exponent += value;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
219 }
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
220
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
221 if (num == 0.0)
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
222 goto valid;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
223
9822
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
224 if (hex)
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
225 {
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
226 /* ldexp takes care of range errors. */
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
227 num = ldexp (num, exponent);
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
228 goto valid;
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
229 }
fb687649f177 Add hex float support.
Eric Blake <ebb9@byu.net>
parents: 9821
diff changeset
230
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
231 /* Multiply NUM by 10 to the EXPONENT power,
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
232 checking for overflow and underflow. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
233
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
234 if (exponent < 0)
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
235 {
311
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
236 if (num < DBL_MIN * pow (10.0, (double) -exponent))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
237 goto underflow;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
238 }
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
239 else if (exponent > 0)
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
240 {
311
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
241 if (num > DBL_MAX * pow (10.0, (double) -exponent))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9833
diff changeset
242 goto overflow;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
243 }
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
244
311
6980a90dddc9 merge with 1.10n5
Jim Meyering <jim@meyering.net>
parents: 310
diff changeset
245 num *= pow (10.0, (double) exponent);
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
246
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
247 valid:
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
248 if (endptr != NULL)
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
249 *endptr = (char *) s;
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
250 return negative ? -num : num;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
251
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
252 overflow:
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
253 /* Return an overflow error. */
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
254 if (endptr != NULL)
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
255 *endptr = (char *) s;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
256 errno = ERANGE;
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
257 return negative ? -HUGE_VAL : HUGE_VAL;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
258
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
259 underflow:
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
260 /* Return an underflow error. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
261 if (endptr != NULL)
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
262 *endptr = (char *) s;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
263 errno = ERANGE;
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
264 return negative ? -0.0 : 0.0;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
265
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
266 noconv:
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
267 /* There was no number. */
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
268 if (endptr != NULL)
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
269 *endptr = (char *) nptr;
9821
18bab9955c43 Document various strtod bugs, with some fixes.
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
270 errno = EINVAL;
9
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
271 return 0.0;
f8dce34b5ab0 Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
272 }