fork download
  1. // https://stackoverflow.com/q/17035464/4208440
  2. #include <cstdio>
  3.  
  4. /**
  5.  * Round to the nearest integer.
  6.  * for tie-breaks: round half to even (bankers' rounding)
  7.  */
  8. inline double rint(double d)
  9. {
  10. double x = 6755399441055744.0; // 1.5 * pow (2, 52);
  11. return d + x - x;
  12. }
  13.  
  14. int main()
  15. {
  16. // round to nearest integer
  17. printf("%.1f, %.1f\n", rint(-12345678.3), rint(-12345678.9));
  18.  
  19. // test tie-breaking rule
  20. printf("%.1f, %.1f, %.1f, %.1f\n", rint(-24.5), rint(-23.5), rint(23.5), rint(24.5));
  21. return 0;
  22. }
  23.  
  24. // output:
  25. // -12345678.0, -12345679.0
  26. // -24.0, -24.0, 24.0, 24.0
  27.  
Success #stdin #stdout 0s 4596KB
stdin
Standard input is empty
stdout
-12345678.0, -12345679.0
-24.0, -24.0, 24.0, 24.0