os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/strtod.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/strtod.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,258 @@
     1.4 +/* 
     1.5 + * strtod.c --
     1.6 + *
     1.7 + *	Source code for the "strtod" library procedure.
     1.8 + *
     1.9 + * Copyright (c) 1988-1993 The Regents of the University of California.
    1.10 + * Copyright (c) 1994 Sun Microsystems, Inc.
    1.11 + *
    1.12 + * See the file "license.terms" for information on usage and redistribution
    1.13 + * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    1.14 + *
    1.15 + * RCS: @(#) $Id: strtod.c,v 1.6 2002/02/25 14:26:12 dgp Exp $
    1.16 + */
    1.17 +
    1.18 +#include "tclInt.h"
    1.19 +#include "tclPort.h"
    1.20 +#include <ctype.h>
    1.21 +
    1.22 +#ifndef TRUE
    1.23 +#define TRUE 1
    1.24 +#define FALSE 0
    1.25 +#endif
    1.26 +#ifndef NULL
    1.27 +#define NULL 0
    1.28 +#endif
    1.29 +
    1.30 +static int maxExponent = 511;	/* Largest possible base 10 exponent.  Any
    1.31 +				 * exponent larger than this will already
    1.32 +				 * produce underflow or overflow, so there's
    1.33 +				 * no need to worry about additional digits.
    1.34 +				 */
    1.35 +static double powersOf10[] = {	/* Table giving binary powers of 10.  Entry */
    1.36 +    10.,			/* is 10^2^i.  Used to convert decimal */
    1.37 +    100.,			/* exponents into floating-point numbers. */
    1.38 +    1.0e4,
    1.39 +    1.0e8,
    1.40 +    1.0e16,
    1.41 +    1.0e32,
    1.42 +    1.0e64,
    1.43 +    1.0e128,
    1.44 +    1.0e256
    1.45 +};
    1.46 +
    1.47 +/*
    1.48 + *----------------------------------------------------------------------
    1.49 + *
    1.50 + * strtod --
    1.51 + *
    1.52 + *	This procedure converts a floating-point number from an ASCII
    1.53 + *	decimal representation to internal double-precision format.
    1.54 + *
    1.55 + * Results:
    1.56 + *	The return value is the double-precision floating-point
    1.57 + *	representation of the characters in string.  If endPtr isn't
    1.58 + *	NULL, then *endPtr is filled in with the address of the
    1.59 + *	next character after the last one that was part of the
    1.60 + *	floating-point number.
    1.61 + *
    1.62 + * Side effects:
    1.63 + *	None.
    1.64 + *
    1.65 + *----------------------------------------------------------------------
    1.66 + */
    1.67 +
    1.68 +double
    1.69 +strtod(string, endPtr)
    1.70 +    CONST char *string;		/* A decimal ASCII floating-point number,
    1.71 +				 * optionally preceded by white space.
    1.72 +				 * Must have form "-I.FE-X", where I is the
    1.73 +				 * integer part of the mantissa, F is the
    1.74 +				 * fractional part of the mantissa, and X
    1.75 +				 * is the exponent.  Either of the signs
    1.76 +				 * may be "+", "-", or omitted.  Either I
    1.77 +				 * or F may be omitted, or both.  The decimal
    1.78 +				 * point isn't necessary unless F is present.
    1.79 +				 * The "E" may actually be an "e".  E and X
    1.80 +				 * may both be omitted (but not just one).
    1.81 +				 */
    1.82 +    char **endPtr;		/* If non-NULL, store terminating character's
    1.83 +				 * address here. */
    1.84 +{
    1.85 +    int sign, expSign = FALSE;
    1.86 +    double fraction, dblExp, *d;
    1.87 +    register CONST char *p;
    1.88 +    register int c;
    1.89 +    int exp = 0;		/* Exponent read from "EX" field. */
    1.90 +    int fracExp = 0;		/* Exponent that derives from the fractional
    1.91 +				 * part.  Under normal circumstatnces, it is
    1.92 +				 * the negative of the number of digits in F.
    1.93 +				 * However, if I is very long, the last digits
    1.94 +				 * of I get dropped (otherwise a long I with a
    1.95 +				 * large negative exponent could cause an
    1.96 +				 * unnecessary overflow on I alone).  In this
    1.97 +				 * case, fracExp is incremented one for each
    1.98 +				 * dropped digit. */
    1.99 +    int mantSize;		/* Number of digits in mantissa. */
   1.100 +    int decPt;			/* Number of mantissa digits BEFORE decimal
   1.101 +				 * point. */
   1.102 +    CONST char *pExp;		/* Temporarily holds location of exponent
   1.103 +				 * in string. */
   1.104 +
   1.105 +    /*
   1.106 +     * Strip off leading blanks and check for a sign.
   1.107 +     */
   1.108 +
   1.109 +    p = string;
   1.110 +    while (isspace(UCHAR(*p))) {
   1.111 +	p += 1;
   1.112 +    }
   1.113 +    if (*p == '-') {
   1.114 +	sign = TRUE;
   1.115 +	p += 1;
   1.116 +    } else {
   1.117 +	if (*p == '+') {
   1.118 +	    p += 1;
   1.119 +	}
   1.120 +	sign = FALSE;
   1.121 +    }
   1.122 +
   1.123 +    /*
   1.124 +     * Count the number of digits in the mantissa (including the decimal
   1.125 +     * point), and also locate the decimal point.
   1.126 +     */
   1.127 +
   1.128 +    decPt = -1;
   1.129 +    for (mantSize = 0; ; mantSize += 1)
   1.130 +    {
   1.131 +	c = *p;
   1.132 +	if (!isdigit(c)) {
   1.133 +	    if ((c != '.') || (decPt >= 0)) {
   1.134 +		break;
   1.135 +	    }
   1.136 +	    decPt = mantSize;
   1.137 +	}
   1.138 +	p += 1;
   1.139 +    }
   1.140 +
   1.141 +    /*
   1.142 +     * Now suck up the digits in the mantissa.  Use two integers to
   1.143 +     * collect 9 digits each (this is faster than using floating-point).
   1.144 +     * If the mantissa has more than 18 digits, ignore the extras, since
   1.145 +     * they can't affect the value anyway.
   1.146 +     */
   1.147 +    
   1.148 +    pExp  = p;
   1.149 +    p -= mantSize;
   1.150 +    if (decPt < 0) {
   1.151 +	decPt = mantSize;
   1.152 +    } else {
   1.153 +	mantSize -= 1;			/* One of the digits was the point. */
   1.154 +    }
   1.155 +    if (mantSize > 18) {
   1.156 +	fracExp = decPt - 18;
   1.157 +	mantSize = 18;
   1.158 +    } else {
   1.159 +	fracExp = decPt - mantSize;
   1.160 +    }
   1.161 +    if (mantSize == 0) {
   1.162 +	fraction = 0.0;
   1.163 +	p = string;
   1.164 +	goto done;
   1.165 +    } else {
   1.166 +	int frac1, frac2;
   1.167 +	frac1 = 0;
   1.168 +	for ( ; mantSize > 9; mantSize -= 1)
   1.169 +	{
   1.170 +	    c = *p;
   1.171 +	    p += 1;
   1.172 +	    if (c == '.') {
   1.173 +		c = *p;
   1.174 +		p += 1;
   1.175 +	    }
   1.176 +	    frac1 = 10*frac1 + (c - '0');
   1.177 +	}
   1.178 +	frac2 = 0;
   1.179 +	for (; mantSize > 0; mantSize -= 1)
   1.180 +	{
   1.181 +	    c = *p;
   1.182 +	    p += 1;
   1.183 +	    if (c == '.') {
   1.184 +		c = *p;
   1.185 +		p += 1;
   1.186 +	    }
   1.187 +	    frac2 = 10*frac2 + (c - '0');
   1.188 +	}
   1.189 +	fraction = (1.0e9 * frac1) + frac2;
   1.190 +    }
   1.191 +
   1.192 +    /*
   1.193 +     * Skim off the exponent.
   1.194 +     */
   1.195 +
   1.196 +    p = pExp;
   1.197 +    if ((*p == 'E') || (*p == 'e')) {
   1.198 +	p += 1;
   1.199 +	if (*p == '-') {
   1.200 +	    expSign = TRUE;
   1.201 +	    p += 1;
   1.202 +	} else {
   1.203 +	    if (*p == '+') {
   1.204 +		p += 1;
   1.205 +	    }
   1.206 +	    expSign = FALSE;
   1.207 +	}
   1.208 +	if (!isdigit(UCHAR(*p))) {
   1.209 +	    p = pExp;
   1.210 +	    goto done;
   1.211 +	}
   1.212 +	while (isdigit(UCHAR(*p))) {
   1.213 +	    exp = exp * 10 + (*p - '0');
   1.214 +	    p += 1;
   1.215 +	}
   1.216 +    }
   1.217 +    if (expSign) {
   1.218 +	exp = fracExp - exp;
   1.219 +    } else {
   1.220 +	exp = fracExp + exp;
   1.221 +    }
   1.222 +
   1.223 +    /*
   1.224 +     * Generate a floating-point number that represents the exponent.
   1.225 +     * Do this by processing the exponent one bit at a time to combine
   1.226 +     * many powers of 2 of 10. Then combine the exponent with the
   1.227 +     * fraction.
   1.228 +     */
   1.229 +    
   1.230 +    if (exp < 0) {
   1.231 +	expSign = TRUE;
   1.232 +	exp = -exp;
   1.233 +    } else {
   1.234 +	expSign = FALSE;
   1.235 +    }
   1.236 +    if (exp > maxExponent) {
   1.237 +	exp = maxExponent;
   1.238 +	errno = ERANGE;
   1.239 +    }
   1.240 +    dblExp = 1.0;
   1.241 +    for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
   1.242 +	if (exp & 01) {
   1.243 +	    dblExp *= *d;
   1.244 +	}
   1.245 +    }
   1.246 +    if (expSign) {
   1.247 +	fraction /= dblExp;
   1.248 +    } else {
   1.249 +	fraction *= dblExp;
   1.250 +    }
   1.251 +
   1.252 +done:
   1.253 +    if (endPtr != NULL) {
   1.254 +	*endPtr = (char *) p;
   1.255 +    }
   1.256 +
   1.257 +    if (sign) {
   1.258 +	return -fraction;
   1.259 +    }
   1.260 +    return fraction;
   1.261 +}