Posted to tcl by patthoyts at Tue Jan 05 19:04:19 GMT 2010view raw

  1. #include <float.h>
  2. double
  3. round(double x)
  4. {
  5. double t;
  6.  
  7. if (!_finite(x))
  8. return (x);
  9.  
  10. if (x >= 0.0) {
  11. t = floor(x);
  12. if (t - x <= -0.5)
  13. t += 1.0;
  14. return (t);
  15. } else {
  16. t = floor(-x);
  17. if (t + x <= -0.5)
  18. t += 1.0;
  19. return (-t);
  20. }
  21. }