| Home | Libraries | People | FAQ | More

#### Changing the Policy on an Ad Hoc Basis for the Special Functions

All of the special functions in this library come in two overloaded forms, one with a final "policy" parameter, and one without. For example:

    namespace boost{ namespace math{

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

    template <class RealType>
    RealType tgamma(RealType);

    }} // namespaces

Normally, the second version is just a forwarding wrapper to the first like this:

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

So calling a special function with a specific policy is just a matter of defining the policy type to use and passing it as the final parameter. For example, suppose we want `tgamma` to behave in a C-compatible fashion and set `::errno` when an error occurs, and never throw an exception:

    #include <boost/math/special_functions/gamma.hpp>
    //
    // Define the policy to use:
    //
    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;
    //
    // Now use the policy when calling 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;
    }

which outputs:

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

Alternatively, for ad hoc use, we can use the `make_policy` helper function to create a policy for us: this usage is more verbose, so is probably only preferred when a policy is going to be used once only:

    #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;
       // Check errno was set:
       std::cout << "errno = " << errno << std::endl;
       // and again with evaluation at a 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;
       // Check errno was set:
       std::cout << "errno = " << errno << std::endl;
    }