File indexing completed on 2026-08-17 08:48:56
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_SF_LOGIT_HPP
0008 #define BOOST_MATH_SF_LOGIT_HPP
0009
0010 #include <boost/math/tools/config.hpp>
0011 #include <boost/math/policies/policy.hpp>
0012 #include <boost/math/policies/error_handling.hpp>
0013 #include <cmath>
0014 #include <cfenv>
0015
0016 namespace boost {
0017 namespace math {
0018
0019 template <typename RealType, typename Policy>
0020 RealType logit(RealType p, const Policy&)
0021 {
0022 BOOST_MATH_STD_USING
0023 using std::atanh;
0024
0025 using promoted_real_type = typename policies::evaluation<RealType, Policy>::type;
0026
0027 if (p < tools::min_value<RealType>())
0028 {
0029 return -policies::raise_overflow_error<RealType>("logit", "sub-normals will overflow ln(x/(1-x))", Policy());
0030 }
0031
0032 static const RealType crossover {RealType{1}/4};
0033 const auto promoted_p {static_cast<promoted_real_type>(p)};
0034 RealType result {};
0035 if (p > crossover)
0036 {
0037 result = static_cast<RealType>(2 * atanh(2 * promoted_p - 1));
0038 }
0039 else
0040 {
0041 result = static_cast<RealType>(log(promoted_p / (1 - promoted_p)));
0042 }
0043
0044 return result;
0045 }
0046
0047 template <typename RealType>
0048 RealType logit(RealType p)
0049 {
0050 return logit(p, policies::policy<>());
0051 }
0052
0053 }
0054 }
0055
0056 #endif