4 * Source code for the "strtod" library procedure.
6 * Copyright (c) 1988-1993 The Regents of the University of California.
7 * Copyright (c) 1994 Sun Microsystems, Inc.
9 * Permission to use, copy, modify, and distribute this
10 * software and its documentation for any purpose and without
11 * fee is hereby granted, provided that the above copyright
12 * notice appear in all copies. The University of California
13 * makes no representations about the suitability of this
14 * software for any purpose. It is provided "as is" without
15 * express or implied warranty.
26 # define const __const__
40 static int maxExponent
= 511; /* Largest possible base 10 exponent. Any
41 * exponent larger than this will already
42 * produce underflow or overflow, so there's
43 * no need to worry about additional digits.
45 static double powersOf10
[] = { /* Table giving binary powers of 10. Entry */
46 10., /* is 10^2^i. Used to convert decimal */
47 100., /* exponents into floating-point numbers. */
58 *----------------------------------------------------------------------
62 * This procedure converts a floating-point number from an ASCII
63 * decimal representation to internal double-precision format.
66 * The return value is the double-precision floating-point
67 * representation of the characters in string. If endPtr isn't
68 * NULL, then *endPtr is filled in with the address of the
69 * next character after the last one that was part of the
70 * floating-point number.
75 *----------------------------------------------------------------------
80 const char *string
, /* A decimal ASCII floating-point number,
81 * optionally preceded by white space.
82 * Must have form "-I.FE-X", where I is the
83 * integer part of the mantissa, F is the
84 * fractional part of the mantissa, and X
85 * is the exponent. Either of the signs
86 * may be "+", "-", or omitted. Either I
87 * or F may be omitted, or both. The decimal
88 * point isn't necessary unless F is present.
89 * The "E" may actually be an "e". E and X
90 * may both be omitted (but not just one).
92 char **endPtr
/* If non-NULL, store terminating character's
96 int sign
, expSign
= FALSE
;
97 double fraction
, dblExp
, *d
;
98 register const char *p
;
100 int exp
= 0; /* Exponent read from "EX" field. */
101 int fracExp
= 0; /* Exponent that derives from the fractional
102 * part. Under normal circumstatnces, it is
103 * the negative of the number of digits in F.
104 * However, if I is very long, the last digits
105 * of I get dropped (otherwise a long I with a
106 * large negative exponent could cause an
107 * unnecessary overflow on I alone). In this
108 * case, fracExp is incremented one for each
110 int mantSize
; /* Number of digits in mantissa. */
111 int decPt
; /* Number of mantissa digits BEFORE decimal
113 const char *pExp
; /* Temporarily holds location of exponent
117 * Strip off leading blanks and check for a sign.
121 while (isspace(*p
)) {
135 * Count the number of digits in the mantissa (including the decimal
136 * point), and also locate the decimal point.
140 for (mantSize
= 0; ; mantSize
+= 1)
144 if ((c
!= '.') || (decPt
>= 0)) {
153 * Now suck up the digits in the mantissa. Use two integers to
154 * collect 9 digits each (this is faster than using floating-point).
155 * If the mantissa has more than 18 digits, ignore the extras, since
156 * they can't affect the value anyway.
164 mantSize
-= 1; /* One of the digits was the point. */
167 fracExp
= decPt
- 18;
170 fracExp
= decPt
- mantSize
;
179 for ( ; mantSize
> 9; mantSize
-= 1)
187 frac1
= 10*frac1
+ (c
- '0');
190 for (; mantSize
> 0; mantSize
-= 1)
198 frac2
= 10*frac2
+ (c
- '0');
200 fraction
= (1.0e9
* frac1
) + frac2
;
204 * Skim off the exponent.
208 if ((*p
== 'E') || (*p
== 'e')) {
219 while (isdigit(*p
)) {
220 exp
= exp
* 10 + (*p
- '0');
231 * Generate a floating-point number that represents the exponent.
232 * Do this by processing the exponent one bit at a time to combine
233 * many powers of 2 of 10. Then combine the exponent with the
243 if (exp
> maxExponent
) {
248 for (d
= powersOf10
; exp
!= 0; exp
>>= 1, d
+= 1) {
260 if (endPtr
!= NULL
) {
261 *endPtr
= (char *) p
;