policy.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. // Copyright John Maddock 2007.
  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. #ifndef BOOST_MATH_POLICY_HPP
  6. #define BOOST_MATH_POLICY_HPP
  7. #include <boost/mpl/list.hpp>
  8. #include <boost/mpl/contains.hpp>
  9. #include <boost/mpl/if.hpp>
  10. #include <boost/mpl/find_if.hpp>
  11. #include <boost/mpl/remove_if.hpp>
  12. #include <boost/mpl/vector.hpp>
  13. #include <boost/mpl/push_back.hpp>
  14. #include <boost/mpl/at.hpp>
  15. #include <boost/mpl/size.hpp>
  16. #include <boost/mpl/comparison.hpp>
  17. #include <boost/type_traits/is_same.hpp>
  18. #include <boost/static_assert.hpp>
  19. #include <boost/assert.hpp>
  20. #include <boost/math/tools/config.hpp>
  21. #include <limits>
  22. // Sadly we do need the .h versions of these to be sure of getting
  23. // FLT_MANT_DIG etc.
  24. #include <limits.h>
  25. #include <stdlib.h>
  26. #include <stddef.h>
  27. #include <math.h>
  28. namespace boost{ namespace math{
  29. namespace tools{
  30. template <class T>
  31. int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T));
  32. template <class T>
  33. T epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T));
  34. }
  35. namespace policies{
  36. //
  37. // Define macros for our default policies, if they're not defined already:
  38. //
  39. #ifndef BOOST_MATH_DOMAIN_ERROR_POLICY
  40. #define BOOST_MATH_DOMAIN_ERROR_POLICY throw_on_error
  41. #endif
  42. #ifndef BOOST_MATH_POLE_ERROR_POLICY
  43. #define BOOST_MATH_POLE_ERROR_POLICY throw_on_error
  44. #endif
  45. #ifndef BOOST_MATH_OVERFLOW_ERROR_POLICY
  46. #define BOOST_MATH_OVERFLOW_ERROR_POLICY throw_on_error
  47. #endif
  48. #ifndef BOOST_MATH_EVALUATION_ERROR_POLICY
  49. #define BOOST_MATH_EVALUATION_ERROR_POLICY throw_on_error
  50. #endif
  51. #ifndef BOOST_MATH_ROUNDING_ERROR_POLICY
  52. #define BOOST_MATH_ROUNDING_ERROR_POLICY throw_on_error
  53. #endif
  54. #ifndef BOOST_MATH_UNDERFLOW_ERROR_POLICY
  55. #define BOOST_MATH_UNDERFLOW_ERROR_POLICY ignore_error
  56. #endif
  57. #ifndef BOOST_MATH_DENORM_ERROR_POLICY
  58. #define BOOST_MATH_DENORM_ERROR_POLICY ignore_error
  59. #endif
  60. #ifndef BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY
  61. #define BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY ignore_error
  62. #endif
  63. #ifndef BOOST_MATH_DIGITS10_POLICY
  64. #define BOOST_MATH_DIGITS10_POLICY 0
  65. #endif
  66. #ifndef BOOST_MATH_PROMOTE_FLOAT_POLICY
  67. #define BOOST_MATH_PROMOTE_FLOAT_POLICY true
  68. #endif
  69. #ifndef BOOST_MATH_PROMOTE_DOUBLE_POLICY
  70. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  71. #define BOOST_MATH_PROMOTE_DOUBLE_POLICY false
  72. #else
  73. #define BOOST_MATH_PROMOTE_DOUBLE_POLICY true
  74. #endif
  75. #endif
  76. #ifndef BOOST_MATH_DISCRETE_QUANTILE_POLICY
  77. #define BOOST_MATH_DISCRETE_QUANTILE_POLICY integer_round_outwards
  78. #endif
  79. #ifndef BOOST_MATH_ASSERT_UNDEFINED_POLICY
  80. #define BOOST_MATH_ASSERT_UNDEFINED_POLICY true
  81. #endif
  82. #ifndef BOOST_MATH_MAX_SERIES_ITERATION_POLICY
  83. #define BOOST_MATH_MAX_SERIES_ITERATION_POLICY 1000000
  84. #endif
  85. #ifndef BOOST_MATH_MAX_ROOT_ITERATION_POLICY
  86. #define BOOST_MATH_MAX_ROOT_ITERATION_POLICY 200
  87. #endif
  88. #if !defined(__BORLANDC__) \
  89. && !(defined(__GNUC__) && (__GNUC__ == 3) && (__GNUC_MINOR__ <= 2))
  90. #define BOOST_MATH_META_INT(type, name, Default)\
  91. template <type N = Default> struct name : public boost::mpl::int_<N>{};\
  92. namespace detail{\
  93. template <type N>\
  94. char test_is_valid_arg(const name<N>*);\
  95. char test_is_default_arg(const name<Default>*);\
  96. template <class T> struct is_##name##_imp\
  97. {\
  98. template <type N> static char test(const name<N>*);\
  99. static double test(...);\
  100. BOOST_STATIC_CONSTANT(bool, value = sizeof(test(static_cast<T*>(0))) == 1);\
  101. };\
  102. }\
  103. template <class T> struct is_##name : public boost::mpl::bool_< ::boost::math::policies::detail::is_##name##_imp<T>::value>{};
  104. #define BOOST_MATH_META_BOOL(name, Default)\
  105. template <bool N = Default> struct name : public boost::mpl::bool_<N>{};\
  106. namespace detail{\
  107. template <bool N>\
  108. char test_is_valid_arg(const name<N>*);\
  109. char test_is_default_arg(const name<Default>*);\
  110. template <class T> struct is_##name##_imp\
  111. {\
  112. template <bool N> static char test(const name<N>*);\
  113. static double test(...);\
  114. BOOST_STATIC_CONSTANT(bool, value = sizeof(test(static_cast<T*>(0))) == 1);\
  115. };\
  116. }\
  117. template <class T> struct is_##name : public boost::mpl::bool_< ::boost::math::policies::detail::is_##name##_imp<T>::value>{};
  118. #else
  119. #define BOOST_MATH_META_INT(Type, name, Default)\
  120. template <Type N = Default> struct name : public boost::mpl::int_<N>{};\
  121. namespace detail{\
  122. template <Type N>\
  123. char test_is_valid_arg(const name<N>*);\
  124. char test_is_default_arg(const name<Default>*);\
  125. template <class T> struct is_##name##_tester\
  126. {\
  127. template <Type N> static char test(const name<N>&);\
  128. static double test(...);\
  129. };\
  130. template <class T> struct is_##name##_imp\
  131. {\
  132. static T inst;\
  133. BOOST_STATIC_CONSTANT(bool, value = sizeof( ::boost::math::policies::detail::is_##name##_tester<T>::test(inst)) == 1);\
  134. };\
  135. }\
  136. template <class T> struct is_##name : public boost::mpl::bool_< ::boost::math::policies::detail::is_##name##_imp<T>::value>\
  137. {\
  138. template <class U> struct apply{ typedef is_##name<U> type; };\
  139. };
  140. #define BOOST_MATH_META_BOOL(name, Default)\
  141. template <bool N = Default> struct name : public boost::mpl::bool_<N>{};\
  142. namespace detail{\
  143. template <bool N>\
  144. char test_is_valid_arg(const name<N>*);\
  145. char test_is_default_arg(const name<Default>*);\
  146. template <class T> struct is_##name##_tester\
  147. {\
  148. template <bool N> static char test(const name<N>&);\
  149. static double test(...);\
  150. };\
  151. template <class T> struct is_##name##_imp\
  152. {\
  153. static T inst;\
  154. BOOST_STATIC_CONSTANT(bool, value = sizeof( ::boost::math::policies::detail::is_##name##_tester<T>::test(inst)) == 1);\
  155. };\
  156. }\
  157. template <class T> struct is_##name : public boost::mpl::bool_< ::boost::math::policies::detail::is_##name##_imp<T>::value>\
  158. {\
  159. template <class U> struct apply{ typedef is_##name<U> type; };\
  160. };
  161. #endif
  162. //
  163. // Begin by defining policy types for error handling:
  164. //
  165. enum error_policy_type
  166. {
  167. throw_on_error = 0,
  168. errno_on_error = 1,
  169. ignore_error = 2,
  170. user_error = 3
  171. };
  172. BOOST_MATH_META_INT(error_policy_type, domain_error, BOOST_MATH_DOMAIN_ERROR_POLICY)
  173. BOOST_MATH_META_INT(error_policy_type, pole_error, BOOST_MATH_POLE_ERROR_POLICY)
  174. BOOST_MATH_META_INT(error_policy_type, overflow_error, BOOST_MATH_OVERFLOW_ERROR_POLICY)
  175. BOOST_MATH_META_INT(error_policy_type, underflow_error, BOOST_MATH_UNDERFLOW_ERROR_POLICY)
  176. BOOST_MATH_META_INT(error_policy_type, denorm_error, BOOST_MATH_DENORM_ERROR_POLICY)
  177. BOOST_MATH_META_INT(error_policy_type, evaluation_error, BOOST_MATH_EVALUATION_ERROR_POLICY)
  178. BOOST_MATH_META_INT(error_policy_type, rounding_error, BOOST_MATH_ROUNDING_ERROR_POLICY)
  179. BOOST_MATH_META_INT(error_policy_type, indeterminate_result_error, BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY)
  180. //
  181. // Policy types for internal promotion:
  182. //
  183. BOOST_MATH_META_BOOL(promote_float, BOOST_MATH_PROMOTE_FLOAT_POLICY)
  184. BOOST_MATH_META_BOOL(promote_double, BOOST_MATH_PROMOTE_DOUBLE_POLICY)
  185. BOOST_MATH_META_BOOL(assert_undefined, BOOST_MATH_ASSERT_UNDEFINED_POLICY)
  186. //
  187. // Policy types for discrete quantiles:
  188. //
  189. enum discrete_quantile_policy_type
  190. {
  191. real,
  192. integer_round_outwards,
  193. integer_round_inwards,
  194. integer_round_down,
  195. integer_round_up,
  196. integer_round_nearest
  197. };
  198. BOOST_MATH_META_INT(discrete_quantile_policy_type, discrete_quantile, BOOST_MATH_DISCRETE_QUANTILE_POLICY)
  199. //
  200. // Precision:
  201. //
  202. BOOST_MATH_META_INT(int, digits10, BOOST_MATH_DIGITS10_POLICY)
  203. BOOST_MATH_META_INT(int, digits2, 0)
  204. //
  205. // Iterations:
  206. //
  207. BOOST_MATH_META_INT(unsigned long, max_series_iterations, BOOST_MATH_MAX_SERIES_ITERATION_POLICY)
  208. BOOST_MATH_META_INT(unsigned long, max_root_iterations, BOOST_MATH_MAX_ROOT_ITERATION_POLICY)
  209. //
  210. // Define the names for each possible policy:
  211. //
  212. #define BOOST_MATH_PARAMETER(name)\
  213. BOOST_PARAMETER_TEMPLATE_KEYWORD(name##_name)\
  214. BOOST_PARAMETER_NAME(name##_name)
  215. struct default_policy{};
  216. namespace detail{
  217. //
  218. // Trait to work out bits precision from digits10 and digits2:
  219. //
  220. template <class Digits10, class Digits2>
  221. struct precision
  222. {
  223. //
  224. // Now work out the precision:
  225. //
  226. typedef typename mpl::if_c<
  227. (Digits10::value == 0),
  228. digits2<0>,
  229. digits2<((Digits10::value + 1) * 1000L) / 301L>
  230. >::type digits2_type;
  231. public:
  232. #ifdef __BORLANDC__
  233. typedef typename mpl::if_c<
  234. (Digits2::value > ::boost::math::policies::detail::precision<Digits10,Digits2>::digits2_type::value),
  235. Digits2, digits2_type>::type type;
  236. #else
  237. typedef typename mpl::if_c<
  238. (Digits2::value > digits2_type::value),
  239. Digits2, digits2_type>::type type;
  240. #endif
  241. };
  242. template <class A, class B, bool b>
  243. struct select_result
  244. {
  245. typedef A type;
  246. };
  247. template <class A, class B>
  248. struct select_result<A, B, false>
  249. {
  250. typedef typename mpl::deref<B>::type type;
  251. };
  252. template <class Seq, class Pred, class DefaultType>
  253. struct find_arg
  254. {
  255. private:
  256. typedef typename mpl::find_if<Seq, Pred>::type iter;
  257. typedef typename mpl::end<Seq>::type end_type;
  258. public:
  259. typedef typename select_result<
  260. DefaultType, iter,
  261. ::boost::is_same<iter, end_type>::value>::type type;
  262. };
  263. double test_is_valid_arg(...);
  264. double test_is_default_arg(...);
  265. char test_is_valid_arg(const default_policy*);
  266. char test_is_default_arg(const default_policy*);
  267. template <class T>
  268. struct is_valid_policy_imp
  269. {
  270. BOOST_STATIC_CONSTANT(bool, value = sizeof(::boost::math::policies::detail::test_is_valid_arg(static_cast<T*>(0))) == 1);
  271. };
  272. template <class T>
  273. struct is_default_policy_imp
  274. {
  275. BOOST_STATIC_CONSTANT(bool, value = sizeof(::boost::math::policies::detail::test_is_default_arg(static_cast<T*>(0))) == 1);
  276. };
  277. template <class T> struct is_valid_policy
  278. : public mpl::bool_<
  279. ::boost::math::policies::detail::is_valid_policy_imp<T>::value>
  280. {};
  281. template <class T> struct is_default_policy
  282. : public mpl::bool_<
  283. ::boost::math::policies::detail::is_default_policy_imp<T>::value>
  284. {
  285. template <class U>
  286. struct apply
  287. {
  288. typedef is_default_policy<U> type;
  289. };
  290. };
  291. template <class Seq, class T, int N>
  292. struct append_N
  293. {
  294. typedef typename mpl::push_back<Seq, T>::type new_seq;
  295. typedef typename append_N<new_seq, T, N-1>::type type;
  296. };
  297. template <class Seq, class T>
  298. struct append_N<Seq, T, 0>
  299. {
  300. typedef Seq type;
  301. };
  302. //
  303. // Traits class to work out what template parameters our default
  304. // policy<> class will have when modified for forwarding:
  305. //
  306. template <bool f, bool d>
  307. struct default_args
  308. {
  309. typedef promote_float<false> arg1;
  310. typedef promote_double<false> arg2;
  311. };
  312. template <>
  313. struct default_args<false, false>
  314. {
  315. typedef default_policy arg1;
  316. typedef default_policy arg2;
  317. };
  318. template <>
  319. struct default_args<true, false>
  320. {
  321. typedef promote_float<false> arg1;
  322. typedef default_policy arg2;
  323. };
  324. template <>
  325. struct default_args<false, true>
  326. {
  327. typedef promote_double<false> arg1;
  328. typedef default_policy arg2;
  329. };
  330. typedef default_args<BOOST_MATH_PROMOTE_FLOAT_POLICY, BOOST_MATH_PROMOTE_DOUBLE_POLICY>::arg1 forwarding_arg1;
  331. typedef default_args<BOOST_MATH_PROMOTE_FLOAT_POLICY, BOOST_MATH_PROMOTE_DOUBLE_POLICY>::arg2 forwarding_arg2;
  332. } // detail
  333. //
  334. // Now define the policy type with enough arguments to handle all
  335. // the policies:
  336. //
  337. template <class A1 = default_policy,
  338. class A2 = default_policy,
  339. class A3 = default_policy,
  340. class A4 = default_policy,
  341. class A5 = default_policy,
  342. class A6 = default_policy,
  343. class A7 = default_policy,
  344. class A8 = default_policy,
  345. class A9 = default_policy,
  346. class A10 = default_policy,
  347. class A11 = default_policy,
  348. class A12 = default_policy,
  349. class A13 = default_policy>
  350. struct policy
  351. {
  352. private:
  353. //
  354. // Validate all our arguments:
  355. //
  356. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A1>::value);
  357. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A2>::value);
  358. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A3>::value);
  359. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A4>::value);
  360. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A5>::value);
  361. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A6>::value);
  362. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A7>::value);
  363. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A8>::value);
  364. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A9>::value);
  365. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A10>::value);
  366. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A11>::value);
  367. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A12>::value);
  368. BOOST_STATIC_ASSERT(::boost::math::policies::detail::is_valid_policy<A13>::value);
  369. //
  370. // Typelist of the arguments:
  371. //
  372. typedef mpl::list<A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13> arg_list;
  373. public:
  374. typedef typename detail::find_arg<arg_list, is_domain_error<mpl::_1>, domain_error<> >::type domain_error_type;
  375. typedef typename detail::find_arg<arg_list, is_pole_error<mpl::_1>, pole_error<> >::type pole_error_type;
  376. typedef typename detail::find_arg<arg_list, is_overflow_error<mpl::_1>, overflow_error<> >::type overflow_error_type;
  377. typedef typename detail::find_arg<arg_list, is_underflow_error<mpl::_1>, underflow_error<> >::type underflow_error_type;
  378. typedef typename detail::find_arg<arg_list, is_denorm_error<mpl::_1>, denorm_error<> >::type denorm_error_type;
  379. typedef typename detail::find_arg<arg_list, is_evaluation_error<mpl::_1>, evaluation_error<> >::type evaluation_error_type;
  380. typedef typename detail::find_arg<arg_list, is_rounding_error<mpl::_1>, rounding_error<> >::type rounding_error_type;
  381. typedef typename detail::find_arg<arg_list, is_indeterminate_result_error<mpl::_1>, indeterminate_result_error<> >::type indeterminate_result_error_type;
  382. private:
  383. //
  384. // Now work out the precision:
  385. //
  386. typedef typename detail::find_arg<arg_list, is_digits10<mpl::_1>, digits10<> >::type digits10_type;
  387. typedef typename detail::find_arg<arg_list, is_digits2<mpl::_1>, digits2<> >::type bits_precision_type;
  388. public:
  389. typedef typename detail::precision<digits10_type, bits_precision_type>::type precision_type;
  390. //
  391. // Internal promotion:
  392. //
  393. typedef typename detail::find_arg<arg_list, is_promote_float<mpl::_1>, promote_float<> >::type promote_float_type;
  394. typedef typename detail::find_arg<arg_list, is_promote_double<mpl::_1>, promote_double<> >::type promote_double_type;
  395. //
  396. // Discrete quantiles:
  397. //
  398. typedef typename detail::find_arg<arg_list, is_discrete_quantile<mpl::_1>, discrete_quantile<> >::type discrete_quantile_type;
  399. //
  400. // Mathematically undefined properties:
  401. //
  402. typedef typename detail::find_arg<arg_list, is_assert_undefined<mpl::_1>, assert_undefined<> >::type assert_undefined_type;
  403. //
  404. // Max iterations:
  405. //
  406. typedef typename detail::find_arg<arg_list, is_max_series_iterations<mpl::_1>, max_series_iterations<> >::type max_series_iterations_type;
  407. typedef typename detail::find_arg<arg_list, is_max_root_iterations<mpl::_1>, max_root_iterations<> >::type max_root_iterations_type;
  408. };
  409. //
  410. // These full specializations are defined to reduce the amount of
  411. // template instantiations that have to take place when using the default
  412. // policies, they have quite a large impact on compile times:
  413. //
  414. template <>
  415. struct policy<default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy>
  416. {
  417. public:
  418. typedef domain_error<> domain_error_type;
  419. typedef pole_error<> pole_error_type;
  420. typedef overflow_error<> overflow_error_type;
  421. typedef underflow_error<> underflow_error_type;
  422. typedef denorm_error<> denorm_error_type;
  423. typedef evaluation_error<> evaluation_error_type;
  424. typedef rounding_error<> rounding_error_type;
  425. typedef indeterminate_result_error<> indeterminate_result_error_type;
  426. #if BOOST_MATH_DIGITS10_POLICY == 0
  427. typedef digits2<> precision_type;
  428. #else
  429. typedef detail::precision<digits10<>, digits2<> >::type precision_type;
  430. #endif
  431. typedef promote_float<> promote_float_type;
  432. typedef promote_double<> promote_double_type;
  433. typedef discrete_quantile<> discrete_quantile_type;
  434. typedef assert_undefined<> assert_undefined_type;
  435. typedef max_series_iterations<> max_series_iterations_type;
  436. typedef max_root_iterations<> max_root_iterations_type;
  437. };
  438. template <>
  439. struct policy<detail::forwarding_arg1, detail::forwarding_arg2, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy, default_policy>
  440. {
  441. public:
  442. typedef domain_error<> domain_error_type;
  443. typedef pole_error<> pole_error_type;
  444. typedef overflow_error<> overflow_error_type;
  445. typedef underflow_error<> underflow_error_type;
  446. typedef denorm_error<> denorm_error_type;
  447. typedef evaluation_error<> evaluation_error_type;
  448. typedef rounding_error<> rounding_error_type;
  449. typedef indeterminate_result_error<> indeterminate_result_error_type;
  450. #if BOOST_MATH_DIGITS10_POLICY == 0
  451. typedef digits2<> precision_type;
  452. #else
  453. typedef detail::precision<digits10<>, digits2<> >::type precision_type;
  454. #endif
  455. typedef promote_float<false> promote_float_type;
  456. typedef promote_double<false> promote_double_type;
  457. typedef discrete_quantile<> discrete_quantile_type;
  458. typedef assert_undefined<> assert_undefined_type;
  459. typedef max_series_iterations<> max_series_iterations_type;
  460. typedef max_root_iterations<> max_root_iterations_type;
  461. };
  462. template <class Policy,
  463. class A1 = default_policy,
  464. class A2 = default_policy,
  465. class A3 = default_policy,
  466. class A4 = default_policy,
  467. class A5 = default_policy,
  468. class A6 = default_policy,
  469. class A7 = default_policy,
  470. class A8 = default_policy,
  471. class A9 = default_policy,
  472. class A10 = default_policy,
  473. class A11 = default_policy,
  474. class A12 = default_policy,
  475. class A13 = default_policy>
  476. struct normalise
  477. {
  478. private:
  479. typedef mpl::list<A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13> arg_list;
  480. typedef typename detail::find_arg<arg_list, is_domain_error<mpl::_1>, typename Policy::domain_error_type >::type domain_error_type;
  481. typedef typename detail::find_arg<arg_list, is_pole_error<mpl::_1>, typename Policy::pole_error_type >::type pole_error_type;
  482. typedef typename detail::find_arg<arg_list, is_overflow_error<mpl::_1>, typename Policy::overflow_error_type >::type overflow_error_type;
  483. typedef typename detail::find_arg<arg_list, is_underflow_error<mpl::_1>, typename Policy::underflow_error_type >::type underflow_error_type;
  484. typedef typename detail::find_arg<arg_list, is_denorm_error<mpl::_1>, typename Policy::denorm_error_type >::type denorm_error_type;
  485. typedef typename detail::find_arg<arg_list, is_evaluation_error<mpl::_1>, typename Policy::evaluation_error_type >::type evaluation_error_type;
  486. typedef typename detail::find_arg<arg_list, is_rounding_error<mpl::_1>, typename Policy::rounding_error_type >::type rounding_error_type;
  487. typedef typename detail::find_arg<arg_list, is_indeterminate_result_error<mpl::_1>, typename Policy::indeterminate_result_error_type >::type indeterminate_result_error_type;
  488. //
  489. // Now work out the precision:
  490. //
  491. typedef typename detail::find_arg<arg_list, is_digits10<mpl::_1>, digits10<> >::type digits10_type;
  492. typedef typename detail::find_arg<arg_list, is_digits2<mpl::_1>, typename Policy::precision_type >::type bits_precision_type;
  493. typedef typename detail::precision<digits10_type, bits_precision_type>::type precision_type;
  494. //
  495. // Internal promotion:
  496. //
  497. typedef typename detail::find_arg<arg_list, is_promote_float<mpl::_1>, typename Policy::promote_float_type >::type promote_float_type;
  498. typedef typename detail::find_arg<arg_list, is_promote_double<mpl::_1>, typename Policy::promote_double_type >::type promote_double_type;
  499. //
  500. // Discrete quantiles:
  501. //
  502. typedef typename detail::find_arg<arg_list, is_discrete_quantile<mpl::_1>, typename Policy::discrete_quantile_type >::type discrete_quantile_type;
  503. //
  504. // Mathematically undefined properties:
  505. //
  506. typedef typename detail::find_arg<arg_list, is_assert_undefined<mpl::_1>, typename Policy::assert_undefined_type >::type assert_undefined_type;
  507. //
  508. // Max iterations:
  509. //
  510. typedef typename detail::find_arg<arg_list, is_max_series_iterations<mpl::_1>, typename Policy::max_series_iterations_type>::type max_series_iterations_type;
  511. typedef typename detail::find_arg<arg_list, is_max_root_iterations<mpl::_1>, typename Policy::max_root_iterations_type>::type max_root_iterations_type;
  512. //
  513. // Define a typelist of the policies:
  514. //
  515. typedef mpl::vector<
  516. domain_error_type,
  517. pole_error_type,
  518. overflow_error_type,
  519. underflow_error_type,
  520. denorm_error_type,
  521. evaluation_error_type,
  522. rounding_error_type,
  523. indeterminate_result_error_type,
  524. precision_type,
  525. promote_float_type,
  526. promote_double_type,
  527. discrete_quantile_type,
  528. assert_undefined_type,
  529. max_series_iterations_type,
  530. max_root_iterations_type> result_list;
  531. //
  532. // Remove all the policies that are the same as the default:
  533. //
  534. typedef typename mpl::remove_if<result_list, detail::is_default_policy<mpl::_> >::type reduced_list;
  535. //
  536. // Pad out the list with defaults:
  537. //
  538. typedef typename detail::append_N<reduced_list, default_policy, (14 - ::boost::mpl::size<reduced_list>::value)>::type result_type;
  539. public:
  540. typedef policy<
  541. typename mpl::at<result_type, mpl::int_<0> >::type,
  542. typename mpl::at<result_type, mpl::int_<1> >::type,
  543. typename mpl::at<result_type, mpl::int_<2> >::type,
  544. typename mpl::at<result_type, mpl::int_<3> >::type,
  545. typename mpl::at<result_type, mpl::int_<4> >::type,
  546. typename mpl::at<result_type, mpl::int_<5> >::type,
  547. typename mpl::at<result_type, mpl::int_<6> >::type,
  548. typename mpl::at<result_type, mpl::int_<7> >::type,
  549. typename mpl::at<result_type, mpl::int_<8> >::type,
  550. typename mpl::at<result_type, mpl::int_<9> >::type,
  551. typename mpl::at<result_type, mpl::int_<10> >::type,
  552. typename mpl::at<result_type, mpl::int_<11> >::type,
  553. typename mpl::at<result_type, mpl::int_<12> >::type > type;
  554. };
  555. //
  556. // Full specialisation to speed up compilation of the common case:
  557. //
  558. template <>
  559. struct normalise<policy<>,
  560. promote_float<false>,
  561. promote_double<false>,
  562. discrete_quantile<>,
  563. assert_undefined<>,
  564. default_policy,
  565. default_policy,
  566. default_policy,
  567. default_policy,
  568. default_policy,
  569. default_policy,
  570. default_policy>
  571. {
  572. typedef policy<detail::forwarding_arg1, detail::forwarding_arg2> type;
  573. };
  574. template <>
  575. struct normalise<policy<detail::forwarding_arg1, detail::forwarding_arg2>,
  576. promote_float<false>,
  577. promote_double<false>,
  578. discrete_quantile<>,
  579. assert_undefined<>,
  580. default_policy,
  581. default_policy,
  582. default_policy,
  583. default_policy,
  584. default_policy,
  585. default_policy,
  586. default_policy>
  587. {
  588. typedef policy<detail::forwarding_arg1, detail::forwarding_arg2> type;
  589. };
  590. inline policy<> make_policy()
  591. { return policy<>(); }
  592. template <class A1>
  593. inline typename normalise<policy<>, A1>::type make_policy(const A1&)
  594. {
  595. typedef typename normalise<policy<>, A1>::type result_type;
  596. return result_type();
  597. }
  598. template <class A1, class A2>
  599. inline typename normalise<policy<>, A1, A2>::type make_policy(const A1&, const A2&)
  600. {
  601. typedef typename normalise<policy<>, A1, A2>::type result_type;
  602. return result_type();
  603. }
  604. template <class A1, class A2, class A3>
  605. inline typename normalise<policy<>, A1, A2, A3>::type make_policy(const A1&, const A2&, const A3&)
  606. {
  607. typedef typename normalise<policy<>, A1, A2, A3>::type result_type;
  608. return result_type();
  609. }
  610. template <class A1, class A2, class A3, class A4>
  611. inline typename normalise<policy<>, A1, A2, A3, A4>::type make_policy(const A1&, const A2&, const A3&, const A4&)
  612. {
  613. typedef typename normalise<policy<>, A1, A2, A3, A4>::type result_type;
  614. return result_type();
  615. }
  616. template <class A1, class A2, class A3, class A4, class A5>
  617. inline typename normalise<policy<>, A1, A2, A3, A4, A5>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&)
  618. {
  619. typedef typename normalise<policy<>, A1, A2, A3, A4, A5>::type result_type;
  620. return result_type();
  621. }
  622. template <class A1, class A2, class A3, class A4, class A5, class A6>
  623. inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&)
  624. {
  625. typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6>::type result_type;
  626. return result_type();
  627. }
  628. template <class A1, class A2, class A3, class A4, class A5, class A6, class A7>
  629. inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&, const A7&)
  630. {
  631. typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7>::type result_type;
  632. return result_type();
  633. }
  634. template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8>
  635. inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&, const A7&, const A8&)
  636. {
  637. typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8>::type result_type;
  638. return result_type();
  639. }
  640. template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9>
  641. inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8, A9>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&, const A7&, const A8&, const A9&)
  642. {
  643. typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8, A9>::type result_type;
  644. return result_type();
  645. }
  646. template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9, class A10>
  647. inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&, const A7&, const A8&, const A9&, const A10&)
  648. {
  649. typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>::type result_type;
  650. return result_type();
  651. }
  652. template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9, class A10, class A11>
  653. inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&, const A7&, const A8&, const A9&, const A10&, const A11&)
  654. {
  655. typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11>::type result_type;
  656. return result_type();
  657. }
  658. //
  659. // Traits class to handle internal promotion:
  660. //
  661. template <class Real, class Policy>
  662. struct evaluation
  663. {
  664. typedef Real type;
  665. };
  666. template <class Policy>
  667. struct evaluation<float, Policy>
  668. {
  669. typedef typename mpl::if_<typename Policy::promote_float_type, double, float>::type type;
  670. };
  671. template <class Policy>
  672. struct evaluation<double, Policy>
  673. {
  674. typedef typename mpl::if_<typename Policy::promote_double_type, long double, double>::type type;
  675. };
  676. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  677. template <class Real>
  678. struct basic_digits : public mpl::int_<0>{ };
  679. template <>
  680. struct basic_digits<float> : public mpl::int_<FLT_MANT_DIG>{ };
  681. template <>
  682. struct basic_digits<double> : public mpl::int_<DBL_MANT_DIG>{ };
  683. template <>
  684. struct basic_digits<long double> : public mpl::int_<LDBL_MANT_DIG>{ };
  685. template <class Real, class Policy>
  686. struct precision
  687. {
  688. BOOST_STATIC_ASSERT( ::std::numeric_limits<Real>::radix == 2);
  689. typedef typename Policy::precision_type precision_type;
  690. typedef basic_digits<Real> digits_t;
  691. typedef typename mpl::if_<
  692. mpl::equal_to<digits_t, mpl::int_<0> >,
  693. // Possibly unknown precision:
  694. precision_type,
  695. typename mpl::if_<
  696. mpl::or_<mpl::less_equal<digits_t, precision_type>, mpl::less_equal<precision_type, mpl::int_<0> > >,
  697. // Default case, full precision for RealType:
  698. digits2< ::std::numeric_limits<Real>::digits>,
  699. // User customised precision:
  700. precision_type
  701. >::type
  702. >::type type;
  703. };
  704. template <class Policy>
  705. struct precision<float, Policy>
  706. {
  707. typedef digits2<FLT_MANT_DIG> type;
  708. };
  709. template <class Policy>
  710. struct precision<double, Policy>
  711. {
  712. typedef digits2<DBL_MANT_DIG> type;
  713. };
  714. template <class Policy>
  715. struct precision<long double, Policy>
  716. {
  717. typedef digits2<LDBL_MANT_DIG> type;
  718. };
  719. #else
  720. template <class Real, class Policy>
  721. struct precision
  722. {
  723. BOOST_STATIC_ASSERT((::std::numeric_limits<Real>::radix == 2) || ((::std::numeric_limits<Real>::is_specialized == 0) || (::std::numeric_limits<Real>::digits == 0)));
  724. #ifndef __BORLANDC__
  725. typedef typename Policy::precision_type precision_type;
  726. typedef typename mpl::if_c<
  727. ((::std::numeric_limits<Real>::is_specialized == 0) || (::std::numeric_limits<Real>::digits == 0)),
  728. // Possibly unknown precision:
  729. precision_type,
  730. typename mpl::if_c<
  731. ((::std::numeric_limits<Real>::digits <= precision_type::value)
  732. || (Policy::precision_type::value <= 0)),
  733. // Default case, full precision for RealType:
  734. digits2< ::std::numeric_limits<Real>::digits>,
  735. // User customised precision:
  736. precision_type
  737. >::type
  738. >::type type;
  739. #else
  740. typedef typename Policy::precision_type precision_type;
  741. typedef mpl::int_< ::std::numeric_limits<Real>::digits> digits_t;
  742. typedef mpl::bool_< ::std::numeric_limits<Real>::is_specialized> spec_t;
  743. typedef typename mpl::if_<
  744. mpl::or_<mpl::equal_to<spec_t, mpl::false_>, mpl::equal_to<digits_t, mpl::int_<0> > >,
  745. // Possibly unknown precision:
  746. precision_type,
  747. typename mpl::if_<
  748. mpl::or_<mpl::less_equal<digits_t, precision_type>, mpl::less_equal<precision_type, mpl::int_<0> > >,
  749. // Default case, full precision for RealType:
  750. digits2< ::std::numeric_limits<Real>::digits>,
  751. // User customised precision:
  752. precision_type
  753. >::type
  754. >::type type;
  755. #endif
  756. };
  757. #endif
  758. #ifdef BOOST_MATH_USE_FLOAT128
  759. template <class Policy>
  760. struct precision<__float128, Policy>
  761. {
  762. typedef mpl::int_<113> type;
  763. };
  764. #endif
  765. namespace detail{
  766. template <class T, class Policy>
  767. inline int digits_imp(mpl::true_ const&)
  768. {
  769. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  770. BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
  771. #else
  772. BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
  773. #endif
  774. typedef typename boost::math::policies::precision<T, Policy>::type p_t;
  775. return p_t::value;
  776. }
  777. template <class T, class Policy>
  778. inline int digits_imp(mpl::false_ const&)
  779. {
  780. return tools::digits<T>();
  781. }
  782. } // namespace detail
  783. template <class T, class Policy>
  784. inline int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
  785. {
  786. typedef mpl::bool_< std::numeric_limits<T>::is_specialized > tag_type;
  787. return detail::digits_imp<T, Policy>(tag_type());
  788. }
  789. template <class Policy>
  790. inline unsigned long get_max_series_iterations()
  791. {
  792. typedef typename Policy::max_series_iterations_type iter_type;
  793. return iter_type::value;
  794. }
  795. template <class Policy>
  796. inline unsigned long get_max_root_iterations()
  797. {
  798. typedef typename Policy::max_root_iterations_type iter_type;
  799. return iter_type::value;
  800. }
  801. namespace detail{
  802. template <class T, class Digits, class Small, class Default>
  803. struct series_factor_calc
  804. {
  805. static T get()
  806. {
  807. return ldexp(T(1.0), 1 - Digits::value);
  808. }
  809. };
  810. template <class T, class Digits>
  811. struct series_factor_calc<T, Digits, mpl::true_, mpl::true_>
  812. {
  813. static T get()
  814. {
  815. return boost::math::tools::epsilon<T>();
  816. }
  817. };
  818. template <class T, class Digits>
  819. struct series_factor_calc<T, Digits, mpl::true_, mpl::false_>
  820. {
  821. static T get()
  822. {
  823. static const boost::uintmax_t v = static_cast<boost::uintmax_t>(1u) << (Digits::value - 1);
  824. return 1 / static_cast<T>(v);
  825. }
  826. };
  827. template <class T, class Digits>
  828. struct series_factor_calc<T, Digits, mpl::false_, mpl::true_>
  829. {
  830. static T get()
  831. {
  832. return boost::math::tools::epsilon<T>();
  833. }
  834. };
  835. template <class T, class Policy>
  836. inline T get_epsilon_imp(mpl::true_ const&)
  837. {
  838. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  839. BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::is_specialized);
  840. BOOST_STATIC_ASSERT( ::std::numeric_limits<T>::radix == 2);
  841. #else
  842. BOOST_ASSERT(::std::numeric_limits<T>::is_specialized);
  843. BOOST_ASSERT(::std::numeric_limits<T>::radix == 2);
  844. #endif
  845. typedef typename boost::math::policies::precision<T, Policy>::type p_t;
  846. typedef mpl::bool_<p_t::value <= std::numeric_limits<boost::uintmax_t>::digits> is_small_int;
  847. typedef mpl::bool_<p_t::value >= std::numeric_limits<T>::digits> is_default_value;
  848. return series_factor_calc<T, p_t, is_small_int, is_default_value>::get();
  849. }
  850. template <class T, class Policy>
  851. inline T get_epsilon_imp(mpl::false_ const&)
  852. {
  853. return tools::epsilon<T>();
  854. }
  855. } // namespace detail
  856. template <class T, class Policy>
  857. inline T get_epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T))
  858. {
  859. typedef mpl::bool_< (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::radix == 2)) > tag_type;
  860. return detail::get_epsilon_imp<T, Policy>(tag_type());
  861. }
  862. namespace detail{
  863. template <class A1,
  864. class A2,
  865. class A3,
  866. class A4,
  867. class A5,
  868. class A6,
  869. class A7,
  870. class A8,
  871. class A9,
  872. class A10,
  873. class A11>
  874. char test_is_policy(const policy<A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11>*);
  875. double test_is_policy(...);
  876. template <class P>
  877. struct is_policy_imp
  878. {
  879. BOOST_STATIC_CONSTANT(bool, value = (sizeof(::boost::math::policies::detail::test_is_policy(static_cast<P*>(0))) == 1));
  880. };
  881. }
  882. template <class P>
  883. struct is_policy : public mpl::bool_< ::boost::math::policies::detail::is_policy_imp<P>::value> {};
  884. //
  885. // Helper traits class for distribution error handling:
  886. //
  887. template <class Policy>
  888. struct constructor_error_check
  889. {
  890. typedef typename Policy::domain_error_type domain_error_type;
  891. typedef typename mpl::if_c<
  892. (domain_error_type::value == throw_on_error) || (domain_error_type::value == user_error),
  893. mpl::true_,
  894. mpl::false_>::type type;
  895. };
  896. template <class Policy>
  897. struct method_error_check
  898. {
  899. typedef typename Policy::domain_error_type domain_error_type;
  900. typedef typename mpl::if_c<
  901. (domain_error_type::value == throw_on_error) && (domain_error_type::value != user_error),
  902. mpl::false_,
  903. mpl::true_>::type type;
  904. };
  905. }}} // namespaces
  906. #endif // BOOST_MATH_POLICY_HPP