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>
23 // These two are for LDBL_MAN_DIG:
27 namespace boost{ namespace math
31 // If T is not specialized, the functions digits, max_value and min_value,
32 // all get synthesised automatically from std::numeric_limits.
33 // However, if numeric_limits is not specialised for type RealType,
34 // for example with NTL::RR type, then you will get a compiler error
35 // when code tries to use these functions, unless you explicitly specialise them.
37 // For example if the precision of RealType varies at runtime,
38 // then numeric_limits support may not be appropriate,
39 // see boost/math/tools/ntl.hpp for examples like
40 // template <> NTL::RR max_value<NTL::RR> ...
41 // See Conceptual Requirements for Real Number Types.
44 inline int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
46 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
47 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
49 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
51 return std::numeric_limits<T>::digits;
55 inline T max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
57 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
58 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
60 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
62 return (std::numeric_limits<T>::max)();
63 } // Also used as a finite 'infinite' value for - and +infinity, for example:
64 // -max_value<double> = -1.79769e+308, max_value<double> = 1.79769e+308.
67 inline T min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
69 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
70 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
72 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
74 return (std::numeric_limits<T>::min)();
79 // Logarithmic limits come next, note that although
80 // we can compute these from the log of the max value
81 // that is not in general thread safe (if we cache the value)
82 // so it's better to specialise these:
84 // For type float first:
87 inline T log_max_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
93 inline T log_min_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
101 inline T log_max_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
107 inline T log_min_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
112 // 80 and 128-bit long doubles:
115 inline T log_max_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
121 inline T log_min_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
127 inline T log_max_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
129 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
130 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
132 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
135 static const T val = log((std::numeric_limits<T>::max)());
140 inline T log_min_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
142 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
143 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
145 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
148 static const T val = log((std::numeric_limits<T>::max)());
153 inline T epsilon(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
155 return std::numeric_limits<T>::epsilon();
158 #if (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) && ((LDBL_MANT_DIG == 106) || (__LDBL_MANT_DIG__ == 106))
160 inline long double epsilon<long double>(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(long double))
162 // numeric_limits on Darwin tells lies here.
163 // This static assert fails for some unknown reason, so
164 // disabled for now...
165 // BOOST_STATIC_ASSERT(std::numeric_limits<long double>::digits == 106);
166 return 2.4651903288156618919116517665087e-32L;
171 inline T epsilon(const mpl::false_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
173 BOOST_MATH_STD_USING // for ADL of std names
174 static const T eps = ldexp(static_cast<T>(1), 1-policies::digits<T, policies::policy<> >());
178 } // namespace detail
181 inline T log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
183 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
184 typedef typename mpl::if_c<
185 std::numeric_limits<T>::max_exponent == 128
186 || std::numeric_limits<T>::max_exponent == 1024
187 || std::numeric_limits<T>::max_exponent == 16384,
188 mpl::int_<std::numeric_limits<T>::max_exponent>,
191 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
192 return detail::log_max_value<T>(tag_type());
194 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
196 static const T val = log((std::numeric_limits<T>::max)());
202 inline T log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
204 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
205 typedef typename mpl::if_c<
206 std::numeric_limits<T>::max_exponent == 128
207 || std::numeric_limits<T>::max_exponent == 1024
208 || std::numeric_limits<T>::max_exponent == 16384,
209 mpl::int_<std::numeric_limits<T>::max_exponent>,
213 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
214 return detail::log_min_value<T>(tag_type());
216 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
218 static const T val = log((std::numeric_limits<T>::min)());
224 inline T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
226 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
227 return detail::epsilon<T>(mpl::bool_< ::std::numeric_limits<T>::is_specialized>());
229 return ::std::numeric_limits<T>::is_specialized ?
230 detail::epsilon<T>(mpl::true_()) :
231 detail::epsilon<T>(mpl::false_());
238 inline T root_epsilon_imp(const mpl::int_<24>&)
240 return static_cast<T>(0.00034526698300124390839884978618400831996329879769945L);
244 inline T root_epsilon_imp(const T*, const mpl::int_<53>&)
246 return static_cast<T>(0.1490116119384765625e-7L);
250 inline T root_epsilon_imp(const T*, const mpl::int_<64>&)
252 return static_cast<T>(0.32927225399135962333569506281281311031656150598474e-9L);
256 inline T root_epsilon_imp(const T*, const mpl::int_<113>&)
258 return static_cast<T>(0.1387778780781445675529539585113525390625e-16L);
261 template <class T, class Tag>
262 inline T root_epsilon_imp(const T*, const Tag&)
265 static const T r_eps = sqrt(tools::epsilon<T>());
270 inline T forth_root_epsilon_imp(const T*, const mpl::int_<24>&)
272 return static_cast<T>(0.018581361171917516667460937040007436176452688944747L);
276 inline T forth_root_epsilon_imp(const T*, const mpl::int_<53>&)
278 return static_cast<T>(0.0001220703125L);
282 inline T forth_root_epsilon_imp(const T*, const mpl::int_<64>&)
284 return static_cast<T>(0.18145860519450699870567321328132261891067079047605e-4L);
288 inline T forth_root_epsilon_imp(const T*, const mpl::int_<113>&)
290 return static_cast<T>(0.37252902984619140625e-8L);
293 template <class T, class Tag>
294 inline T forth_root_epsilon_imp(const T*, const Tag&)
297 static const T r_eps = sqrt(sqrt(tools::epsilon<T>()));
304 inline T root_epsilon()
306 typedef mpl::int_<std::numeric_limits<T>::digits> tag_type;
307 return detail::root_epsilon_imp(static_cast<T const*>(0), tag_type());
311 inline T forth_root_epsilon()
313 typedef mpl::int_<std::numeric_limits<T>::digits> tag_type;
314 return detail::forth_root_epsilon_imp(static_cast<T const*>(0), tag_type());
321 #endif // BOOST_MATH_TOOLS_PRECISION_INCLUDED