round.h 924 B

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