fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void print(double v)
  7. {
  8. cout << "0x" << hex << (*(long *)&v) << " = " << fixed << setprecision(40) << v << endl;
  9. }
  10.  
  11. int main()
  12. {
  13. // unsigned long val_ul = 0x3d824f7400000000;
  14. // double val = *(double *)&val_ul;
  15.  
  16. double val = 0.0;
  17.  
  18. double step = 0.001;
  19. double min = -99999.0;
  20. double max = 99999.0;
  21.  
  22. double r1 = val - min;
  23. double r2 = r1 / step;
  24. double r3 = round(r2);
  25. double r4 = r3 * step;
  26. double r5 = r4 + min;
  27.  
  28. print(val);
  29. print(step);
  30. print(min);
  31. print(max);
  32.  
  33. cout << endl;
  34.  
  35. print(r1);
  36. print(r2);
  37. print(r3);
  38. print(r4);
  39. print(r5);
  40.  
  41. cout << endl;
  42.  
  43. double temp = round((val - min) / step) * step;
  44. double correct = temp + min;
  45. print(correct);
  46.  
  47. cout << endl;
  48.  
  49. double wrong = round((val - min) / step) * step + min;
  50. print(wrong);
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
0x0 = 0.0000000000000000000000000000000000000000
0x3f50624dd2f1a9fc = 0.0010000000000000000208166817117216851329
0xc0f869f000000000 = -99999.0000000000000000000000000000000000000000
0x40f869f000000000 = 99999.0000000000000000000000000000000000000000

0x40f869f000000000 = 99999.0000000000000000000000000000000000000000
0x4197d77460000000 = 99999000.0000000000000000000000000000000000000000
0x4197d77460000000 = 99999000.0000000000000000000000000000000000000000
0x40f869f000000000 = 99999.0000000000000000000000000000000000000000
0x0 = 0.0000000000000000000000000000000000000000

0x0 = 0.0000000000000000000000000000000000000000

0x0 = 0.0000000000000000000000000000000000000000