0

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?

algae
  • 407
  • 4
  • 15

2 Answers2

0

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;
}
algae
  • 407
  • 4
  • 15
  • The ADL here is intentional: You want to use the libc version of pow if possible (for speed), and only use the higher-precision multiprecision::pow when using multiprecision types. – user14717 Sep 15 '20 at 15:33
  • 1
    I was caught out because usually we have this paranoia about being specific with which functions and namespaces we call and so on. – algae Sep 16 '20 at 01:38
  • It's a sensible paranoia, only to be overridden for good reason. – user14717 Sep 16 '20 at 14:38
  • 1
    If you look at the constants, they are long text strings which are then parsed and rounded to the precision. – user14717 Sep 16 '20 at 14:40
-2

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.