Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <float.h>
- #include <typeinfo>
- #define debug(x) printf(#x " = %.20lle\n", x)
- template<class R>
- R sinhatanh(R x)
- {
- if (typeid(R) == typeid(float))
- return sinhf(atanhf(x));
- else if (typeid(R) == typeid(double))
- return sinh(atanh(x));
- else if (typeid(R) == typeid(long double))
- return sinhl(atanhl(x));
- __builtin_unreachable();
- }
- template<class R>
- R sinhatanh2(R x)
- {
- if (typeid(R) == typeid(float))
- return x/sqrtf(1 - x*x);
- else if (typeid(R) == typeid(double))
- return x/sqrt(1 - x*x);
- else if (typeid(R) == typeid(long double))
- return x/sqrtl(1 - x*x);
- __builtin_unreachable();
- }
- template<class R>
- R coshatanh(R x)
- {
- if (typeid(R) == typeid(float))
- return coshf(atanhf(x));
- else if (typeid(R) == typeid(double))
- return cosh(atanh(x));
- else if (typeid(R) == typeid(long double))
- return coshl(atanhl(x));
- __builtin_unreachable();
- }
- template<class R>
- R coshatanh2(R x)
- {
- if (typeid(R) == typeid(float))
- return 1/sqrtf(1 - x*x);
- else if (typeid(R) == typeid(double))
- return 1/sqrt(1 - x*x);
- else if (typeid(R) == typeid(long double))
- return 1/sqrtl(1 - x*x);
- __builtin_unreachable();
- }
- template<class R>
- R nextafter_(R x, R y)
- {
- if (typeid(R) == typeid(float))
- return nextafterf(x, y);
- else if (typeid(R) == typeid(double))
- return nextafter(x, y);
- else if (typeid(R) == typeid(long double))
- return nextafterl(x, y);
- __builtin_unreachable();
- }
- void sinh_test()
- {
- long double xi = 1, s1, s2, mone=-1;
- do
- {
- s1 = sinhatanh(xi);
- s2 = sinhatanh2(xi);
- if (s1 != s2)
- {
- debug(s1);
- debug(s2);
- debug(s1 - s2);
- debug(xi);
- }
- xi = nextafter_(xi, mone);
- } while(isnan(s1-s2) || isinf(s2-s1) || s1 - s2 == 0);
- }
- void cosh_test()
- {
- long double xi = 1, c1, c2, mone=-1;
- do
- {
- c1 = coshatanh(xi);
- c2 = coshatanh2(xi);
- if (c1 != c2)
- {
- debug(c1);
- debug(c2);
- debug(c1 - c2);
- debug(xi);
- }
- xi = nextafter_(xi, mone);
- } while (isnan(c1-c2) || isinf(c2-c1) || c1 - c2 == 0);
- }
- int main()
- {
- sinh_test();
- cosh_test();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement