1: /*
   2:  * Copyright (c) 1987 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(LIBC_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)atof.c	2.2 (Berkeley) 1/22/87";
   9: #endif LIBC_SCCS and not lint
  10: 
  11: /*
  12:  *	C library - ascii to floating
  13:  */
  14: 
  15: #include <math.h>
  16: #include <ctype.h>
  17: 
  18: double
  19: atof(p)
  20: register char *p;
  21: {
  22:     register c;
  23:     double fl, flexp, exp5;
  24:     double big = 72057594037927936.;  /*2^56*/
  25:     double ldexp();
  26:     int nd;
  27:     register eexp, exp, neg, negexp, bexp;
  28: 
  29:     neg = 1;
  30:     while((c = *p++) == ' ')
  31:         ;
  32:     if (c == '-')
  33:         neg = -1;
  34:     else if (c=='+')
  35:         ;
  36:     else
  37:         --p;
  38: 
  39:     exp = 0;
  40:     fl = 0;
  41:     nd = 0;
  42:     while ((c = *p++), isdigit(c)) {
  43:         if (fl<big)
  44:             fl = 10*fl + (c-'0');
  45:         else
  46:             exp++;
  47:         nd++;
  48:     }
  49: 
  50:     if (c == '.') {
  51:         while ((c = *p++), isdigit(c)) {
  52:             if (fl<big) {
  53:                 fl = 10*fl + (c-'0');
  54:                 exp--;
  55:             }
  56:         nd++;
  57:         }
  58:     }
  59: 
  60:     negexp = 1;
  61:     eexp = 0;
  62:     if ((c == 'E') || (c == 'e')) {
  63:         if ((c= *p++) == '+')
  64:             ;
  65:         else if (c=='-')
  66:             negexp = -1;
  67:         else
  68:             --p;
  69: 
  70:         while ((c = *p++), isdigit(c)) {
  71:             eexp = 10*eexp+(c-'0');
  72:         }
  73:         if (negexp<0)
  74:             eexp = -eexp;
  75:         exp = exp + eexp;
  76:     }
  77: 
  78:     negexp = 1;
  79:     if (exp<0) {
  80:         negexp = -1;
  81:         exp = -exp;
  82:     }
  83: 
  84: 
  85:     if((nd+exp*negexp) < -LOGHUGE){
  86:         fl = 0;
  87:         exp = 0;
  88:     }
  89:     flexp = 1;
  90:     exp5 = 5;
  91:     bexp = exp;
  92:     for (;;) {
  93:         if (exp&01)
  94:             flexp *= exp5;
  95:         exp >>= 1;
  96:         if (exp==0)
  97:             break;
  98:         exp5 *= exp5;
  99:     }
 100:     if (negexp<0)
 101:         fl /= flexp;
 102:     else
 103:         fl *= flexp;
 104:     fl = ldexp(fl, negexp*bexp);
 105:     if (neg<0)
 106:         fl = -fl;
 107:     return(fl);
 108: }

Defined functions

atof defined in line 18; used 88 times

Defined variables

sccsid defined in line 8; never used
Last modified: 1987-01-25
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1298
Valid CSS Valid XHTML 1.0 Strict