policy.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 870 行 · 第 1/3 页
HPP
870 行
// Copyright John Maddock 2007.// Use, modification and distribution are subject to the// Boost Software License, Version 1.0. (See accompanying file// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)#ifndef BOOST_MATH_POLICY_HPP#define BOOST_MATH_POLICY_HPP#include <boost/mpl/list.hpp>#include <boost/mpl/contains.hpp>#include <boost/mpl/if.hpp>#include <boost/mpl/find_if.hpp>#include <boost/mpl/remove_if.hpp>#include <boost/mpl/vector.hpp>#include <boost/mpl/push_back.hpp>#include <boost/mpl/at.hpp>#include <boost/mpl/size.hpp>#include <boost/mpl/comparison.hpp>#include <boost/type_traits/is_same.hpp>#include <boost/static_assert.hpp>#include <boost/assert.hpp>#include <boost/math/tools/config.hpp>#include <limits>// Sadly we do need the .h versions of these to be sure of getting// FLT_MANT_DIG etc.#include <limits.h>#include <stdlib.h>#include <stddef.h>#include <math.h>namespace boost{ namespace math{ namespace tools{template <class T>int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE(T));}namespace policies{//// Define macros for our default policies, if they're not defined already://#ifndef BOOST_MATH_DOMAIN_ERROR_POLICY#define BOOST_MATH_DOMAIN_ERROR_POLICY throw_on_error#endif#ifndef BOOST_MATH_POLE_ERROR_POLICY#define BOOST_MATH_POLE_ERROR_POLICY throw_on_error#endif#ifndef BOOST_MATH_OVERFLOW_ERROR_POLICY#define BOOST_MATH_OVERFLOW_ERROR_POLICY throw_on_error#endif#ifndef BOOST_MATH_EVALUATION_ERROR_POLICY#define BOOST_MATH_EVALUATION_ERROR_POLICY throw_on_error#endif#ifndef BOOST_MATH_ROUNDING_ERROR_POLICY#define BOOST_MATH_ROUNDING_ERROR_POLICY throw_on_error#endif#ifndef BOOST_MATH_UNDERFLOW_ERROR_POLICY#define BOOST_MATH_UNDERFLOW_ERROR_POLICY ignore_error#endif#ifndef BOOST_MATH_DENORM_ERROR_POLICY#define BOOST_MATH_DENORM_ERROR_POLICY ignore_error#endif#ifndef BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY#define BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY ignore_error#endif#ifndef BOOST_MATH_DIGITS10_POLICY#define BOOST_MATH_DIGITS10_POLICY 0#endif#ifndef BOOST_MATH_PROMOTE_FLOAT_POLICY#define BOOST_MATH_PROMOTE_FLOAT_POLICY true#endif#ifndef BOOST_MATH_PROMOTE_DOUBLE_POLICY#ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS#define BOOST_MATH_PROMOTE_DOUBLE_POLICY false#else#define BOOST_MATH_PROMOTE_DOUBLE_POLICY true#endif#endif#ifndef BOOST_MATH_DISCRETE_QUANTILE_POLICY#define BOOST_MATH_DISCRETE_QUANTILE_POLICY integer_round_outwards#endif#ifndef BOOST_MATH_ASSERT_UNDEFINED_POLICY#define BOOST_MATH_ASSERT_UNDEFINED_POLICY true#endif#ifndef BOOST_MATH_MAX_SERIES_ITERATION_POLICY#define BOOST_MATH_MAX_SERIES_ITERATION_POLICY 1000000#endif#ifndef BOOST_MATH_MAX_ROOT_ITERATION_POLICY#define BOOST_MATH_MAX_ROOT_ITERATION_POLICY 200#endif#if !defined(__BORLANDC__)#define BOOST_MATH_META_INT(type, name, Default)\ template <type N = Default> struct name : public boost::mpl::int_<N>{};\ namespace detail{\ template <type N>\ char test_is_valid_arg(const name<N>*);\ char test_is_default_arg(const name<Default>*);\ template <class T> struct is_##name##_imp\ {\ template <type N> static char test(const name<N>*);\ static double test(...);\ BOOST_STATIC_CONSTANT(bool, value = sizeof(test(static_cast<T*>(0))) == 1);\ };\ }\ template <class T> struct is_##name : public boost::mpl::bool_<detail::is_##name##_imp<T>::value>{};#define BOOST_MATH_META_BOOL(name, Default)\ template <bool N = Default> struct name : public boost::mpl::bool_<N>{};\ namespace detail{\ template <bool N>\ char test_is_valid_arg(const name<N>*);\ char test_is_default_arg(const name<Default>*);\ template <class T> struct is_##name##_imp\ {\ template <bool N> static char test(const name<N>*);\ static double test(...);\ BOOST_STATIC_CONSTANT(bool, value = sizeof(test(static_cast<T*>(0))) == 1);\ };\ }\ template <class T> struct is_##name : public boost::mpl::bool_<detail::is_##name##_imp<T>::value>{};#else#define BOOST_MATH_META_INT(Type, name, Default)\ template <Type N = Default> struct name : public boost::mpl::int_<N>{};\ namespace detail{\ template <Type N>\ char test_is_valid_arg(const name<N>*);\ char test_is_default_arg(const name<Default>*);\ template <class T> struct is_##name##_tester\ {\ template <Type N> static char test(const name<N>&);\ static double test(...);\ };\ template <class T> struct is_##name##_imp\ {\ static T inst;\ BOOST_STATIC_CONSTANT(bool, value = sizeof(detail::is_##name##_tester<T>::test(inst)) == 1);\ };\ }\ template <class T> struct is_##name : public boost::mpl::bool_<detail::is_##name##_imp<T>::value>\ {\ template <class U> struct apply{ typedef is_##name<U> type; };\ };#define BOOST_MATH_META_BOOL(name, Default)\ template <bool N = Default> struct name : public boost::mpl::bool_<N>{};\ namespace detail{\ template <bool N>\ char test_is_valid_arg(const name<N>*);\ char test_is_default_arg(const name<Default>*);\ template <class T> struct is_##name##_tester\ {\ template <bool N> static char test(const name<N>&);\ static double test(...);\ };\ template <class T> struct is_##name##_imp\ {\ static T inst;\ BOOST_STATIC_CONSTANT(bool, value = sizeof(detail::is_##name##_tester<T>::test(inst)) == 1);\ };\ }\ template <class T> struct is_##name : public boost::mpl::bool_<detail::is_##name##_imp<T>::value>\ {\ template <class U> struct apply{ typedef is_##name<U> type; };\ };#endif//// Begin by defining policy types for error handling://enum error_policy_type{ throw_on_error = 0, errno_on_error = 1, ignore_error = 2, user_error = 3};BOOST_MATH_META_INT(error_policy_type, domain_error, BOOST_MATH_DOMAIN_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, pole_error, BOOST_MATH_POLE_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, overflow_error, BOOST_MATH_OVERFLOW_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, underflow_error, BOOST_MATH_UNDERFLOW_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, denorm_error, BOOST_MATH_DENORM_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, evaluation_error, BOOST_MATH_EVALUATION_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, rounding_error, BOOST_MATH_ROUNDING_ERROR_POLICY)BOOST_MATH_META_INT(error_policy_type, indeterminate_result_error, BOOST_MATH_INDETERMINATE_RESULT_ERROR_POLICY)//// Policy types for internal promotion://BOOST_MATH_META_BOOL(promote_float, BOOST_MATH_PROMOTE_FLOAT_POLICY)BOOST_MATH_META_BOOL(promote_double, BOOST_MATH_PROMOTE_DOUBLE_POLICY)BOOST_MATH_META_BOOL(assert_undefined, BOOST_MATH_ASSERT_UNDEFINED_POLICY)//// Policy types for discrete quantiles://enum discrete_quantile_policy_type{ real, integer_round_outwards, integer_round_inwards, integer_round_down, integer_round_up, integer_round_nearest};BOOST_MATH_META_INT(discrete_quantile_policy_type, discrete_quantile, BOOST_MATH_DISCRETE_QUANTILE_POLICY)//// Precision://BOOST_MATH_META_INT(int, digits10, BOOST_MATH_DIGITS10_POLICY)BOOST_MATH_META_INT(int, digits2, 0)//// Iterations://BOOST_MATH_META_INT(unsigned long, max_series_iterations, BOOST_MATH_MAX_SERIES_ITERATION_POLICY)BOOST_MATH_META_INT(unsigned long, max_root_iterations, BOOST_MATH_MAX_ROOT_ITERATION_POLICY)//// Define the names for each possible policy://#define BOOST_MATH_PARAMETER(name)\ BOOST_PARAMETER_TEMPLATE_KEYWORD(name##_name)\ BOOST_PARAMETER_NAME(name##_name)struct default_policy{};namespace detail{//// Trait to work out bits precision from digits10 and digits2://template <class Digits10, class Digits2>struct precision{ // // Now work out the precision: // typedef typename mpl::if_c< (Digits10::value == 0), digits2<0>, digits2<((Digits10::value + 1) * 1000L) / 301L> >::type digits2_type;public:#ifdef __BORLANDC__ typedef typename mpl::if_c< (Digits2::value > ::boost::math::policies::detail::precision<Digits10,Digits2>::digits2_type::value), Digits2, digits2_type>::type type;#else typedef typename mpl::if_c< (Digits2::value > digits2_type::value), Digits2, digits2_type>::type type;#endif};template <class A, class B, bool b>struct select_result{ typedef A type;};template <class A, class B>struct select_result<A, B, false>{ typedef typename mpl::deref<B>::type type;};template <class Seq, class Pred, class DefaultType>struct find_arg{private: typedef typename mpl::find_if<Seq, Pred>::type iter; typedef typename mpl::end<Seq>::type end_type;public: typedef typename select_result< DefaultType, iter, ::boost::is_same<iter, end_type>::value>::type type;};double test_is_valid_arg(...);double test_is_default_arg(...);char test_is_valid_arg(const default_policy*);char test_is_default_arg(const default_policy*);template <class T>struct is_valid_policy_imp { BOOST_STATIC_CONSTANT(bool, value = sizeof(test_is_valid_arg(static_cast<T*>(0))) == 1);};template <class T>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?