Advertisement
Guest User

sinhatanh-exceptions

a guest
Nov 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <float.h>
  4. #include <fenv.h>
  5. #include <typeinfo>
  6. #include <iomanip>
  7. #include <iostream>
  8. #include <limits>
  9. #include <errno.h>
  10.  
  11. #define PREC 10
  12.  
  13. #define qNAN (std::numeric_limits<double>::quiet_NaN())
  14. #define sNAN (std::numeric_limits<double>::signaling_NaN())
  15. #define INF (1./0.)
  16.  
  17. #define debug(msg, x) std::cout << std::scientific << std::setprecision(PREC) << msg << " = " << x << std::endl;
  18.  
  19. template<class R>
  20. R sinhatanh(R x)
  21. {
  22.     if (typeid(R) == typeid(float))
  23.         return sinhf(atanhf(x));
  24.     else if (typeid(R) == typeid(double))
  25.         return sinh(atanh(x));
  26.     else if (typeid(R) == typeid(long double))
  27.         return sinhl(atanhl(x));
  28.  
  29.     __builtin_unreachable();
  30. }
  31.  
  32. template<class R>
  33. R sinhatanh2_dijkstra(R x)
  34. {
  35.     if (typeid(R) == typeid(float))
  36.         return x/sqrtf((1-x)*(1+x));
  37.     else if (typeid(R) == typeid(double))
  38.         return x/sqrt((1-x)*(1+x));
  39.     else if (typeid(R) == typeid(long double))
  40.         return x/sqrtl((1-x)*(1+x));
  41.     __builtin_unreachable();
  42. }
  43.  
  44. template<class R>
  45. R coshatanh(R x)
  46. {
  47.     if (typeid(R) == typeid(float))
  48.         return coshf(atanhf(x));
  49.     else if (typeid(R) == typeid(double))
  50.         return cosh(atanh(x));
  51.     else if (typeid(R) == typeid(long double))
  52.         return coshl(atanhl(x));
  53.  
  54.     __builtin_unreachable();
  55. }
  56.  
  57. template<class R>
  58. R coshatanh2_dijkstra(R x)
  59. {
  60.     R ret;
  61.     if (typeid(R) == typeid(float))
  62.         ret =  1/sqrtf((1-x)*(1+x));
  63.     else if (typeid(R) == typeid(double))
  64.         ret =  1/sqrt((1-x)*(1+x));
  65.     else if (typeid(R) == typeid(long double))
  66.         ret = 1/sqrtl((1-x)*(1+x));
  67.     return ret;
  68. }
  69.  
  70. #define ARR_LEN(x) (sizeof(x)/sizeof(x[0]))
  71.  
  72. int main()
  73. {
  74.     static const double values[] = {-qNAN, -sNAN, -INF -1., -0., +0., 1., INF, sNAN, qNAN};
  75.     int i;
  76.  
  77.     for (i = 0; i < ARR_LEN(values); ++i)
  78.     {
  79.         double before = sinhatanh(values[i]);
  80.         int err_before = errno;
  81.         double after  = sinhatanh2_dijkstra(values[i]);
  82.         int err_after = errno;
  83.  
  84.         debug("before: ", before);
  85.         debug("after : ", after)
  86.  
  87.         if (err_before != err_after)
  88.             std::cout << "Errno changed!" << std::endl;
  89.  
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement