home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ROUND.H < prev    next >
C/C++ Source or Header  |  1997-07-05  |  924b  |  56 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** rounding macros by Dave Knapp, Thad Smith, Jon Strayer, & Bob Stout
  5. */
  6.  
  7. #ifndef ROUND__H
  8. #define ROUND__H
  9.  
  10. #include <math.h>
  11.  
  12. #if defined(__cplusplus) && __cplusplus
  13.  
  14. /*
  15. ** Safe C++ inline versions
  16. */
  17.  
  18. /* round to integer */
  19.  
  20. inline int iround(double x)
  21. {
  22.       return (int)floor(x + 0.5);
  23. }
  24.  
  25. /* round number n to d decimal points */
  26.  
  27. inline double fround(double n, unsigned d)
  28. {
  29.       return floor(n * pow(10., d) + .5) / pow(10., d);
  30. }
  31.  
  32. #else
  33.  
  34. /*
  35. ** NOTE: These C macro versions are unsafe since arguments are referenced
  36. **       more than once.
  37. **
  38. **       Avoid using these with expression arguments to be safe.
  39. */
  40.  
  41. /*
  42. ** round to integer
  43. */
  44.  
  45. #define iround(x) floor((x) + 0.5)
  46.  
  47. /*
  48. ** round number n to d decimal points
  49. */
  50.  
  51. #define fround(n,d) (floor((n)*pow(10.,(d))+.5)/pow(10.,(d)))
  52.  
  53. #endif
  54.  
  55. #endif /* ROUND__H */
  56.