1 // Copyright John Maddock 2005-2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_TOOLS_PRECISION_INCLUDED
7 #define BOOST_MATH_TOOLS_PRECISION_INCLUDED
13 #include <boost/limits.hpp>
14 #include <boost/assert.hpp>
15 #include <boost/static_assert.hpp>
16 #include <boost/mpl/int.hpp>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/mpl/if.hpp>
19 #include <boost/math/policies/policy.hpp>
21 // These two are for LDBL_MAN_DIG:
25 namespace boost{ namespace math
29 // If T is not specialized, the functions digits, max_value and min_value,
30 // all get synthesised automatically from std::numeric_limits.
31 // However, if numeric_limits is not specialised for type RealType,
32 // for example with NTL::RR type, then you will get a compiler error
33 // when code tries to use these functions, unless you explicitly specialise them.
35 // For example if the precision of RealType varies at runtime,
36 // then numeric_limits support may not be appropriate,
37 // see boost/math/tools/ntl.hpp for examples like
38 // template <> NTL::RR max_value<NTL::RR> ...
39 // See Conceptual Requirements for Real Number Types.
42 inline BOOST_MATH_CONSTEXPR int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) BOOST_NOEXCEPT
44 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
45 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
46 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::radix == 2 || ::std::numeric_limits<T>::radix == 10);
48 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
49 BOOST_ASSERT(::std::numeric_limits<T>::radix == 2 || ::std::numeric_limits<T>::radix == 10);
51 return std::numeric_limits<T>::radix == 2
52 ? std::numeric_limits<T>::digits
53 : ((std::numeric_limits<T>::digits + 1) * 1000L) / 301L;
57 inline BOOST_MATH_CONSTEXPR T max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
59 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
60 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
62 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
64 return (std::numeric_limits<T>::max)();
65 } // Also used as a finite 'infinite' value for - and +infinity, for example:
66 // -max_value<double> = -1.79769e+308, max_value<double> = 1.79769e+308.
69 inline BOOST_MATH_CONSTEXPR T min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
71 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
72 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
74 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
76 return (std::numeric_limits<T>::min)();
81 // Logarithmic limits come next, note that although
82 // we can compute these from the log of the max value
83 // that is not in general thread safe (if we cache the value)
84 // so it's better to specialise these:
86 // For type float first:
89 inline BOOST_MATH_CONSTEXPR T log_max_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
95 inline BOOST_MATH_CONSTEXPR T log_min_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
103 inline BOOST_MATH_CONSTEXPR T log_max_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
109 inline BOOST_MATH_CONSTEXPR T log_min_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
114 // 80 and 128-bit long doubles:
117 inline BOOST_MATH_CONSTEXPR T log_max_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
123 inline BOOST_MATH_CONSTEXPR T log_min_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
129 inline T log_max_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
133 static const T m = boost::math::tools::max_value<T>();
134 static const T val = log(m);
136 static const T val = log(boost::math::tools::max_value<T>());
142 inline T log_min_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
146 static const T m = boost::math::tools::min_value<T>();
147 static const T val = log(m);
149 static const T val = log(boost::math::tools::min_value<T>());
155 inline BOOST_MATH_CONSTEXPR T epsilon(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
157 return std::numeric_limits<T>::epsilon();
160 #if defined(__GNUC__) && ((LDBL_MANT_DIG == 106) || (__LDBL_MANT_DIG__ == 106))
162 inline BOOST_MATH_CONSTEXPR long double epsilon<long double>(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(long double)) BOOST_MATH_NOEXCEPT(long double)
164 // numeric_limits on Darwin (and elsewhere) tells lies here:
165 // the issue is that long double on a few platforms is
166 // really a "double double" which has a non-contiguous
167 // mantissa: 53 bits followed by an unspecified number of
168 // zero bits, followed by 53 more bits. Thus the apparent
169 // precision of the type varies depending where it's been.
170 // Set epsilon to the value that a 106 bit fixed mantissa
171 // type would have, as that will give us sensible behaviour everywhere.
173 // This static assert fails for some unknown reason, so
174 // disabled for now...
175 // BOOST_STATIC_ASSERT(std::numeric_limits<long double>::digits == 106);
176 return 2.4651903288156618919116517665087e-32L;
181 inline T epsilon(const mpl::false_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
183 // Note: don't cache result as precision may vary at runtime:
184 BOOST_MATH_STD_USING // for ADL of std names
185 return ldexp(static_cast<T>(1), 1-policies::digits<T, policies::policy<> >());
189 struct log_limit_traits
191 typedef typename mpl::if_c<
192 (std::numeric_limits<T>::radix == 2) &&
193 (std::numeric_limits<T>::max_exponent == 128
194 || std::numeric_limits<T>::max_exponent == 1024
195 || std::numeric_limits<T>::max_exponent == 16384),
196 mpl::int_<(std::numeric_limits<T>::max_exponent > INT_MAX ? INT_MAX : static_cast<int>(std::numeric_limits<T>::max_exponent))>,
199 BOOST_STATIC_CONSTANT(bool, value = tag_type::value ? true : false);
200 BOOST_STATIC_ASSERT(::std::numeric_limits<T>::is_specialized || (value == 0));
203 template <class T, bool b> struct log_limit_noexcept_traits_imp : public log_limit_traits<T> {};
204 template <class T> struct log_limit_noexcept_traits_imp<T, false> : public boost::integral_constant<bool, false> {};
207 struct log_limit_noexcept_traits : public log_limit_noexcept_traits_imp<T, BOOST_MATH_IS_FLOAT(T)> {};
209 } // namespace detail
212 #pragma warning(push)
213 #pragma warning(disable:4309)
217 inline BOOST_MATH_CONSTEXPR T log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_NOEXCEPT_IF(detail::log_limit_noexcept_traits<T>::value)
219 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
220 return detail::log_max_value<T>(typename detail::log_limit_traits<T>::tag_type());
222 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
224 static const T val = log((std::numeric_limits<T>::max)());
230 inline BOOST_MATH_CONSTEXPR T log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_NOEXCEPT_IF(detail::log_limit_noexcept_traits<T>::value)
232 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
233 return detail::log_min_value<T>(typename detail::log_limit_traits<T>::tag_type());
235 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
237 static const T val = log((std::numeric_limits<T>::min)());
247 inline BOOST_MATH_CONSTEXPR T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) BOOST_MATH_NOEXCEPT(T)
249 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
250 return detail::epsilon<T>(mpl::bool_< ::std::numeric_limits<T>::is_specialized>());
252 return ::std::numeric_limits<T>::is_specialized ?
253 detail::epsilon<T>(mpl::true_()) :
254 detail::epsilon<T>(mpl::false_());
261 inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const mpl::int_<24>&) BOOST_MATH_NOEXCEPT(T)
263 return static_cast<T>(0.00034526698300124390839884978618400831996329879769945L);
267 inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const T*, const mpl::int_<53>&) BOOST_MATH_NOEXCEPT(T)
269 return static_cast<T>(0.1490116119384765625e-7L);
273 inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const T*, const mpl::int_<64>&) BOOST_MATH_NOEXCEPT(T)
275 return static_cast<T>(0.32927225399135962333569506281281311031656150598474e-9L);
279 inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const T*, const mpl::int_<113>&) BOOST_MATH_NOEXCEPT(T)
281 return static_cast<T>(0.1387778780781445675529539585113525390625e-16L);
284 template <class T, class Tag>
285 inline T root_epsilon_imp(const T*, const Tag&)
288 static const T r_eps = sqrt(tools::epsilon<T>());
293 inline T root_epsilon_imp(const T*, const mpl::int_<0>&)
296 return sqrt(tools::epsilon<T>());
300 inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const mpl::int_<24>&) BOOST_MATH_NOEXCEPT(T)
302 return static_cast<T>(0.0049215666011518482998719164346805794944150447839903L);
306 inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const T*, const mpl::int_<53>&) BOOST_MATH_NOEXCEPT(T)
308 return static_cast<T>(6.05545445239333906078989272793696693569753008995e-6L);
312 inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const T*, const mpl::int_<64>&) BOOST_MATH_NOEXCEPT(T)
314 return static_cast<T>(4.76837158203125e-7L);
318 inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const T*, const mpl::int_<113>&) BOOST_MATH_NOEXCEPT(T)
320 return static_cast<T>(5.7749313854154005630396773604745549542403508090496e-12L);
323 template <class T, class Tag>
324 inline T cbrt_epsilon_imp(const T*, const Tag&)
326 BOOST_MATH_STD_USING;
327 static const T cbrt_eps = pow(tools::epsilon<T>(), T(1) / 3);
332 inline T cbrt_epsilon_imp(const T*, const mpl::int_<0>&)
334 BOOST_MATH_STD_USING;
335 return pow(tools::epsilon<T>(), T(1) / 3);
339 inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const mpl::int_<24>&) BOOST_MATH_NOEXCEPT(T)
341 return static_cast<T>(0.018581361171917516667460937040007436176452688944747L);
345 inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const mpl::int_<53>&) BOOST_MATH_NOEXCEPT(T)
347 return static_cast<T>(0.0001220703125L);
351 inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const mpl::int_<64>&) BOOST_MATH_NOEXCEPT(T)
353 return static_cast<T>(0.18145860519450699870567321328132261891067079047605e-4L);
357 inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const mpl::int_<113>&) BOOST_MATH_NOEXCEPT(T)
359 return static_cast<T>(0.37252902984619140625e-8L);
362 template <class T, class Tag>
363 inline T forth_root_epsilon_imp(const T*, const Tag&)
366 static const T r_eps = sqrt(sqrt(tools::epsilon<T>()));
371 inline T forth_root_epsilon_imp(const T*, const mpl::int_<0>&)
374 return sqrt(sqrt(tools::epsilon<T>()));
378 struct root_epsilon_traits
380 typedef mpl::int_< (::std::numeric_limits<T>::radix == 2) ? std::numeric_limits<T>::digits : 0> tag_type;
381 BOOST_STATIC_CONSTANT(bool, has_noexcept = (tag_type::value == 113) || (tag_type::value == 64) || (tag_type::value == 53) || (tag_type::value == 24));
387 inline BOOST_MATH_CONSTEXPR T root_epsilon() BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && detail::root_epsilon_traits<T>::has_noexcept)
389 return detail::root_epsilon_imp(static_cast<T const*>(0), typename detail::root_epsilon_traits<T>::tag_type());
393 inline BOOST_MATH_CONSTEXPR T cbrt_epsilon() BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && detail::root_epsilon_traits<T>::has_noexcept)
395 return detail::cbrt_epsilon_imp(static_cast<T const*>(0), typename detail::root_epsilon_traits<T>::tag_type());
399 inline BOOST_MATH_CONSTEXPR T forth_root_epsilon() BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && detail::root_epsilon_traits<T>::has_noexcept)
401 return detail::forth_root_epsilon_imp(static_cast<T const*>(0), typename detail::root_epsilon_traits<T>::tag_type());
408 #endif // BOOST_MATH_TOOLS_PRECISION_INCLUDED