Home Libraries People FAQ More

#### 為基於特定基礎的特殊函數改變策略(Changing the Policy on an Ad Hoc Basis for the Special Functions)

namespace boost{ namespace math{

template <class RealType, class Policy>
RealType tgamma(RealType, const Policy&);

template <class RealType>
RealType tgamma(RealType);

}} // namespaces

template <class RealType> inline RealType tgamma(RealType x) { return tgamma(x, policies::policy<>()); }

#include <boost/math/special_functions/gamma.hpp>
//
//定義將要使用的策略:
//
using namespace boost::math::policies;
typedef policy<
domain_error<errno_on_error>,
pole_error<errno_on_error>,
overflow_error<errno_on_error>,
evaluation_error<errno_on_error>
> c_policy;
//
// 當調用函數tgamma時使用這個策略:
//
int main()
{
errno = 0;
std::cout << "Result of tgamma(30000) is: "
<< boost::math::tgamma(30000, c_policy()) << std::endl;
std::cout << "errno = " << errno << std::endl;
std::cout << "Result of tgamma(-10) is: "
<< boost::math::tgamma(-10, c_policy()) << std::endl;
std::cout << "errno = " << errno << std::endl;
}

Result of tgamma(30000) is: 1.#INF
errno = 34
Result of tgamma(-10) is: 1.#QNAN
errno = 33

#include <boost/math/special_functions/gamma.hpp>

int main()
{
using namespace boost::math::policies;
errno = 0;
std::cout << "Result of tgamma(30000) is: "
<< boost::math::tgamma(
30000,
make_policy(
domain_error<errno_on_error>(),
pole_error<errno_on_error>(),
overflow_error<errno_on_error>(),
evaluation_error<errno_on_error>()
)
) << std::endl;
// 檢查 errno 已被設置:
std::cout << "errno = " << errno << std::endl;
// 在一個 pole 處計算:
std::cout << "Result of tgamma(-10) is: "
<< boost::math::tgamma(
-10,
make_policy(
domain_error<errno_on_error>(),
pole_error<errno_on_error>(),
overflow_error<errno_on_error>(),
evaluation_error<errno_on_error>()
)
) << std::endl;
// 檢查 errno 已被設置:
std::cout << "errno = " << errno << std::endl;
}