1: /*	@(#)atof.c	2.1	SCCS id keyword	*/
   2: /*
   3: 	C library - ascii to floating
   4: */
   5: 
   6: #include <math.h>
   7: #include <ctype.h>
   8: 
   9: double
  10: atof(p)
  11: register char *p;
  12: {
  13:     register c;
  14:     double fl, flexp, exp5;
  15:     double big = 72057594037927936.;  /*2^56*/
  16:     double ldexp();
  17:     int nd;
  18:     register eexp, exp, neg, negexp, bexp;
  19: 
  20:     neg = 1;
  21:     while((c = *p++) == ' ')
  22:         ;
  23:     if (c == '-')
  24:         neg = -1;
  25:     else if (c=='+')
  26:         ;
  27:     else
  28:         --p;
  29: 
  30:     exp = 0;
  31:     fl = 0;
  32:     nd = 0;
  33:     while ((c = *p++), isdigit(c)) {
  34:         if (fl<big)
  35:             fl = 10*fl + (c-'0');
  36:         else
  37:             exp++;
  38:         nd++;
  39:     }
  40: 
  41:     if (c == '.') {
  42:         while ((c = *p++), isdigit(c)) {
  43:             if (fl<big) {
  44:                 fl = 10*fl + (c-'0');
  45:                 exp--;
  46:             }
  47:         nd++;
  48:         }
  49:     }
  50: 
  51:     negexp = 1;
  52:     eexp = 0;
  53:     if ((c == 'E') || (c == 'e')) {
  54:         if ((c= *p++) == '+')
  55:             ;
  56:         else if (c=='-')
  57:             negexp = -1;
  58:         else
  59:             --p;
  60: 
  61:         while ((c = *p++), isdigit(c)) {
  62:             eexp = 10*eexp+(c-'0');
  63:         }
  64:         if (negexp<0)
  65:             eexp = -eexp;
  66:         exp = exp + eexp;
  67:     }
  68: 
  69:     negexp = 1;
  70:     if (exp<0) {
  71:         negexp = -1;
  72:         exp = -exp;
  73:     }
  74: 
  75: 
  76:     if((nd+exp*negexp) < -LOGHUGE){
  77:         fl = 0;
  78:         exp = 0;
  79:     }
  80:     flexp = 1;
  81:     exp5 = 5;
  82:     bexp = exp;
  83:     for (;;) {
  84:         if (exp&01)
  85:             flexp *= exp5;
  86:         exp >>= 1;
  87:         if (exp==0)
  88:             break;
  89:         exp5 *= exp5;
  90:     }
  91:     if (negexp<0)
  92:         fl /= flexp;
  93:     else
  94:         fl *= flexp;
  95:     fl = ldexp(fl, negexp*bexp);
  96:     if (neg<0)
  97:         fl = -fl;
  98:     return(fl);
  99: }
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 728
Valid CSS Valid XHTML 1.0 Strict