fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cfenv>
  4. #include <cerrno>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. void throw_fe()
  10. {
  11. if (errno) throw runtime_error(strerror(errno));
  12. if(fetestexcept(FE_DIVBYZERO)) throw runtime_error("FE_DIVBYZERO");
  13. if(fetestexcept(FE_INEXACT)) throw runtime_error("FE_INEXACT");
  14. if(fetestexcept(FE_INVALID)) throw runtime_error("FE_INVALID");
  15. if(fetestexcept(FE_OVERFLOW)) throw runtime_error("FE_OVERFLOW");
  16. if(fetestexcept(FE_UNDERFLOW)) throw runtime_error("FE_UNDERFLOW");
  17. }
  18.  
  19.  
  20. double Pow(double x, double y)
  21. {
  22. errno = 0;
  23. feclearexcept(FE_ALL_EXCEPT);
  24. double z = pow(x,y);
  25. throw_fe();
  26. return z;
  27. }
  28.  
  29. double Sqrt(double x)
  30. {
  31. errno = 0;
  32. feclearexcept(FE_ALL_EXCEPT);
  33. double z = sqrt(x);
  34. throw_fe();
  35. return z;
  36. }
  37.  
  38. double Log(double x)
  39. {
  40. errno = 0;
  41. feclearexcept(FE_ALL_EXCEPT);
  42. double z = log(x);
  43. throw_fe();
  44. return z;
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50. try {
  51. double q = Sqrt(-2) + Pow(1e20,1e20);
  52. printf("log(0) = %f\n", q);
  53. } catch(exception&e)
  54. {
  55. cout << "Catch: " << e.what() << endl;
  56. }
  57. }
  58.  
Success #stdin #stdout 0s 4496KB
stdin
Standard input is empty
stdout
Catch: Numerical argument out of domain