home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09044a < prev    next >
Text File  |  1990-07-21  |  3KB  |  85 lines

  1. /*  >  DPRINTF-F.C
  2.  *  dprintf -- PrintFloat 2nd Version
  3.  *  (C)  August 30  1989  Arkin Asaf
  4.  *  All rights reserved
  5.  *  Altered PrintFloat does no double to int casts; the
  6.  *  math library floor function is used instead.
  7.  *  PrintFloat and ToFloat perform floating point
  8.  *  arithmetic, requiring the presence of a FP processor
  9.  *  or emulator, and the floor function (rounds down a
  10.  *  double to its integer value. */
  11.  
  12. /*  void  PrintFloat(long double, int, int, char, char,
  13.  *  int *) Print a floating point number in standard (%f)
  14.  *  or engineering (%e) form; the %g format requires that
  15.  *  the shortest of the two be selected. The number divides
  16.  *  into integer, fraction and exponent parts; the exponent
  17.  *  is stringized with ToInteger, the integer and fraction
  18.  *  with ToFloat. */
  19.  
  20. static void  PrintFloat(long double Float, int Width, int
  21.                         Precis, char Flags, char Format,
  22.                         int *OutCnt)
  23. {
  24.   :
  25.   long double  Float2;
  26.  
  27.   :
  28.   :
  29.   /*  The mantissa divides into integer and fraction parts,
  30.    *  stringized by ToFloat: the last digit always rounds
  31.    *  up; a period is printed only before a fraction or in
  32.    *  the varient format; the %g format allows trailing
  33.    *  zeros in the fraction to be lost. N.B.: floor rounds
  34.    *  only doubles -- long doubles are not catered for. */
  35.   Float2=floor(Float);
  36.   Float-=Float2;
  37.   if (Precis<=0 && Float>=.5)
  38.     ++Float2;
  39.   LengthI=ToFloat(&BufferI,(double) Float2,-1);
  40.   if (Precis>0)
  41.   {
  42.     for (LengthF=0; LengthF<Precis; ++LengthF)
  43.       Float*=10;
  44.     Float2=floor(Float);
  45.     Float-=Float2;
  46.     if (Float>=.5)
  47.       ++Float2;
  48.     LengthF=ToFloat(&BufferF,(double) Float2,Precis);
  49.   }
  50.   else
  51.     LengthF=0;
  52.   :
  53.   :
  54. }
  55.  
  56. /*  int  ToFloat(char **, double, int)
  57.  *  Convert a double to a NULL-terminated string of digits.
  58.  *  If the string has less digits than the precision,
  59.  *  additional zeros are inserted at the start of it.
  60.  *  ToFloat allocates a memory block in which it stores
  61.  *  the string -- Buffer returns its address.  */
  62.  
  63. static int  ToFloat(char **Buffer, double Float, int Precis)
  64. {
  65.   int  Cnt,  Length,  Char;
  66.   double  TempFloat = Float;
  67.  
  68.   if (Precis<0)
  69.     Precis=1;
  70.   for (Cnt=0; TempFloat>=1; TempFloat/=10, ++Cnt)  ;
  71.   *Buffer=malloc(Maximum(Precis,Cnt)+1);
  72.   if (*Buffer==NULL)
  73.     return  0;
  74.   for (Length=0; Length+Cnt<Precis; )
  75.     (*Buffer)[Length++]='0';
  76.   Cnt= Length=Maximum(Length+Cnt,Precis);
  77.   (*Buffer)[Length]='\0';
  78.   for (; Float>=1; Float/=10)
  79.   {
  80.     Char=floor(Float-floor(Float/10)*10);
  81.     (*Buffer)[--Cnt]=ToDigit(Char);
  82.   }
  83.   return  Length;
  84. }
  85.