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