[comp.sys.atari.st] Enhancements for GCC library for Atari ST

piet@cs.ruu.nl (Piet van Oostrum) (07/11/89)

The Atari GCC compiler does not properly deal with floating point numbers
with exponents (like 1E10). This is becuase the standard library's atof
does not like them. I just put it in there. Here is the new routine:


/* jrd's kludgy atof */
/* It is much less kludgy now -- Piet van Oostrum (piet@cs.ruu.nl) */
/* July 11, 1989 */

#include <ctype.h>

#define maybe_negate(num,flg) ((flg) ? -(num) : (num))

double atof(str)
char * str;
{
  int neg = 0;
  double accum, factor, ten = 10.0;
  char c;

  while (isspace(*str)) str++;

  if (*str == '-') 
	{
	neg = 1;
	str++;
	}
  else if (*str == '+') str++;

  accum = 0;
  for ( ; ((c = *str++) && (isdigit(c))) ; )
	{
	accum = accum * ten + (c - '0');
	}
  if (c == '.')
      {
	  double tenth = 0.1;
	  factor = tenth;

	  for ( ; ((c = *str++) && (isdigit(c))) ; )
	      {
		  accum = accum + ((c - '0') * factor);
		  factor = factor * tenth;
	      }
      }
  if (c == 'e' || c == 'E')
      {
	  register int negexp = 0, exp = 0;
	  while (isspace(*str)) str++ ;

	  if (*str == '-') 
	      {
		  negexp = 1;
		  str++;
	      }
	  else if (*str == '+') str++;

	  for ( ; ((c = *str++) && (isdigit(c))) ; )
	      {
		  exp = exp * 10 + c - '0';
	      }

	  factor = 1.0;

	  for ( ; exp >= 10; exp -= 10)
	      factor *= 10000000000.0;

	  for ( ; exp > 0; exp--)
		  factor *= ten;

	  if (negexp)
	      accum /= factor;
	  else
	      accum *= factor;
      }

  return(maybe_negate(accum,neg));
}
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)