Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / math / special_functions / detail / bernoulli_details.hpp
index 75fadbf..3046949 100644 (file)
@@ -69,7 +69,7 @@ T t2n_asymptotic(int n)
 //
 struct max_bernoulli_root_functor
 {
-   max_bernoulli_root_functor(long long t) : target(static_cast<double>(t)) {}
+   max_bernoulli_root_functor(ulong_long_type t) : target(static_cast<double>(t)) {}
    double operator()(double n)
    {
       BOOST_MATH_STD_USING
@@ -93,11 +93,18 @@ private:
 template <class T, class Policy>
 inline std::size_t find_bernoulli_overflow_limit(const mpl::false_&)
 {
-   long long t = lltrunc(boost::math::tools::log_max_value<T>());
+   // Set a limit on how large the result can ever be:
+   static const double max_result = static_cast<double>((std::numeric_limits<std::size_t>::max)() - 1000u);
+
+   ulong_long_type t = lltrunc(boost::math::tools::log_max_value<T>());
    max_bernoulli_root_functor fun(t);
    boost::math::tools::equal_floor tol;
    boost::uintmax_t max_iter = boost::math::policies::get_max_root_iterations<Policy>();
-   return static_cast<std::size_t>(boost::math::tools::toms748_solve(fun, sqrt(double(t)), double(t), tol, max_iter).first) / 2;
+   double result = boost::math::tools::toms748_solve(fun, sqrt(double(t)), double(t), tol, max_iter).first / 2;
+   if (result > max_result)
+      result = max_result;
+   
+   return static_cast<std::size_t>(result);
 }
 
 template <class T, class Policy>