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 int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
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 T max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(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 T min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(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 T log_max_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
95 inline T log_min_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
103 inline T log_max_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
109 inline T log_min_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
114 // 80 and 128-bit long doubles:
117 inline T log_max_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
123 inline T log_min_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
129 inline T log_max_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
131 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
132 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
134 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
137 static const T val = log((std::numeric_limits<T>::max)());
142 inline T log_min_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
144 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
145 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
147 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
150 static const T val = log((std::numeric_limits<T>::min)());
155 inline T epsilon(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
157 return std::numeric_limits<T>::epsilon();
160 #if (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) && ((LDBL_MANT_DIG == 106) || (__LDBL_MANT_DIG__ == 106))
162 inline long double epsilon<long double>(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(long double))
164 // numeric_limits on Darwin tells lies here.
165 // This static assert fails for some unknown reason, so
166 // disabled for now...
167 // BOOST_STATIC_ASSERT(std::numeric_limits<long double>::digits == 106);
168 return 2.4651903288156618919116517665087e-32L;
173 inline T epsilon(const mpl::false_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
175 BOOST_MATH_STD_USING // for ADL of std names
176 static const T eps = ldexp(static_cast<T>(1), 1-policies::digits<T, policies::policy<> >());
180 } // namespace detail
183 #pragma warning(push)
184 #pragma warning(disable:4309)
188 inline T log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
190 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
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_ASSERT( ::std::numeric_limits<T>::is_specialized);
200 return detail::log_max_value<T>(tag_type());
202 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
204 static const T val = log((std::numeric_limits<T>::max)());
210 inline T log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
212 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
213 typedef typename mpl::if_c<
214 (std::numeric_limits<T>::radix == 2) &&
215 (std::numeric_limits<T>::max_exponent == 128
216 || std::numeric_limits<T>::max_exponent == 1024
217 || std::numeric_limits<T>::max_exponent == 16384),
218 mpl::int_<(std::numeric_limits<T>::max_exponent > INT_MAX ? INT_MAX : static_cast<int>(std::numeric_limits<T>::max_exponent))>,
222 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
223 return detail::log_min_value<T>(tag_type());
225 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
227 static const T val = log((std::numeric_limits<T>::min)());
237 inline T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
239 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
240 return detail::epsilon<T>(mpl::bool_< ::std::numeric_limits<T>::is_specialized>());
242 return ::std::numeric_limits<T>::is_specialized ?
243 detail::epsilon<T>(mpl::true_()) :
244 detail::epsilon<T>(mpl::false_());
251 inline T root_epsilon_imp(const mpl::int_<24>&)
253 return static_cast<T>(0.00034526698300124390839884978618400831996329879769945L);
257 inline T root_epsilon_imp(const T*, const mpl::int_<53>&)
259 return static_cast<T>(0.1490116119384765625e-7L);
263 inline T root_epsilon_imp(const T*, const mpl::int_<64>&)
265 return static_cast<T>(0.32927225399135962333569506281281311031656150598474e-9L);
269 inline T root_epsilon_imp(const T*, const mpl::int_<113>&)
271 return static_cast<T>(0.1387778780781445675529539585113525390625e-16L);
274 template <class T, class Tag>
275 inline T root_epsilon_imp(const T*, const Tag&)
278 static const T r_eps = sqrt(tools::epsilon<T>());
283 inline T forth_root_epsilon_imp(const T*, const mpl::int_<24>&)
285 return static_cast<T>(0.018581361171917516667460937040007436176452688944747L);
289 inline T forth_root_epsilon_imp(const T*, const mpl::int_<53>&)
291 return static_cast<T>(0.0001220703125L);
295 inline T forth_root_epsilon_imp(const T*, const mpl::int_<64>&)
297 return static_cast<T>(0.18145860519450699870567321328132261891067079047605e-4L);
301 inline T forth_root_epsilon_imp(const T*, const mpl::int_<113>&)
303 return static_cast<T>(0.37252902984619140625e-8L);
306 template <class T, class Tag>
307 inline T forth_root_epsilon_imp(const T*, const Tag&)
310 static const T r_eps = sqrt(sqrt(tools::epsilon<T>()));
317 inline T root_epsilon()
319 typedef mpl::int_< (::std::numeric_limits<T>::radix == 2) ? std::numeric_limits<T>::digits : 0> tag_type;
320 return detail::root_epsilon_imp(static_cast<T const*>(0), tag_type());
324 inline T forth_root_epsilon()
326 typedef mpl::int_< (::std::numeric_limits<T>::radix == 2) ? std::numeric_limits<T>::digits : 0> tag_type;
327 return detail::forth_root_epsilon_imp(static_cast<T const*>(0), tag_type());
334 #endif // BOOST_MATH_TOOLS_PRECISION_INCLUDED