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.
33 static int maxExponent
= 511; /* Largest possible base 10 exponent. Any
34 * exponent larger than this will already
35 * produce underflow or overflow, so there's
36 * no need to worry about additional digits.
38 static double powersOf10
[] = { /* Table giving binary powers of 10. Entry */
39 10., /* is 10^2^i. Used to convert decimal */
40 100., /* exponents into floating-point numbers. */
51 *----------------------------------------------------------------------
55 * This procedure converts a floating-point number from an ASCII
56 * decimal representation to internal double-precision format.
59 * The return value is the double-precision floating-point
60 * representation of the characters in string. If endPtr isn't
61 * NULL, then *endPtr is filled in with the address of the
62 * next character after the last one that was part of the
63 * floating-point number.
68 *----------------------------------------------------------------------
71 double litehtml::t_strtod(const char* string
, char** endPtr
)
73 int sign
, expSign
= FALSE
;
74 double fraction
, dblExp
, *d
;
77 int exp
= 0; /* Exponent read from "EX" field. */
78 int fracExp
= 0; /* Exponent that derives from the fractional
79 * part. Under normal circumstatnces, it is
80 * the negative of the number of digits in F.
81 * However, if I is very long, the last digits
82 * of I get dropped (otherwise a long I with a
83 * large negative exponent could cause an
84 * unnecessary overflow on I alone). In this
85 * case, fracExp is incremented one for each
87 int mantSize
; /* Number of digits in mantissa. */
88 int decPt
; /* Number of mantissa digits BEFORE decimal
90 const char *pExp
; /* Temporarily holds location of exponent
94 * Strip off leading blanks and check for a sign.
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)
126 if ((c
!= '.') || (decPt
>= 0))
136 * Now suck up the digits in the mantissa. Use two integers to
137 * collect 9 digits each (this is faster than using floating-point).
138 * If the mantissa has more than 18 digits, ignore the extras, since
139 * they can't affect the value anyway.
149 mantSize
-= 1; /* One of the digits was the point. */
153 fracExp
= decPt
- 18;
157 fracExp
= decPt
- mantSize
;
168 for ( ; mantSize
> 9; mantSize
-= 1)
177 frac1
= 10*frac1
+ (c
- '0');
180 for (; mantSize
> 0; mantSize
-= 1)
189 frac2
= 10*frac2
+ (c
- '0');
191 fraction
= (1.0e9
* frac1
) + frac2
;
195 * Skim off the exponent.
199 if ((*p
== 'E') || (*p
== 'e'))
216 exp
= exp
* 10 + (*p
- '0');
229 * Generate a floating-point number that represents the exponent.
230 * Do this by processing the exponent one bit at a time to combine
231 * many powers of 2 of 10. Then combine the exponent with the
243 if (exp
> maxExponent
)
249 for (d
= powersOf10
; exp
!= 0; exp
>>= 1, d
+= 1)
265 if (endPtr
!= nullptr)
267 *endPtr
= (char *) p
;