fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <limits>
  4.  
  5. template <typename T1, typename T2>
  6. double pow_bad(T1 base, T2 exp)
  7. {
  8. return std::exp( double(exp) * std::log(double(base)) );
  9. }
  10.  
  11. int main()
  12. {
  13. using namespace std;
  14.  
  15. const int LENGTH = 15;
  16. cout.precision(numeric_limits<double>::max_digits10);
  17.  
  18. for (int i = 1; i < LENGTH; i += 2)
  19. {
  20. double res = pow(i, 2);
  21. cout << i << "^2.0 = " << res << " " << int(res) << endl;
  22. }
  23. cout << endl;
  24.  
  25. for (int i = 1; i < LENGTH; i += 2)
  26. {
  27. double res = pow_bad(i, 2);
  28. cout << i << "^2.0 = " << res << " " << int(res) << endl;
  29. }
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1^2.0 = 1 1
3^2.0 = 9 9
5^2.0 = 25 25
7^2.0 = 49 49
9^2.0 = 81 81
11^2.0 = 121 121
13^2.0 = 169 169

1^2.0 = 1 1
3^2.0 = 9.0000000000000018 9
5^2.0 = 24.999999999999996 24
7^2.0 = 48.999999999999993 48
9^2.0 = 81.000000000000028 81
11^2.0 = 121.00000000000003 121
13^2.0 = 169 169