fork(1) download
  1. #include <time.h>
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main() {
  7. // your code goes here
  8. double duration = 24000.0/1001.0;
  9. double first_pts = (double)time(NULL);
  10. unsigned long long nb_frame = 1000000;
  11.  
  12. double time_after_1 = first_pts + nb_frame * duration;
  13. double time_after_2 = first_pts;
  14. for(unsigned long long i = 0 ; i < nb_frame ; ++i)
  15. {
  16. time_after_2 += duration;
  17. }
  18.  
  19. double time_after_3 = first_pts;
  20. double c = 0.0; // A running compensation for lost low-order bits.
  21. for(unsigned long long i = 0 ; i < nb_frame ; ++i)
  22. {
  23. double y = duration - c; // So far, so good: c is zero.
  24. double t = time_after_3 + y; // Alas, sum is big, y small, so low-order digits of y are lost.
  25. c = (t - time_after_3) - y; // (t - sum) recovers the high-order part of y; subtracting y recovers -(low part of y)
  26. time_after_3 = t; // Algebraically, c should always be zero. Beware overly-aggressive optimizing compilers!
  27. // Next time around, the lost low part will be added to y in a fresh attempt.
  28. }
  29. cout << time_after_1 - time_after_2 << " or " << time_after_1 - time_after_3 << endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 3340KB
stdin
Standard input is empty
stdout
3.14042e-05 or 1.74623e-09