1: # include   <sccs.h>
   2: 
   3: SCCSID(@(#)atof.c	8.1	12/31/84)
   4: 
   5: /*
   6: **  ATOF -- ASCII TO FLOATING CONVERSION
   7: **
   8: **	Converts the string 'str' to floating point and stores the
   9: **	result into the cell pointed to by 'val'.
  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: **	Parameters:
  19: **		str -- string to convert.
  20: **		val -- pointer to place to put the result (which
  21: **			must be type double).
  22: **
  23: **	Returns:
  24: **		zero -- ok.
  25: **		-1 -- syntax error.
  26: **		+1 -- overflow (someday).
  27: **
  28: **	Side Effects:
  29: **		clobbers *val.
  30: */
  31: 
  32: atof(str, val)
  33: char    *str;
  34: double  *val;
  35: {
  36:     register char   *p;
  37:     double      v;
  38:     extern double   pow();
  39:     double      fact;
  40:     int     minus;
  41:     register char   c;
  42:     int     expon;
  43:     register int    gotmant;
  44: 
  45:     v = 0.0;
  46:     p = str;
  47:     minus = 0;
  48: 
  49:     /* skip leading blanks */
  50:     while (c = *p)
  51:     {
  52:         if (c != ' ')
  53:             break;
  54:         p++;
  55:     }
  56: 
  57:     /* handle possible sign */
  58:     switch (c)
  59:     {
  60: 
  61:       case '-':
  62:         minus++;
  63: 
  64:       case '+':
  65:         p++;
  66: 
  67:     }
  68: 
  69:     /* skip blanks after sign */
  70:     while (c = *p)
  71:     {
  72:         if (c != ' ')
  73:             break;
  74:         p++;
  75:     }
  76: 
  77:     /* start collecting the number to the decimal point */
  78:     gotmant = 0;
  79:     for (;;)
  80:     {
  81:         c = *p;
  82:         if (c < '0' || c > '9')
  83:             break;
  84:         v = v * 10.0 + (c - '0');
  85:         gotmant++;
  86:         p++;
  87:     }
  88: 
  89:     /* check for fractional part */
  90:     if (c == '.')
  91:     {
  92:         fact = 1.0;
  93:         for (;;)
  94:         {
  95:             c = *++p;
  96:             if (c < '0' || c > '9')
  97:                 break;
  98:             fact *= 0.1;
  99:             v += (c - '0') * fact;
 100:             gotmant++;
 101:         }
 102:     }
 103: 
 104:     /* skip blanks before possible exponent */
 105:     while (c = *p)
 106:     {
 107:         if (c != ' ')
 108:             break;
 109:         p++;
 110:     }
 111: 
 112:     /* test for exponent */
 113:     if (c == 'e' || c == 'E')
 114:     {
 115:         p++;
 116:         expon = atoi(p);
 117:         if (!gotmant)
 118:             v = 1.0;
 119:         fact = expon;
 120:         v *= pow(10.0, fact);
 121:     }
 122:     else
 123:     {
 124:         /* if no exponent, then nothing */
 125:         if (c != 0)
 126:             return (-1);
 127:     }
 128: 
 129:     /* store the result and exit */
 130:     if (minus)
 131:         v = -v;
 132:     *val = v;
 133:     return (0);
 134: }

Defined functions

atof defined in line 3; never used
Last modified: 1986-04-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 892
Valid CSS Valid XHTML 1.0 Strict