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.
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.
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. */
38 *----------------------------------------------------------------------
42 * This procedure converts a floating-point number from an ASCII
43 * decimal representation to internal double-precision format.
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.
55 *----------------------------------------------------------------------
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).
72 char **endPtr
; /* If non-NULL, store terminating character's
75 int sign
, expSign
= 0;
76 double fraction
, dblExp
, *d
;
77 register const char *p
;
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
89 int mantSize
; /* Number of digits in mantissa. */
90 int decPt
; /* Number of mantissa digits BEFORE decimal
92 const char *pExp
; /* Temporarily holds location of exponent
96 * Strip off leading blanks and check for a sign.
100 while (isspace(*p
)) {
114 * Count the number of digits in the mantissa (including the decimal
115 * point), and also locate the decimal point.
119 for (mantSize
= 0; ; mantSize
+= 1)
123 if ((c
!= '.') || (decPt
>= 0)) {
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.
143 mantSize
-= 1; /* One of the digits was the point. */
146 fracExp
= decPt
- 18;
149 fracExp
= decPt
- mantSize
;
158 for ( ; mantSize
> 9; mantSize
-= 1)
166 frac1
= 10*frac1
+ (c
- '0');
169 for (; mantSize
> 0; mantSize
-= 1)
177 frac2
= 10*frac2
+ (c
- '0');
179 fraction
= (1.0e9
* frac1
) + frac2
;
183 * Skim off the exponent.
187 if ((*p
== 'E') || (*p
== 'e')) {
198 while (isdigit(*p
)) {
199 exp
= exp
* 10 + (*p
- '0');
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
222 if (exp
> maxExponent
) {
227 for (d
= powersOf10
; exp
!= 0; exp
>>= 1, d
+= 1) {
239 if (endPtr
!= NULL
) {
240 *endPtr
= (char *) p
;