1 // boost asinh.hpp header file
3 // (C) Copyright Eric Ford 2001 & Hubert Holin.
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_ACOSH_HPP
12 #define BOOST_ACOSH_HPP
18 #include <boost/config/no_tr1/cmath.hpp>
19 #include <boost/config.hpp>
20 #include <boost/math/tools/precision.hpp>
21 #include <boost/math/policies/error_handling.hpp>
22 #include <boost/math/special_functions/math_fwd.hpp>
23 #include <boost/math/special_functions/log1p.hpp>
24 #include <boost/math/constants/constants.hpp>
26 // This is the inverse of the hyperbolic cosine function.
34 #if defined(__GNUC__) && (__GNUC__ < 3)
35 // gcc 2.x ignores function scope using declarations,
36 // put them in the scope of the enclosing namespace instead:
42 using ::std::numeric_limits;
45 template<typename T, typename Policy>
46 inline T acosh_imp(const T x, const Policy& pol)
52 return policies::raise_domain_error<T>(
53 "boost::math::acosh<%1%>(%1%)",
54 "acosh requires x >= 1, but got x = %1%.", x, pol);
56 else if ((x - 1) >= tools::root_epsilon<T>())
58 if (x > 1 / tools::root_epsilon<T>())
60 // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/06/01/0001/
61 // approximation by laurent series in 1/x at 0+ order from -1 to 0
62 return log(x) + constants::ln_two<T>();
66 // This is just a rearrangement of the standard form below
67 // devised to minimse loss of precision when x ~ 1:
69 return boost::math::log1p(y + sqrt(y * y + 2 * y), pol);
73 // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/
74 return( log( x + sqrt(x * x - 1) ) );
79 // see http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/04/01/0001/
82 // approximation by taylor series in y at 0 up to order 2
83 T result = sqrt(2 * y) * (1 - y /12 + 3 * y * y / 160);
89 template<typename T, typename Policy>
90 inline typename tools::promote_args<T>::type acosh(T x, const Policy&)
92 typedef typename tools::promote_args<T>::type result_type;
93 typedef typename policies::evaluation<result_type, Policy>::type value_type;
94 typedef typename policies::normalise<
96 policies::promote_float<false>,
97 policies::promote_double<false>,
98 policies::discrete_quantile<>,
99 policies::assert_undefined<> >::type forwarding_policy;
100 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
101 detail::acosh_imp(static_cast<value_type>(x), forwarding_policy()),
102 "boost::math::acosh<%1%>(%1%)");
105 inline typename tools::promote_args<T>::type acosh(T x)
107 return boost::math::acosh(x, policies::policy<>());
113 #endif /* BOOST_ACOSH_HPP */