]> git.donarmstrong.com Git - rsem.git/blob - boost/math/tools/precision.hpp
Updated boost to v1.55.0
[rsem.git] / boost / math / tools / precision.hpp
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
25 namespace boost{ namespace math
26 {
27 namespace 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
41 template <class T>
42 inline int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
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
56 template <class T>
57 inline T max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(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
68 template <class T>
69 inline T min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(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
79 namespace 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 //
88 template <class T>
89 inline T log_max_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
90 {
91    return 88.0f;
92 }
93
94 template <class T>
95 inline T log_min_value(const mpl::int_<128>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
96 {
97    return -87.0f;
98 }
99 //
100 // Now double:
101 //
102 template <class T>
103 inline T log_max_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
104 {
105    return 709.0;
106 }
107
108 template <class T>
109 inline T log_min_value(const mpl::int_<1024>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
110 {
111    return -708.0;
112 }
113 //
114 // 80 and 128-bit long doubles:
115 //
116 template <class T>
117 inline T log_max_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
118 {
119    return 11356.0L;
120 }
121
122 template <class T>
123 inline T log_min_value(const mpl::int_<16384>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
124 {
125    return -11355.0L;
126 }
127
128 template <class T>
129 inline T log_max_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
130 {
131 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
132    BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
133 #else
134    BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
135 #endif
136    BOOST_MATH_STD_USING
137    static const T val = log((std::numeric_limits<T>::max)());
138    return val;
139 }
140
141 template <class T>
142 inline T log_min_value(const mpl::int_<0>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
143 {
144 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
145    BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
146 #else
147    BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
148 #endif
149    BOOST_MATH_STD_USING
150    static const T val = log((std::numeric_limits<T>::min)());
151    return val;
152 }
153
154 template <class T>
155 inline T epsilon(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
156 {
157    return std::numeric_limits<T>::epsilon();
158 }
159
160 #if (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) && ((LDBL_MANT_DIG == 106) || (__LDBL_MANT_DIG__ == 106))
161 template <>
162 inline long double epsilon<long double>(const mpl::true_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(long double))
163 {
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;
169 }
170 #endif
171
172 template <class T>
173 inline T epsilon(const mpl::false_& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
174 {
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<> >());
177    return eps;
178 }
179
180 } // namespace detail
181
182 #ifdef BOOST_MSVC
183 #pragma warning(push)
184 #pragma warning(disable:4309)
185 #endif
186
187 template <class T>
188 inline T log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
189 {
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))>,
197       mpl::int_<0>
198    >::type tag_type;
199    BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
200    return detail::log_max_value<T>(tag_type());
201 #else
202    BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
203    BOOST_MATH_STD_USING
204    static const T val = log((std::numeric_limits<T>::max)());
205    return val;
206 #endif
207 }
208
209 template <class T>
210 inline T log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
211 {
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))>,
219       mpl::int_<0>
220    >::type tag_type;
221
222    BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
223    return detail::log_min_value<T>(tag_type());
224 #else
225    BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
226    BOOST_MATH_STD_USING
227    static const T val = log((std::numeric_limits<T>::min)());
228    return val;
229 #endif
230 }
231
232 #ifdef BOOST_MSVC
233 #pragma warning(pop)
234 #endif
235
236 template <class T>
237 inline T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
238 {
239 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
240    return detail::epsilon<T>(mpl::bool_< ::std::numeric_limits<T>::is_specialized>());
241 #else
242    return ::std::numeric_limits<T>::is_specialized ?
243       detail::epsilon<T>(mpl::true_()) :
244       detail::epsilon<T>(mpl::false_());
245 #endif
246 }
247
248 namespace detail{
249
250 template <class T>
251 inline T root_epsilon_imp(const mpl::int_<24>&)
252 {
253    return static_cast<T>(0.00034526698300124390839884978618400831996329879769945L);
254 }
255
256 template <class T>
257 inline T root_epsilon_imp(const T*, const mpl::int_<53>&)
258 {
259    return static_cast<T>(0.1490116119384765625e-7L);
260 }
261
262 template <class T>
263 inline T root_epsilon_imp(const T*, const mpl::int_<64>&)
264 {
265    return static_cast<T>(0.32927225399135962333569506281281311031656150598474e-9L);
266 }
267
268 template <class T>
269 inline T root_epsilon_imp(const T*, const mpl::int_<113>&)
270 {
271    return static_cast<T>(0.1387778780781445675529539585113525390625e-16L);
272 }
273
274 template <class T, class Tag>
275 inline T root_epsilon_imp(const T*, const Tag&)
276 {
277    BOOST_MATH_STD_USING
278    static const T r_eps = sqrt(tools::epsilon<T>());
279    return r_eps;
280 }
281
282 template <class T>
283 inline T forth_root_epsilon_imp(const T*, const mpl::int_<24>&)
284 {
285    return static_cast<T>(0.018581361171917516667460937040007436176452688944747L);
286 }
287
288 template <class T>
289 inline T forth_root_epsilon_imp(const T*, const mpl::int_<53>&)
290 {
291    return static_cast<T>(0.0001220703125L);
292 }
293
294 template <class T>
295 inline T forth_root_epsilon_imp(const T*, const mpl::int_<64>&)
296 {
297    return static_cast<T>(0.18145860519450699870567321328132261891067079047605e-4L);
298 }
299
300 template <class T>
301 inline T forth_root_epsilon_imp(const T*, const mpl::int_<113>&)
302 {
303    return static_cast<T>(0.37252902984619140625e-8L);
304 }
305
306 template <class T, class Tag>
307 inline T forth_root_epsilon_imp(const T*, const Tag&)
308 {
309    BOOST_MATH_STD_USING
310    static const T r_eps = sqrt(sqrt(tools::epsilon<T>()));
311    return r_eps;
312 }
313
314 }
315
316 template <class T>
317 inline T root_epsilon()
318 {
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());
321 }
322
323 template <class T>
324 inline T forth_root_epsilon()
325 {
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());
328 }
329
330 } // namespace tools
331 } // namespace math
332 } // namespace boost
333
334 #endif // BOOST_MATH_TOOLS_PRECISION_INCLUDED
335