home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / ffpstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  3.4 KB  |  138 lines

  1. /* ACE floating point to string conversion.
  2. ** Copyright (C) 1998 David Benn
  3. ** 
  4. ** This program is free software; you can redistribute it and/or
  5. ** modify it under the terms of the GNU General Public License
  6. ** as published by the Free Software Foundation; either version 2
  7. ** of the License, or (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18.    -- char *strsingle(float) --
  19.  
  20.    This function converts a single-precision (FFP) floating point
  21.    number into a string of fixed-point or exponential format.
  22.  
  23.    Uses fpa() to give a string of the form: +|-.nnnnnnnn+|-Enn
  24.  
  25.    Author: David J Benn
  26.      Date: 16th,18th February 1993
  27. */
  28.  
  29. static char     fnumbuf[40];    /* final buffer for transformed fnum */
  30.  
  31. /* external references */
  32. extern    long     MathBase;     /* FFP math library base */
  33. extern    long    decimal_places;    /* number of places to round to -- default=8 */
  34.  
  35. /* FFP math functions */
  36. long     SPTst();
  37. float    afp();
  38.  
  39. /* strsingle() */
  40. char     *strsingle(fnum)
  41. float     fnum;
  42. {
  43. static     char     fpabuf[15];
  44. static    char    ex_buf[3];
  45. char     *src,*dest,*tmp;
  46. long    ex,nonzero,i,j;
  47.  
  48.  /* convert fnum to ASCII format */
  49.  ex=fpa(fnum,fpabuf);
  50.  
  51.  /* round? (see _fix in ffp.s) */
  52.  if (decimal_places != 8) 
  53.  {
  54.   arnd(decimal_places,ex,fpabuf);
  55.   fnum=afp(fpabuf);
  56.  }
  57.  
  58.  src=fpabuf;
  59.  dest=fnumbuf;
  60.  
  61.  if (SPTst(fnum) == 0) 
  62.  { 
  63.   *dest++=' ';
  64.   *dest++='0';
  65.   *dest='\0';
  66.   return(fnumbuf); 
  67.  }
  68.  else
  69.  {
  70.   /* find last non-zero digit */
  71.   for (nonzero=9;src[nonzero]=='0';--nonzero);
  72.  
  73.   /* leading minus or space */
  74.   if (src[0] == '-') *dest++ = '-'; else *dest++ = ' ';
  75.  
  76.   /* if exponent is < 0 -> need leading zeros */
  77.   if (ex <= 0) 
  78.   {
  79.    if (ex >= -6)
  80.    {
  81.     /* fixed-point format */
  82.     *dest++='0'; 
  83.     *dest++='.';
  84.     for (i=1;i<=-ex;i++) *dest++='0';
  85.     for (i=2;i<=nonzero;i++) *dest++=src[i];
  86.    }
  87.    else
  88.    {
  89.     /* exponential format */
  90.     /* mantissa */
  91.     *dest++=src[2];  /* first digit */
  92.     *dest++='.';
  93.     if (nonzero > 2) 
  94.        for (i=3;i<=nonzero;i++) *dest++=src[i];
  95.     else
  96.        *dest++='0';  /* no fractional part after first digit */
  97.  
  98.     /* include exponent */
  99.     *dest++='E';
  100.     *dest++=src[11]; /* sign of exponent */
  101.     sprintf(ex_buf,"%ld",-ex+1); /* exponent */
  102.     *dest++=ex_buf[0];
  103.     *dest++=ex_buf[1];
  104.    }
  105.   }
  106.   else
  107.   /* id exponent is > 0 -> print digits until last non-zero digit */
  108.   if (ex <= 7)
  109.   { 
  110.    /* fixed-point format */
  111.    for (i=2;i<=ex+1;i++) *dest++=src[i];
  112.    if (i <= nonzero) *dest++='.';  /* if nonzero=i -> integer */ 
  113.    for (j=i;j<=nonzero;j++) *dest++=src[j];
  114.   }
  115.   else
  116.   {
  117.    /* exponential format */
  118.    /* mantissa */
  119.    *dest++=src[2];  /* first digit */
  120.    *dest++='.';
  121.    if (nonzero > 2) 
  122.       for (i=3;i<=nonzero;i++) *dest++=src[i];
  123.    else
  124.       *dest++='0';  /* no fractional part after first digit */
  125.  
  126.    /* include exponent */
  127.    *dest++='E';
  128.    *dest++=src[11]; /* sign of exponent */
  129.    sprintf(ex_buf,"%ld",ex-1); /* exponent */
  130.    *dest++=ex_buf[0];
  131.    *dest++=ex_buf[1];
  132.   }
  133.  }  
  134.  
  135.  *dest='\0';
  136.  return(fnumbuf);
  137. }
  138.