Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <float.h>
- #include <fenv.h>
- #include <typeinfo>
- #include <iomanip>
- #include <iostream>
- #include <limits>
- #include <errno.h>
- #define PREC 10
- #define qNAN (std::numeric_limits<double>::quiet_NaN())
- #define sNAN (std::numeric_limits<double>::signaling_NaN())
- #define INF (1./0.)
- #define debug(msg, x) std::cout << std::scientific << std::setprecision(PREC) << msg << " = " << x << std::endl;
- 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_dijkstra(R x)
- {
- if (typeid(R) == typeid(float))
- return x/sqrtf((1-x)*(1+x));
- else if (typeid(R) == typeid(double))
- return x/sqrt((1-x)*(1+x));
- else if (typeid(R) == typeid(long double))
- return x/sqrtl((1-x)*(1+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_dijkstra(R x)
- {
- R ret;
- if (typeid(R) == typeid(float))
- ret = 1/sqrtf((1-x)*(1+x));
- else if (typeid(R) == typeid(double))
- ret = 1/sqrt((1-x)*(1+x));
- else if (typeid(R) == typeid(long double))
- ret = 1/sqrtl((1-x)*(1+x));
- return ret;
- }
- #define ARR_LEN(x) (sizeof(x)/sizeof(x[0]))
- int main()
- {
- static const double values[] = {-qNAN, -sNAN, -INF -1., -0., +0., 1., INF, sNAN, qNAN};
- int i;
- for (i = 0; i < ARR_LEN(values); ++i)
- {
- double before = sinhatanh(values[i]);
- int err_before = errno;
- double after = sinhatanh2_dijkstra(values[i]);
- int err_after = errno;
- debug("before: ", before);
- debug("after : ", after)
- if (err_before != err_after)
- std::cout << "Errno changed!" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement