home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / libc / gen / atof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-05-05  |  1.2 KB  |  99 lines

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