30200
|
1 /*
|
|
2 * Copyright (c) 1988-1993 The Regents of the University of California.
|
|
3 * Copyright (c) 1994 Sun Microsystems, Inc.
|
|
4 *
|
|
5 * Permission to use, copy, modify, and distribute this
|
|
6 * software and its documentation for any purpose and without
|
|
7 * fee is hereby granted, provided that the above copyright
|
|
8 * notice appear in all copies. The University of California
|
|
9 * makes no representations about the suitability of this
|
|
10 * software for any purpose. It is provided "as is" without
|
|
11 * express or implied warranty.
|
|
12 *
|
|
13 */
|
|
14
|
|
15 #include <stdlib.h>
|
|
16 #include <ctype.h>
|
|
17 #include <errno.h>
|
|
18
|
|
19 static int maxExponent = 511; /* Largest possible base 10 exponent. Any
|
|
20 * exponent larger than this will already
|
|
21 * produce underflow or overflow, so there's
|
|
22 * no need to worry about additional digits.
|
|
23 */
|
|
24
|
|
25 static double powersOf10[] = { /* Table giving binary powers of 10. Entry */
|
|
26 10., /* is 10^2^i. Used to convert decimal */
|
|
27 100., /* exponents into floating-point numbers. */
|
|
28 1.0e4,
|
|
29 1.0e8,
|
|
30 1.0e16,
|
|
31 1.0e32,
|
|
32 1.0e64,
|
|
33 1.0e128,
|
|
34 1.0e256
|
|
35 };
|
|
36
|
|
37 /*
|
|
38 *----------------------------------------------------------------------
|
|
39 *
|
|
40 * strtod --
|
|
41 *
|
|
42 * This procedure converts a floating-point number from an ASCII
|
|
43 * decimal representation to internal double-precision format.
|
|
44 *
|
|
45 * Results:
|
|
46 * The return value is the double-precision floating-point
|
|
47 * representation of the characters in string. If endPtr isn't
|
|
48 * NULL, then *endPtr is filled in with the address of the
|
|
49 * next character after the last one that was part of the
|
|
50 * floating-point number.
|
|
51 *
|
|
52 * Side effects:
|
|
53 * None.
|
|
54 *
|
|
55 *----------------------------------------------------------------------
|
|
56 */
|
|
57
|
|
58 double
|
|
59 ass_strtod(string, endPtr)
|
|
60 const char *string; /* A decimal ASCII floating-point number,
|
|
61 * optionally preceded by white space.
|
|
62 * Must have form "-I.FE-X", where I is the
|
|
63 * integer part of the mantissa, F is the
|
|
64 * fractional part of the mantissa, and X
|
|
65 * is the exponent. Either of the signs
|
|
66 * may be "+", "-", or omitted. Either I
|
|
67 * or F may be omitted, or both. The decimal
|
|
68 * point isn't necessary unless F is present.
|
|
69 * The "E" may actually be an "e". E and X
|
|
70 * may both be omitted (but not just one).
|
|
71 */
|
|
72 char **endPtr; /* If non-NULL, store terminating character's
|
|
73 * address here. */
|
|
74 {
|
|
75 int sign, expSign = 0;
|
|
76 double fraction, dblExp, *d;
|
|
77 register const char *p;
|
|
78 register int c;
|
|
79 int exp = 0; /* Exponent read from "EX" field. */
|
|
80 int fracExp = 0; /* Exponent that derives from the fractional
|
|
81 * part. Under normal circumstatnces, it is
|
|
82 * the negative of the number of digits in F.
|
|
83 * However, if I is very long, the last digits
|
|
84 * of I get dropped (otherwise a long I with a
|
|
85 * large negative exponent could cause an
|
|
86 * unnecessary overflow on I alone). In this
|
|
87 * case, fracExp is incremented one for each
|
|
88 * dropped digit. */
|
|
89 int mantSize; /* Number of digits in mantissa. */
|
|
90 int decPt; /* Number of mantissa digits BEFORE decimal
|
|
91 * point. */
|
|
92 const char *pExp; /* Temporarily holds location of exponent
|
|
93 * in string. */
|
|
94
|
|
95 /*
|
|
96 * Strip off leading blanks and check for a sign.
|
|
97 */
|
|
98
|
|
99 p = string;
|
|
100 while (isspace(*p)) {
|
|
101 p += 1;
|
|
102 }
|
|
103 if (*p == '-') {
|
|
104 sign = 1;
|
|
105 p += 1;
|
|
106 } else {
|
|
107 if (*p == '+') {
|
|
108 p += 1;
|
|
109 }
|
|
110 sign = 0;
|
|
111 }
|
|
112
|
|
113 /*
|
|
114 * Count the number of digits in the mantissa (including the decimal
|
|
115 * point), and also locate the decimal point.
|
|
116 */
|
|
117
|
|
118 decPt = -1;
|
|
119 for (mantSize = 0; ; mantSize += 1)
|
|
120 {
|
|
121 c = *p;
|
|
122 if (!isdigit(c)) {
|
|
123 if ((c != '.') || (decPt >= 0)) {
|
|
124 break;
|
|
125 }
|
|
126 decPt = mantSize;
|
|
127 }
|
|
128 p += 1;
|
|
129 }
|
|
130
|
|
131 /*
|
|
132 * Now suck up the digits in the mantissa. Use two integers to
|
|
133 * collect 9 digits each (this is faster than using floating-point).
|
|
134 * If the mantissa has more than 18 digits, ignore the extras, since
|
|
135 * they can't affect the value anyway.
|
|
136 */
|
|
137
|
|
138 pExp = p;
|
|
139 p -= mantSize;
|
|
140 if (decPt < 0) {
|
|
141 decPt = mantSize;
|
|
142 } else {
|
|
143 mantSize -= 1; /* One of the digits was the point. */
|
|
144 }
|
|
145 if (mantSize > 18) {
|
|
146 fracExp = decPt - 18;
|
|
147 mantSize = 18;
|
|
148 } else {
|
|
149 fracExp = decPt - mantSize;
|
|
150 }
|
|
151 if (mantSize == 0) {
|
|
152 fraction = 0.0;
|
|
153 p = string;
|
|
154 goto done;
|
|
155 } else {
|
|
156 int frac1, frac2;
|
|
157 frac1 = 0;
|
|
158 for ( ; mantSize > 9; mantSize -= 1)
|
|
159 {
|
|
160 c = *p;
|
|
161 p += 1;
|
|
162 if (c == '.') {
|
|
163 c = *p;
|
|
164 p += 1;
|
|
165 }
|
|
166 frac1 = 10*frac1 + (c - '0');
|
|
167 }
|
|
168 frac2 = 0;
|
|
169 for (; mantSize > 0; mantSize -= 1)
|
|
170 {
|
|
171 c = *p;
|
|
172 p += 1;
|
|
173 if (c == '.') {
|
|
174 c = *p;
|
|
175 p += 1;
|
|
176 }
|
|
177 frac2 = 10*frac2 + (c - '0');
|
|
178 }
|
|
179 fraction = (1.0e9 * frac1) + frac2;
|
|
180 }
|
|
181
|
|
182 /*
|
|
183 * Skim off the exponent.
|
|
184 */
|
|
185
|
|
186 p = pExp;
|
|
187 if ((*p == 'E') || (*p == 'e')) {
|
|
188 p += 1;
|
|
189 if (*p == '-') {
|
|
190 expSign = 1;
|
|
191 p += 1;
|
|
192 } else {
|
|
193 if (*p == '+') {
|
|
194 p += 1;
|
|
195 }
|
|
196 expSign = 0;
|
|
197 }
|
|
198 while (isdigit(*p)) {
|
|
199 exp = exp * 10 + (*p - '0');
|
|
200 p += 1;
|
|
201 }
|
|
202 }
|
|
203 if (expSign) {
|
|
204 exp = fracExp - exp;
|
|
205 } else {
|
|
206 exp = fracExp + exp;
|
|
207 }
|
|
208
|
|
209 /*
|
|
210 * Generate a floating-point number that represents the exponent.
|
|
211 * Do this by processing the exponent one bit at a time to combine
|
|
212 * many powers of 2 of 10. Then combine the exponent with the
|
|
213 * fraction.
|
|
214 */
|
|
215
|
|
216 if (exp < 0) {
|
|
217 expSign = 1;
|
|
218 exp = -exp;
|
|
219 } else {
|
|
220 expSign = 0;
|
|
221 }
|
|
222 if (exp > maxExponent) {
|
|
223 exp = maxExponent;
|
|
224 errno = ERANGE;
|
|
225 }
|
|
226 dblExp = 1.0;
|
|
227 for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
|
|
228 if (exp & 01) {
|
|
229 dblExp *= *d;
|
|
230 }
|
|
231 }
|
|
232 if (expSign) {
|
|
233 fraction /= dblExp;
|
|
234 } else {
|
|
235 fraction *= dblExp;
|
|
236 }
|
|
237
|
|
238 done:
|
|
239 if (endPtr != NULL) {
|
|
240 *endPtr = (char *) p;
|
|
241 }
|
|
242
|
|
243 if (sign) {
|
|
244 return -fraction;
|
|
245 }
|
|
246 return fraction;
|
|
247 }
|