Overload boost::multiprecision::pow with fixed precision

362 views Asked by At

The following code

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>, boost::multiprecision::et_off> float_50; 

int main()
{
    float_50 a = boost::multiprecision::pow((float_50)5, (float_50)10); // works
    double b = boost::multiprecision::pow((double)5, (double)10);       // doesn't work

    //double b = boost::multiprecision::pow<double>((double)5, (double)10); // Why can't be overloaded?
    return 0;
}

won't compile since boost::multiprecision::pow does not recognised fixed precision type. What is the usual solution for this? I'd rather have a single pow function which accepts both multi and fixed precision types. It is strange to me that boost constants, for instance, have template definitions so

boost::math::constants::pi<double>()
boost::math::constants::pi<float_50>()

work fine. Shouldn't boost::multiprecision::pow also be overloaded?

2

There are 2 answers

4
algae On BEST ANSWER

The solution is to leave off the namespace.

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>, boost::multiprecision::et_off> float_50; 

int main()
{
    float_50 a = pow((float_50)5, (float_50)10); 
    double b = pow(5., 10.);      
    return 0;
}
0
Christopher Yeleighton On

There is no way for a single function to handle arguments of different types. You need a separate function for each supported parameter set. The function may be autogenerated from a template—but pow does not seem to be a candidate for a template implementation.