2 * Copyright (c) 1988-1993 The Regents of the University of California.
3 * Copyright (c) 1994 Sun Microsystems, Inc.
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.
20 static 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.
27 static 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. */
40 *----------------------------------------------------------------------
44 * This procedure converts a floating-point number from an ASCII
45 * decimal representation to internal double-precision format.
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.
57 *----------------------------------------------------------------------
61 ass_strtod(string, endPtr)
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).
74 char **endPtr; /* If non-NULL, store terminating character's
77 int sign, expSign = 0;
78 double fraction, dblExp, *d;
79 register const char *p;
81 int exp = 0; /* Exponent read from "EX" field. */
82 int fracExp = 0; /* Exponent that derives from the fractional
83 * part. Under normal circumstatnces, it is
84 * the negative of the number of digits in F.
85 * However, if I is very long, the last digits
86 * of I get dropped (otherwise a long I with a
87 * large negative exponent could cause an
88 * unnecessary overflow on I alone). In this
89 * case, fracExp is incremented one for each
91 int mantSize; /* Number of digits in mantissa. */
92 int decPt; /* Number of mantissa digits BEFORE decimal
94 const char *pExp; /* Temporarily holds location of exponent
98 * Strip off leading blanks and check for a sign.
102 while (isspace(*p)) {
116 * Count the number of digits in the mantissa (including the decimal
117 * point), and also locate the decimal point.
121 for (mantSize = 0; ; mantSize += 1)
125 if ((c != '.') || (decPt >= 0)) {
134 * Now suck up the digits in the mantissa. Use two integers to
135 * collect 9 digits each (this is faster than using floating-point).
136 * If the mantissa has more than 18 digits, ignore the extras, since
137 * they can't affect the value anyway.
145 mantSize -= 1; /* One of the digits was the point. */
148 fracExp = decPt - 18;
151 fracExp = decPt - mantSize;
160 for ( ; mantSize > 9; mantSize -= 1)
168 frac1 = 10*frac1 + (c - '0');
171 for (; mantSize > 0; mantSize -= 1)
179 frac2 = 10*frac2 + (c - '0');
181 fraction = (1.0e9 * frac1) + frac2;
185 * Skim off the exponent.
189 if ((*p == 'E') || (*p == 'e')) {
200 while (isdigit(*p)) {
201 exp = exp * 10 + (*p - '0');
212 * Generate a floating-point number that represents the exponent.
213 * Do this by processing the exponent one bit at a time to combine
214 * many powers of 2 of 10. Then combine the exponent with the
224 if (exp > maxExponent) {
229 for (d = (double *) powersOf10; exp != 0; exp >>= 1, d += 1) {
241 if (endPtr != NULL) {
242 *endPtr = (char *) p;