1 // boost atanh.hpp header file
3 // (C) Copyright Hubert Holin 2001.
4 // (C) Copyright John Maddock 2008.
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org for updates, documentation, and revision history.
11 #ifndef BOOST_ATANH_HPP
12 #define BOOST_ATANH_HPP
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <boost/config.hpp>
21 #include <boost/math/tools/precision.hpp>
22 #include <boost/math/policies/error_handling.hpp>
23 #include <boost/math/special_functions/math_fwd.hpp>
24 #include <boost/math/special_functions/log1p.hpp>
26 // This is the inverse of the hyperbolic tangent function.
34 // This is the main fare
36 template<typename T, typename Policy>
37 inline T atanh_imp(const T x, const Policy& pol)
40 static const char* function = "boost::math::atanh<%1%>(%1%)";
44 return policies::raise_domain_error<T>(
46 "atanh requires x >= -1, but got x = %1%.", x, pol);
50 return policies::raise_domain_error<T>(
52 "atanh requires x <= 1, but got x = %1%.", x, pol);
54 else if((boost::math::isnan)(x))
56 return policies::raise_domain_error<T>(
58 "atanh requires -1 <= x <= 1, but got x = %1%.", x, pol);
60 else if(x < -1 + tools::epsilon<T>())
63 return -policies::raise_overflow_error<T>(function, 0, pol);
65 else if(x > 1 - tools::epsilon<T>())
68 return policies::raise_overflow_error<T>(function, 0, pol);
70 else if(abs(x) >= tools::forth_root_epsilon<T>())
72 // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/
74 return (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
75 return(log( (1 + x) / (1 - x) ) / 2);
79 // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/06/01/03/01/
80 // approximation by taylor series in x at 0 up to order 2
83 if (abs(x) >= tools::root_epsilon<T>())
87 // approximation by taylor series in x at 0 up to order 4
88 result += x3/static_cast<T>(3);
96 template<typename T, typename Policy>
97 inline typename tools::promote_args<T>::type atanh(T x, const Policy&)
99 typedef typename tools::promote_args<T>::type result_type;
100 typedef typename policies::evaluation<result_type, Policy>::type value_type;
101 typedef typename policies::normalise<
103 policies::promote_float<false>,
104 policies::promote_double<false>,
105 policies::discrete_quantile<>,
106 policies::assert_undefined<> >::type forwarding_policy;
107 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
108 detail::atanh_imp(static_cast<value_type>(x), forwarding_policy()),
109 "boost::math::atanh<%1%>(%1%)");
112 inline typename tools::promote_args<T>::type atanh(T x)
114 return boost::math::atanh(x, policies::policy<>());
120 #endif /* BOOST_ATANH_HPP */