www.pudn.com > iccavr_lib.rar > atof.c, change:2002-02-05,size:1314b


/* Functions to perform various conversions to and from single-precision        */ 
/* floating point        2/ 8/00        E.M.Greene                              */ 
/* Adapted by rfm ImageCraft (see math2.c.save)                                 */ 
/* Modified (slightly) to accept expressions without decimal point, 7th Jan 2001 A.L.E.    */ 
 
#include <ctype.h> 
#include <math.h> 
 
float powi(int x, int y); 
 
float atof(char *s) 
 { 
 float v = 0.0, 
       scale = 0.1; 
 char  mneg = ' ', 
       eneg = ' '; 
 int   e = 0; 
	    
 while (isspace(*s)) 
  s++; 
 
 if (*s == '-') 
  mneg = *s++; 
 else 
  if (*s == '+') 
   s++; 
   
 while (isdigit(*s)) 
  v = 10.0 * v + *s++ - '0'; 
   
 if (*s == '.')  
  s++; 
    
 while(isdigit(*s))  
  { 
  v += (*s++ - '0') * scale; 
  scale /= 10.0; 
  } 
   
 if (toupper(*s) == 'E')  
  { 
  s++; 
  if (*s == '-') 
   eneg = *s++; 
  else  
   if (*s == '+') 
    s++; 
  while (isdigit(*s)) 
   e = 10 * e + *s++ - '0'; 
  if (eneg == '-') 
   v = v / powi(10,e); 
  else 
   v = v * powi(10,e); 
  } 
   
 if (mneg == '-') 
  v = -v; 
    
 return v; 
 } 
 
float powi(int x, int y) 
// Determines x-raised-to-the-power-of-y and returns the 
// result as a float 
 { 
 int d; 
 float p = 1; 
  
 for (d = 0; d < y; d++) 
  p *= x; 
   
 return p; 
 }