policy.hpp 39 KB

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