home *** CD-ROM | disk | FTP | other *** search
/ pc.louisiana.edu/pub/unix/ / Louisiana_UNIX.tar / Louisiana_UNIX / xspread3.0.zoo / utils.c < prev    next >
C/C++ Source or Header  |  1994-03-25  |  1KB  |  52 lines

  1.  
  2. #include "config.h"
  3. #include <math.h>
  4.  
  5. #ifdef RINT
  6. /**     round-to-even, also known as ``banker's rounding''.
  7.     With round-to-even, a number exactly halfway between two values is
  8.     rounded to whichever is even; e.g. rnd(0.5)=0, rnd(1.5)=2,
  9.     rnd(2.5)=2, rnd(3.5)=4.  This is the default rounding mode for
  10.     IEEE floating point, for good reason: it has better numeric
  11.     properties.  For example, if X+Y is an integer,
  12.     then X+Y = rnd(X)+rnd(Y) with round-to-even,
  13.     but not always with sc's rounding (which is
  14.     round-to-positive-infinity).  I ran into this problem when trying to
  15.     split interest in an account to two people fairly.
  16. **/
  17. double 
  18. rint(d)
  19. double d;
  20. {
  21.     /* as sent */
  22.     double fl = floor(d),  fr = d - fl;
  23.  
  24.     return ((fr < 0.5) || ((fr == 0.5) && (fl == floor(fl/2) * 2)))
  25.             ? fl : ceil(d);
  26. }
  27. #endif
  28.  
  29.  
  30. /*
  31.  * pow10 calculates the power function of base 10
  32.  */
  33.  
  34. double pow10(p)
  35.    double p;
  36. {
  37.    double q;
  38.  
  39.    p = floor(p);
  40.    if (p >= 0) {
  41.       for (q = 1; p > 0; --p)
  42.          q = q * 10;
  43.    }
  44.    else {
  45.       p = -p;
  46.       for (q = 1; p > 0; --p)
  47.          q = q / 10;
  48.    }
  49.    return q;
  50. }
  51.  
  52.