1: /*
   2: **  ASCII TO FLOATING CONVERSION
   3: **
   4: **	Converts the string 'str' to floating point and stores the
   5: **	result into the cell pointed to by 'val'.
   6: **
   7: **	Returns zero for ok, negative one for syntax error, and
   8: **	positive one for overflow.
   9: **	(actually, it doesn't check for overflow)
  10: **
  11: **	The syntax which it accepts is pretty much what you would
  12: **	expect.  Basically, it is:
  13: **		{<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
  14: **	where <exp> is "e" or "E" followed by an integer, <sp> is a
  15: **	space character, <digit> is zero through nine, [] is zero or
  16: **	one, and {} is zero or more.
  17: **
  18: **	History:
  19: **		2/12/79 (eric) -- fixed bug where '0.0e56' (zero
  20: **			mantissa with an exponent) reset the
  21: **			mantissa to one.
  22: */
  23: 
  24: atof(str, val)
  25: char    *str;
  26: double  *val;
  27: {
  28:     register char   *p;
  29:     double      v;
  30:     extern double   pow();
  31:     double      fact;
  32:     int     minus;
  33:     register char   c;
  34:     int     expon;
  35:     register int    gotmant;
  36: 
  37:     v = 0.0;
  38:     p = str;
  39:     minus = 0;
  40: 
  41:     /* skip leading blanks */
  42:     while (c = *p)
  43:     {
  44:         if (c != ' ')
  45:             break;
  46:         p++;
  47:     }
  48: 
  49:     /* handle possible sign */
  50:     switch (c)
  51:     {
  52: 
  53:       case '-':
  54:         minus++;
  55: 
  56:       case '+':
  57:         p++;
  58: 
  59:     }
  60: 
  61:     /* skip blanks after sign */
  62:     while (c = *p)
  63:     {
  64:         if (c != ' ')
  65:             break;
  66:         p++;
  67:     }
  68: 
  69:     /* start collecting the number to the decimal point */
  70:     gotmant = 0;
  71:     for (;;)
  72:     {
  73:         c = *p;
  74:         if (c < '0' || c > '9')
  75:             break;
  76:         v = v * 10.0 + (c - '0');
  77:         gotmant++;
  78:         p++;
  79:     }
  80: 
  81:     /* check for fractional part */
  82:     if (c == '.')
  83:     {
  84:         fact = 1.0;
  85:         for (;;)
  86:         {
  87:             c = *++p;
  88:             if (c < '0' || c > '9')
  89:                 break;
  90:             fact *= 0.1;
  91:             v += (c - '0') * fact;
  92:             gotmant++;
  93:         }
  94:     }
  95: 
  96:     /* skip blanks before possible exponent */
  97:     while (c = *p)
  98:     {
  99:         if (c != ' ')
 100:             break;
 101:         p++;
 102:     }
 103: 
 104:     /* test for exponent */
 105:     if (c == 'e' || c == 'E')
 106:     {
 107:         p++;
 108:         if (atoi(p, &expon))
 109:             return (-1);
 110:         if (!gotmant)
 111:             v = 1.0;
 112:         fact = expon;
 113:         v *= pow(10.0, fact);
 114:     }
 115:     else
 116:     {
 117:         /* if no exponent, then nothing */
 118:         if (c != 0)
 119:             return (-1);
 120:     }
 121: 
 122:     /* store the result and exit */
 123:     if (minus)
 124:         v = -v;
 125:     *val = v;
 126:     return (0);
 127: }
Last modified: 1995-02-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 730
Valid CSS Valid XHTML 1.0 Strict