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.
19
* Taken from http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8/missing/strtod.c
30
# define const __const__
44
static int maxExponent = 511; /* Largest possible base 10 exponent. Any
45
* exponent larger than this will already
46
* produce underflow or overflow, so there's
47
* no need to worry about additional digits.
49
static double powersOf10[] = { /* Table giving binary powers of 10. Entry */
50
10., /* is 10^2^i. Used to convert decimal */
51
100., /* exponents into floating-point numbers. */
62
*----------------------------------------------------------------------
66
* This procedure converts a floating-point number from an ASCII
67
* decimal representation to internal double-precision format.
70
* The return value is the double-precision floating-point
71
* representation of the characters in string. If endPtr isn't
72
* NULL, then *endPtr is filled in with the address of the
73
* next character after the last one that was part of the
74
* floating-point number.
79
*----------------------------------------------------------------------
83
strtod(string, endPtr)
84
const char *string; /* A decimal ASCII floating-point number,
85
* optionally preceded by white space.
86
* Must have form "-I.FE-X", where I is the
87
* integer part of the mantissa, F is the
88
* fractional part of the mantissa, and X
89
* is the exponent. Either of the signs
90
* may be "+", "-", or omitted. Either I
91
* or F may be omitted, or both. The decimal
92
* point isn't necessary unless F is present.
93
* The "E" may actually be an "e". E and X
94
* may both be omitted (but not just one).
96
char **endPtr; /* If non-NULL, store terminating character's
99
int sign, expSign = FALSE;
100
double fraction, dblExp, *d;
101
register const char *p;
103
int exp = 0; /* Exponent read from "EX" field. */
104
int fracExp = 0; /* Exponent that derives from the fractional
105
* part. Under normal circumstatnces, it is
106
* the negative of the number of digits in F.
107
* However, if I is very long, the last digits
108
* of I get dropped (otherwise a long I with a
109
* large negative exponent could cause an
110
* unnecessary overflow on I alone). In this
111
* case, fracExp is incremented one for each
113
int mantSize; /* Number of digits in mantissa. */
114
int decPt; /* Number of mantissa digits BEFORE decimal
116
const char *pExp; /* Temporarily holds location of exponent
120
* Strip off leading blanks and check for a sign.
124
while (isspace(*p)) {
138
* Count the number of digits in the mantissa (including the decimal
139
* point), and also locate the decimal point.
143
for (mantSize = 0; ; mantSize += 1)
147
if ((c != '.') || (decPt >= 0)) {
156
* Now suck up the digits in the mantissa. Use two integers to
157
* collect 9 digits each (this is faster than using floating-point).
158
* If the mantissa has more than 18 digits, ignore the extras, since
159
* they can't affect the value anyway.
167
mantSize -= 1; /* One of the digits was the point. */
170
fracExp = decPt - 18;
173
fracExp = decPt - mantSize;
182
for ( ; mantSize > 9; mantSize -= 1)
190
frac1 = 10*frac1 + (c - '0');
193
for (; mantSize > 0; mantSize -= 1)
201
frac2 = 10*frac2 + (c - '0');
203
fraction = (1.0e9 * frac1) + frac2;
207
* Skim off the exponent.
211
if ((*p == 'E') || (*p == 'e')) {
222
while (isdigit(*p)) {
223
exp = exp * 10 + (*p - '0');
234
* Generate a floating-point number that represents the exponent.
235
* Do this by processing the exponent one bit at a time to combine
236
* many powers of 2 of 10. Then combine the exponent with the
246
if (exp > maxExponent) {
251
for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
263
if (endPtr != NULL) {
264
*endPtr = (char *) p;
274
* Implementations added for emscripten.
276
// XXX add real support for long double
278
strtold(const char* nptr, char **endptr)
280
return (long double) strtod(nptr, endptr);
283
// use stdtod to handle strtof
285
strtof(const char* nptr, char **endptr)
287
return (float) strtod(nptr, endptr);
290
// XXX no locale support yet
292
strtod_l(const char* nptr, char **endptr, locale_t loc)
294
return strtod(nptr, endptr);
297
strtold_l(const char* nptr, char **endptr, locale_t loc)
299
return strtold(nptr, endptr);
302
double atof(const char* str)
304
return strtod(str, (char **) NULL);