home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tcl7.0b1 / compat / strtod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-19  |  6.6 KB  |  270 lines

  1. /* 
  2.  * strtod.c --
  3.  *
  4.  *    Source code for the "strtod" library procedure.
  5.  *
  6.  * Copyright (c) 1988-1993 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Permission is hereby granted, without written agreement and without
  10.  * license or royalty fees, to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose, provided that the
  12.  * above copyright notice and the following two paragraphs appear in
  13.  * all copies of this software.
  14.  * 
  15.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  16.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  17.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  18.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19.  *
  20.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  21.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  22.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  23.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  24.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25.  */
  26.  
  27. #ifndef lint
  28. static char rcsid[] = "$Header: /user6/ouster/tcl/compat/RCS/strtod.c,v 1.4 93/03/19 15:25:47 ouster Exp $ SPRITE (Berkeley)";
  29. #endif /* not lint */
  30.  
  31. #include <tcl.h>
  32. #include <stdlib.h>
  33. #include <ctype.h>
  34.  
  35. #ifndef TRUE
  36. #define TRUE 1
  37. #define FALSE 0
  38. #endif
  39. #ifndef NULL
  40. #define NULL 0
  41. #endif
  42.  
  43. static int maxExponent = 511;    /* Largest possible base 10 exponent.  Any
  44.                  * exponent larger than this will already
  45.                  * produce underflow or overflow, so there's
  46.                  * no need to worry about additional digits.
  47.                  */
  48. static double powersOf10[] = {    /* Table giving binary powers of 10.  Entry */
  49.     10.,            /* is 10^2^i.  Used to convert decimal */
  50.     100.,            /* exponents into floating-point numbers. */
  51.     1.0e4,
  52.     1.0e8,
  53.     1.0e16,
  54.     1.0e32,
  55.     1.0e64,
  56.     1.0e128,
  57.     1.0e256
  58. };
  59.  
  60. /*
  61.  *----------------------------------------------------------------------
  62.  *
  63.  * strtod --
  64.  *
  65.  *    This procedure converts a floating-point number from an ASCII
  66.  *    decimal representation to internal double-precision format.
  67.  *
  68.  * Results:
  69.  *    The return value is the double-precision floating-point
  70.  *    representation of the characters in string.  If endPtr isn't
  71.  *    NULL, then *endPtr is filled in with the address of the
  72.  *    next character after the last one that was part of the
  73.  *    floating-point number.
  74.  *
  75.  * Side effects:
  76.  *    None.
  77.  *
  78.  *----------------------------------------------------------------------
  79.  */
  80.  
  81. double
  82. strtod(string, endPtr)
  83.     CONST char *string;        /* A decimal ASCII floating-point number,
  84.                  * optionally preceded by white space.
  85.                  * Must have form "-I.FE-X", where I is the
  86.                  * integer part of the mantissa, F is the
  87.                  * fractional part of the mantissa, and X
  88.                  * is the exponent.  Either of the signs
  89.                  * may be "+", "-", or omitted.  Either I
  90.                  * or F may be omitted, or both.  The decimal
  91.                  * point isn't necessary unless F is present.
  92.                  * The "E" may actually be an "e".  E and X
  93.                  * may both be omitted (but not just one).
  94.                  */
  95.     char **endPtr;        /* If non-NULL, store terminating character's
  96.                  * address here. */
  97. {
  98.     int sign, expSign = FALSE;
  99.     double fraction, dblExp, *d;
  100.     register CONST char *p;
  101.     register int c;
  102.     int exp = 0;        /* Exponent read from "EX" field. */
  103.     int fracExp = 0;        /* Exponent that derives from the fractional
  104.                  * part.  Under normal circumstatnces, it is
  105.                  * the negative of the number of digits in F.
  106.                  * However, if I is very long, the last digits
  107.                  * of I get dropped (otherwise a long I with a
  108.                  * large negative exponent could cause an
  109.                  * unnecessary overflow on I alone).  In this
  110.                  * case, fracExp is incremented one for each
  111.                  * dropped digit. */
  112.     int mantSize;        /* Number of digits in mantissa. */
  113.     int decPt;            /* Number of mantissa digits BEFORE decimal
  114.                  * point. */
  115.     CONST char *pExp;        /* Temporarily holds location of exponent
  116.                  * in string. */
  117.  
  118.     /*
  119.      * Strip off leading blanks and check for a sign.
  120.      */
  121.  
  122.     p = string;
  123.     while (isspace(*p)) {
  124.     p += 1;
  125.     }
  126.     if (*p == '-') {
  127.     sign = TRUE;
  128.     p += 1;
  129.     } else {
  130.     if (*p == '+') {
  131.         p += 1;
  132.     }
  133.     sign = FALSE;
  134.     }
  135.  
  136.     /*
  137.      * Count the number of digits in the mantissa (including the decimal
  138.      * point), and also locate the decimal point.
  139.      */
  140.  
  141.     decPt = -1;
  142.     for (mantSize = 0; ; mantSize += 1)
  143.     {
  144.     c = *p;
  145.     if (!isdigit(c)) {
  146.         if ((c != '.') || (decPt >= 0)) {
  147.         break;
  148.         }
  149.         decPt = mantSize;
  150.     }
  151.     p += 1;
  152.     }
  153.  
  154.     /*
  155.      * Now suck up the digits in the mantissa.  Use two integers to
  156.      * collect 9 digits each (this is faster than using floating-point).
  157.      * If the mantissa has more than 18 digits, ignore the extras, since
  158.      * they can't affect the value anyway.
  159.      */
  160.     
  161.     pExp  = p;
  162.     p -= mantSize;
  163.     if (decPt < 0) {
  164.     decPt = mantSize;
  165.     } else {
  166.     mantSize -= 1;            /* One of the digits was the point. */
  167.     }
  168.     if (mantSize > 18) {
  169.     fracExp = decPt - 18;
  170.     mantSize = 18;
  171.     } else {
  172.     fracExp = decPt - mantSize;
  173.     }
  174.     if (mantSize == 0) {
  175.     fraction = 0.0;
  176.     p = string;
  177.     goto done;
  178.     } else {
  179.     int frac1, frac2;
  180.     frac1 = 0;
  181.     for ( ; mantSize > 9; mantSize -= 1)
  182.     {
  183.         c = *p;
  184.         p += 1;
  185.         if (c == '.') {
  186.         c = *p;
  187.         p += 1;
  188.         }
  189.         frac1 = 10*frac1 + (c - '0');
  190.     }
  191.     frac2 = 0;
  192.     for (; mantSize > 0; mantSize -= 1)
  193.     {
  194.         c = *p;
  195.         p += 1;
  196.         if (c == '.') {
  197.         c = *p;
  198.         p += 1;
  199.         }
  200.         frac2 = 10*frac2 + (c - '0');
  201.     }
  202.     fraction = (1.0e9 * frac1) + frac2;
  203.     }
  204.  
  205.     /*
  206.      * Skim off the exponent.
  207.      */
  208.  
  209.     p = pExp;
  210.     if ((*p == 'E') || (*p == 'e')) {
  211.     p += 1;
  212.     if (*p == '-') {
  213.         expSign = TRUE;
  214.         p += 1;
  215.     } else {
  216.         if (*p == '+') {
  217.         p += 1;
  218.         }
  219.         expSign = FALSE;
  220.     }
  221.     while (isdigit(*p)) {
  222.         exp = exp * 10 + (*p - '0');
  223.         p += 1;
  224.     }
  225.     }
  226.     if (expSign) {
  227.     exp = fracExp - exp;
  228.     } else {
  229.     exp = fracExp + exp;
  230.     }
  231.  
  232.     /*
  233.      * Generate a floating-point number that represents the exponent.
  234.      * Do this by processing the exponent one bit at a time to combine
  235.      * many powers of 2 of 10. Then combine the exponent with the
  236.      * fraction.
  237.      */
  238.     
  239.     if (exp < 0) {
  240.     expSign = TRUE;
  241.     exp = -exp;
  242.     } else {
  243.     expSign = FALSE;
  244.     }
  245.     if (exp > maxExponent) {
  246.     exp = maxExponent;
  247.     }
  248.     dblExp = 1.0;
  249.     for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
  250.     if (exp & 01) {
  251.         dblExp *= *d;
  252.     }
  253.     }
  254.     if (expSign) {
  255.     fraction /= dblExp;
  256.     } else {
  257.     fraction *= dblExp;
  258.     }
  259.  
  260. done:
  261.     if (endPtr != NULL) {
  262.     *endPtr = (char *) p;
  263.     }
  264.  
  265.     if (sign) {
  266.     return -fraction;
  267.     }
  268.     return fraction;
  269. }
  270.