// Copyright John Maddock 2005-2006. // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MATH_TOOLS_PRECISION_INCLUDED #define BOOST_MATH_TOOLS_PRECISION_INCLUDED #ifdef _MSC_VER #pragma once #endif #include #include #include #include #include #include #include #include // LDBL_MANT_DIG namespace boost{ namespace math { namespace tools { // If T is not specialized, the functions digits, max_value and min_value, // all get synthesised automatically from std::numeric_limits. // However, if numeric_limits is not specialised for type RealType, // for example with NTL::RR type, then you will get a compiler error // when code tries to use these functions, unless you explicitly specialise them. // For example if the precision of RealType varies at runtime, // then numeric_limits support may not be appropriate, // see boost/math/tools/ntl.hpp for examples like // template <> NTL::RR max_value ... // See Conceptual Requirements for Real Number Types. template inline constexpr int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) noexcept { static_assert( ::std::numeric_limits::is_specialized, "Type T must be specialized"); static_assert( ::std::numeric_limits::radix == 2 || ::std::numeric_limits::radix == 10, "Type T must have a radix of 2 or 10"); return std::numeric_limits::radix == 2 ? std::numeric_limits::digits : ((std::numeric_limits::digits + 1) * 1000L) / 301L; } template inline constexpr T max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { static_assert( ::std::numeric_limits::is_specialized, "Type T must be specialized"); return (std::numeric_limits::max)(); } // Also used as a finite 'infinite' value for - and +infinity, for example: // -max_value = -1.79769e+308, max_value = 1.79769e+308. template inline constexpr T min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { static_assert( ::std::numeric_limits::is_specialized, "Type T must be specialized"); return (std::numeric_limits::min)(); } namespace detail{ // // Logarithmic limits come next, note that although // we can compute these from the log of the max value // that is not in general thread safe (if we cache the value) // so it's better to specialise these: // // For type float first: // template inline constexpr T log_max_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return 88.0f; } template inline constexpr T log_min_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return -87.0f; } // // Now double: // template inline constexpr T log_max_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return 709.0; } template inline constexpr T log_min_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return -708.0; } // // 80 and 128-bit long doubles: // template inline constexpr T log_max_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return 11356.0L; } template inline constexpr T log_min_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return -11355.0L; } template inline T log_max_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) { BOOST_MATH_STD_USING #ifdef __SUNPRO_CC static const T m = boost::math::tools::max_value(); static const T val = log(m); #else static const T val = log(boost::math::tools::max_value()); #endif return val; } template inline T log_min_value(const std::integral_constant& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) { BOOST_MATH_STD_USING #ifdef __SUNPRO_CC static const T m = boost::math::tools::min_value(); static const T val = log(m); #else static const T val = log(boost::math::tools::min_value()); #endif return val; } template inline constexpr T epsilon(const std::true_type& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(std::is_floating_point::value) { return std::numeric_limits::epsilon(); } #if defined(__GNUC__) && ((LDBL_MANT_DIG == 106) || (__LDBL_MANT_DIG__ == 106)) template <> inline constexpr long double epsilon(const std::true_type& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(long double)) noexcept(std::is_floating_point::value) { // numeric_limits on Darwin (and elsewhere) tells lies here: // the issue is that long double on a few platforms is // really a "double double" which has a non-contiguous // mantissa: 53 bits followed by an unspecified number of // zero bits, followed by 53 more bits. Thus the apparent // precision of the type varies depending where it's been. // Set epsilon to the value that a 106 bit fixed mantissa // type would have, as that will give us sensible behaviour everywhere. // // This static assert fails for some unknown reason, so // disabled for now... // static_assert(std::numeric_limits::digits == 106); return 2.4651903288156618919116517665087e-32L; } #endif template inline T epsilon(const std::false_type& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) { // Note: don't cache result as precision may vary at runtime: BOOST_MATH_STD_USING // for ADL of std names return ldexp(static_cast(1), 1-policies::digits >()); } template struct log_limit_traits { typedef typename std::conditional< (std::numeric_limits::radix == 2) && (std::numeric_limits::max_exponent == 128 || std::numeric_limits::max_exponent == 1024 || std::numeric_limits::max_exponent == 16384), std::integral_constant::max_exponent > INT_MAX ? INT_MAX : static_cast(std::numeric_limits::max_exponent))>, std::integral_constant >::type tag_type; static constexpr bool value = tag_type::value ? true : false; static_assert(::std::numeric_limits::is_specialized || (value == 0), "Type T must be specialized or equal to 0"); }; template struct log_limit_noexcept_traits_imp : public log_limit_traits {}; template struct log_limit_noexcept_traits_imp : public std::integral_constant {}; template struct log_limit_noexcept_traits : public log_limit_noexcept_traits_imp::value> {}; } // namespace detail #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable:4309) #endif template inline constexpr T log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(detail::log_limit_noexcept_traits::value) { #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS return detail::log_max_value(typename detail::log_limit_traits::tag_type()); #else BOOST_MATH_ASSERT(::std::numeric_limits::is_specialized); BOOST_MATH_STD_USING static const T val = log((std::numeric_limits::max)()); return val; #endif } template inline constexpr T log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) noexcept(detail::log_limit_noexcept_traits::value) { #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS return detail::log_min_value(typename detail::log_limit_traits::tag_type()); #else BOOST_MATH_ASSERT(::std::numeric_limits::is_specialized); BOOST_MATH_STD_USING static const T val = log((std::numeric_limits::min)()); return val; #endif } #ifdef _MSC_VER #pragma warning(pop) #endif template inline constexpr T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) noexcept(std::is_floating_point::value) { #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS return detail::epsilon(std::integral_constant::is_specialized>()); #else return ::std::numeric_limits::is_specialized ? detail::epsilon(std::true_type()) : detail::epsilon(std::false_type()); #endif } namespace detail{ template inline constexpr T root_epsilon_imp(const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.00034526698300124390839884978618400831996329879769945L); } template inline constexpr T root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.1490116119384765625e-7L); } template inline constexpr T root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.32927225399135962333569506281281311031656150598474e-9L); } template inline constexpr T root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.1387778780781445675529539585113525390625e-16L); } template inline T root_epsilon_imp(const T*, const Tag&) { BOOST_MATH_STD_USING static const T r_eps = sqrt(tools::epsilon()); return r_eps; } template inline T root_epsilon_imp(const T*, const std::integral_constant&) { BOOST_MATH_STD_USING return sqrt(tools::epsilon()); } template inline constexpr T cbrt_epsilon_imp(const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.0049215666011518482998719164346805794944150447839903L); } template inline constexpr T cbrt_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(6.05545445239333906078989272793696693569753008995e-6L); } template inline constexpr T cbrt_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(4.76837158203125e-7L); } template inline constexpr T cbrt_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(5.7749313854154005630396773604745549542403508090496e-12L); } template inline T cbrt_epsilon_imp(const T*, const Tag&) { BOOST_MATH_STD_USING; static const T cbrt_eps = pow(tools::epsilon(), T(1) / 3); return cbrt_eps; } template inline T cbrt_epsilon_imp(const T*, const std::integral_constant&) { BOOST_MATH_STD_USING; return pow(tools::epsilon(), T(1) / 3); } template inline constexpr T forth_root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.018581361171917516667460937040007436176452688944747L); } template inline constexpr T forth_root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.0001220703125L); } template inline constexpr T forth_root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.18145860519450699870567321328132261891067079047605e-4L); } template inline constexpr T forth_root_epsilon_imp(const T*, const std::integral_constant&) noexcept(std::is_floating_point::value) { return static_cast(0.37252902984619140625e-8L); } template inline T forth_root_epsilon_imp(const T*, const Tag&) { BOOST_MATH_STD_USING static const T r_eps = sqrt(sqrt(tools::epsilon())); return r_eps; } template inline T forth_root_epsilon_imp(const T*, const std::integral_constant&) { BOOST_MATH_STD_USING return sqrt(sqrt(tools::epsilon())); } template struct root_epsilon_traits { typedef std::integral_constant::radix == 2) && (::std::numeric_limits::digits != INT_MAX) ? std::numeric_limits::digits : 0> tag_type; static constexpr bool has_noexcept = (tag_type::value == 113) || (tag_type::value == 64) || (tag_type::value == 53) || (tag_type::value == 24); }; } template inline constexpr T root_epsilon() noexcept(std::is_floating_point::value && detail::root_epsilon_traits::has_noexcept) { return detail::root_epsilon_imp(static_cast(0), typename detail::root_epsilon_traits::tag_type()); } template inline constexpr T cbrt_epsilon() noexcept(std::is_floating_point::value && detail::root_epsilon_traits::has_noexcept) { return detail::cbrt_epsilon_imp(static_cast(0), typename detail::root_epsilon_traits::tag_type()); } template inline constexpr T forth_root_epsilon() noexcept(std::is_floating_point::value && detail::root_epsilon_traits::has_noexcept) { return detail::forth_root_epsilon_imp(static_cast(0), typename detail::root_epsilon_traits::tag_type()); } } // namespace tools } // namespace math } // namespace boost #endif // BOOST_MATH_TOOLS_PRECISION_INCLUDED