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)
5
6#ifndef BOOST_MATH_TOOLS_PRECISION_INCLUDED
7#define BOOST_MATH_TOOLS_PRECISION_INCLUDED
8
9#ifdef _MSC_VER
10#pragma once
11#endif
12
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>
20
21// These two are for LDBL_MAN_DIG:
22#include <limits.h>
23#include <math.h>
24
25namespace boost{ namespace math
26{
27namespace tools
28{
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.
34
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.
40
41template <class T>
42inline BOOST_MATH_CONSTEXPR int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) BOOST_NOEXCEPT
43{
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);
47#else
48 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
49 BOOST_ASSERT(::std::numeric_limits<T>::radix == 2 || ::std::numeric_limits<T>::radix == 10);
50#endif
51 return std::numeric_limits<T>::radix == 2
52 ? std::numeric_limits<T>::digits
53 : ((std::numeric_limits<T>::digits + 1) * 1000L) / 301L;
54}
55
56template <class T>
57inline BOOST_MATH_CONSTEXPR T max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
58{
59#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
60 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
61#else
62 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
63#endif
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.
67
68template <class T>
69inline BOOST_MATH_CONSTEXPR T min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
70{
71#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
72 BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
73#else
74 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
75#endif
76 return (std::numeric_limits<T>::min)();
77}
78
79namespace detail{
80//
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:
85//
86// For type float first:
87//
88template <class T>
89inline BOOST_MATH_CONSTEXPR T log_max_value(const boost::integral_constant<int, 128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
90{
91 return 88.0f;
92}
93
94template <class T>
95inline BOOST_MATH_CONSTEXPR T log_min_value(const boost::integral_constant<int, 128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
96{
97 return -87.0f;
98}
99//
100// Now double:
101//
102template <class T>
103inline BOOST_MATH_CONSTEXPR T log_max_value(const boost::integral_constant<int, 1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
104{
105 return 709.0;
106}
107
108template <class T>
109inline BOOST_MATH_CONSTEXPR T log_min_value(const boost::integral_constant<int, 1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
110{
111 return -708.0;
112}
113//
114// 80 and 128-bit long doubles:
115//
116template <class T>
117inline BOOST_MATH_CONSTEXPR T log_max_value(const boost::integral_constant<int, 16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
118{
119 return 11356.0L;
120}
121
122template <class T>
123inline BOOST_MATH_CONSTEXPR T log_min_value(const boost::integral_constant<int, 16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
124{
125 return -11355.0L;
126}
127
128template <class T>
129inline T log_max_value(const boost::integral_constant<int, 0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
130{
131 BOOST_MATH_STD_USING
132#ifdef __SUNPRO_CC
133 static const T m = boost::math::tools::max_value<T>();
134 static const T val = log(m);
135#else
136 static const T val = log(boost::math::tools::max_value<T>());
137#endif
138 return val;
139}
140
141template <class T>
142inline T log_min_value(const boost::integral_constant<int, 0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
143{
144 BOOST_MATH_STD_USING
145#ifdef __SUNPRO_CC
146 static const T m = boost::math::tools::min_value<T>();
147 static const T val = log(m);
148#else
149 static const T val = log(boost::math::tools::min_value<T>());
150#endif
151 return val;
152}
153
154template <class T>
155inline BOOST_MATH_CONSTEXPR T epsilon(const boost::true_type& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_MATH_NOEXCEPT(T)
156{
157 return std::numeric_limits<T>::epsilon();
158}
159
160#if defined(__GNUC__) && ((LDBL_MANT_DIG == 106) || (__LDBL_MANT_DIG__ == 106))
161template <>
162inline BOOST_MATH_CONSTEXPR long double epsilon<long double>(const boost::true_type& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(long double)) BOOST_MATH_NOEXCEPT(long double)
163{
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.
172 //
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;
177}
178#endif
179
180template <class T>
181inline T epsilon(const boost::false_type& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
182{
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<> >());
186}
187
188template <class T>
189struct log_limit_traits
190{
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 boost::integral_constant<int, (std::numeric_limits<T>::max_exponent > INT_MAX ? INT_MAX : static_cast<int>(std::numeric_limits<T>::max_exponent))>,
197 boost::integral_constant<int, 0>
198 >::type tag_type;
199 BOOST_STATIC_CONSTANT(bool, value = tag_type::value ? true : false);
200 BOOST_STATIC_ASSERT(::std::numeric_limits<T>::is_specialized || (value == 0));
201};
202
203template <class T, bool b> struct log_limit_noexcept_traits_imp : public log_limit_traits<T> {};
204template <class T> struct log_limit_noexcept_traits_imp<T, false> : public boost::integral_constant<bool, false> {};
205
206template <class T>
207struct log_limit_noexcept_traits : public log_limit_noexcept_traits_imp<T, BOOST_MATH_IS_FLOAT(T)> {};
208
209} // namespace detail
210
211#ifdef BOOST_MSVC
212#pragma warning(push)
213#pragma warning(disable:4309)
214#endif
215
216template <class T>
217inline BOOST_MATH_CONSTEXPR T log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_NOEXCEPT_IF(detail::log_limit_noexcept_traits<T>::value)
218{
219#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
220 return detail::log_max_value<T>(typename detail::log_limit_traits<T>::tag_type());
221#else
222 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
223 BOOST_MATH_STD_USING
224 static const T val = log((std::numeric_limits<T>::max)());
225 return val;
226#endif
227}
228
229template <class T>
230inline BOOST_MATH_CONSTEXPR T log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T)) BOOST_NOEXCEPT_IF(detail::log_limit_noexcept_traits<T>::value)
231{
232#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
233 return detail::log_min_value<T>(typename detail::log_limit_traits<T>::tag_type());
234#else
235 BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
236 BOOST_MATH_STD_USING
237 static const T val = log((std::numeric_limits<T>::min)());
238 return val;
239#endif
240}
241
242#ifdef BOOST_MSVC
243#pragma warning(pop)
244#endif
245
246template <class T>
247inline BOOST_MATH_CONSTEXPR T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) BOOST_MATH_NOEXCEPT(T)
248{
249#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
250 return detail::epsilon<T>(boost::integral_constant<bool, ::std::numeric_limits<T>::is_specialized>());
251#else
252 return ::std::numeric_limits<T>::is_specialized ?
253 detail::epsilon<T>(boost::true_type()) :
254 detail::epsilon<T>(boost::false_type());
255#endif
256}
257
258namespace detail{
259
260template <class T>
261inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const boost::integral_constant<int, 24>&) BOOST_MATH_NOEXCEPT(T)
262{
263 return static_cast<T>(0.00034526698300124390839884978618400831996329879769945L);
264}
265
266template <class T>
267inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const T*, const boost::integral_constant<int, 53>&) BOOST_MATH_NOEXCEPT(T)
268{
269 return static_cast<T>(0.1490116119384765625e-7L);
270}
271
272template <class T>
273inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const T*, const boost::integral_constant<int, 64>&) BOOST_MATH_NOEXCEPT(T)
274{
275 return static_cast<T>(0.32927225399135962333569506281281311031656150598474e-9L);
276}
277
278template <class T>
279inline BOOST_MATH_CONSTEXPR T root_epsilon_imp(const T*, const boost::integral_constant<int, 113>&) BOOST_MATH_NOEXCEPT(T)
280{
281 return static_cast<T>(0.1387778780781445675529539585113525390625e-16L);
282}
283
284template <class T, class Tag>
285inline T root_epsilon_imp(const T*, const Tag&)
286{
287 BOOST_MATH_STD_USING
288 static const T r_eps = sqrt(tools::epsilon<T>());
289 return r_eps;
290}
291
292template <class T>
293inline T root_epsilon_imp(const T*, const boost::integral_constant<int, 0>&)
294{
295 BOOST_MATH_STD_USING
296 return sqrt(tools::epsilon<T>());
297}
298
299template <class T>
300inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const boost::integral_constant<int, 24>&) BOOST_MATH_NOEXCEPT(T)
301{
302 return static_cast<T>(0.0049215666011518482998719164346805794944150447839903L);
303}
304
305template <class T>
306inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const T*, const boost::integral_constant<int, 53>&) BOOST_MATH_NOEXCEPT(T)
307{
308 return static_cast<T>(6.05545445239333906078989272793696693569753008995e-6L);
309}
310
311template <class T>
312inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const T*, const boost::integral_constant<int, 64>&) BOOST_MATH_NOEXCEPT(T)
313{
314 return static_cast<T>(4.76837158203125e-7L);
315}
316
317template <class T>
318inline BOOST_MATH_CONSTEXPR T cbrt_epsilon_imp(const T*, const boost::integral_constant<int, 113>&) BOOST_MATH_NOEXCEPT(T)
319{
320 return static_cast<T>(5.7749313854154005630396773604745549542403508090496e-12L);
321}
322
323template <class T, class Tag>
324inline T cbrt_epsilon_imp(const T*, const Tag&)
325{
326 BOOST_MATH_STD_USING;
327 static const T cbrt_eps = pow(tools::epsilon<T>(), T(1) / 3);
328 return cbrt_eps;
329}
330
331template <class T>
332inline T cbrt_epsilon_imp(const T*, const boost::integral_constant<int, 0>&)
333{
334 BOOST_MATH_STD_USING;
335 return pow(tools::epsilon<T>(), T(1) / 3);
336}
337
338template <class T>
339inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const boost::integral_constant<int, 24>&) BOOST_MATH_NOEXCEPT(T)
340{
341 return static_cast<T>(0.018581361171917516667460937040007436176452688944747L);
342}
343
344template <class T>
345inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const boost::integral_constant<int, 53>&) BOOST_MATH_NOEXCEPT(T)
346{
347 return static_cast<T>(0.0001220703125L);
348}
349
350template <class T>
351inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const boost::integral_constant<int, 64>&) BOOST_MATH_NOEXCEPT(T)
352{
353 return static_cast<T>(0.18145860519450699870567321328132261891067079047605e-4L);
354}
355
356template <class T>
357inline BOOST_MATH_CONSTEXPR T forth_root_epsilon_imp(const T*, const boost::integral_constant<int, 113>&) BOOST_MATH_NOEXCEPT(T)
358{
359 return static_cast<T>(0.37252902984619140625e-8L);
360}
361
362template <class T, class Tag>
363inline T forth_root_epsilon_imp(const T*, const Tag&)
364{
365 BOOST_MATH_STD_USING
366 static const T r_eps = sqrt(sqrt(tools::epsilon<T>()));
367 return r_eps;
368}
369
370template <class T>
371inline T forth_root_epsilon_imp(const T*, const boost::integral_constant<int, 0>&)
372{
373 BOOST_MATH_STD_USING
374 return sqrt(sqrt(tools::epsilon<T>()));
375}
376
377template <class T>
378struct root_epsilon_traits
379{
380 typedef boost::integral_constant<int, (::std::numeric_limits<T>::radix == 2) && (::std::numeric_limits<T>::digits != INT_MAX) ? 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));
382};
383
384}
385
386template <class T>
387inline BOOST_MATH_CONSTEXPR T root_epsilon() BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && detail::root_epsilon_traits<T>::has_noexcept)
388{
389 return detail::root_epsilon_imp(static_cast<T const*>(0), typename detail::root_epsilon_traits<T>::tag_type());
390}
391
392template <class T>
393inline BOOST_MATH_CONSTEXPR T cbrt_epsilon() BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && detail::root_epsilon_traits<T>::has_noexcept)
394{
395 return detail::cbrt_epsilon_imp(static_cast<T const*>(0), typename detail::root_epsilon_traits<T>::tag_type());
396}
397
398template <class T>
399inline BOOST_MATH_CONSTEXPR T forth_root_epsilon() BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && detail::root_epsilon_traits<T>::has_noexcept)
400{
401 return detail::forth_root_epsilon_imp(static_cast<T const*>(0), typename detail::root_epsilon_traits<T>::tag_type());
402}
403
404} // namespace tools
405} // namespace math
406} // namespace boost
407
408#endif // BOOST_MATH_TOOLS_PRECISION_INCLUDED
409
410

source code of include/boost/math/tools/precision.hpp