Search code examples
c++overloadingpowboost-multiprecision

Overload boost::multiprecision::pow with fixed precision


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?


Solution

  • 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;
    }