cannot use power function from multiprecision boost library

I have installed boost library version 1.65.0 . I cannot use the power function
"pow" from it. Whenever i write pow and use intellisense , it will only give me the pow function from math.h . I have included the following libraries:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/debug_adaptor.hpp>
#include <boost/math/constants/constants.hpp>

#include <boost/multiprecision/detail/functions/pow.hpp>


using namespace boost::multiprecision;

int main() { cout << pow(112.2 ,3.4) << endl } ;


Are there some other libraries I can use to compute the power function of a large real number such as in the case of "sin(1.0)**2+cos(1.0)**2" so that it returns a value of 1.0 accurately ?

Last edited on
Intellisense is not the compiler. How do you know that your program doesn't use the right pow function when it executes?
First, the expression pow(112.2, 3.4) will call the double, double overload of std::pow. If you want to call boost::multiprecision::pow instead, you need to have arguments of a type it understands, such as cpp_dec_float_50 or cpp_dec_float_100. Let's also bump the output precision:

1
2
3
4
5
6
7
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
using boost::multiprecision::cpp_dec_float_100;
int main() {
    cpp_dec_float_100 a = 112.2, b = 3.4;
    std::cout << std::setprecision(100) << pow(a, b) << '\n';
}

that prints
9332021.131197257591348458922719177023364501846775209425573316488942730002711710486442926388398457225

live demo: https://wandbox.org/permlink/tsy1Fv39kbpyJylr

Second, you want to work with high-precision math, but you're losing it right away:
the expression 112.2 is not 112 and two-tenths. It is a double with the value 112.2000000000000028421709430404007434844970703125. Likewise, "3.4" in a C++ program is not three and four-tenths, it is 3.399999999999999911182158029987476766109466552734375

So, fixing these issues,

1
2
3
4
5
6
7
8
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
using boost::multiprecision::cpp_dec_float_100;
int main() {
    auto a = cpp_dec_float_100(1122)/10;
    auto b = cpp_dec_float_100(34)/10;
    std::cout << std::setprecision(100) << pow(a, b) << '\n';
}

live: https://wandbox.org/permlink/86pIHR2prfIPHpLb
9332021.131197260700021602241138475068619624389028454081951744553393671746470878427399406755645506478


to compare, wolfram alpha for 112.2^3.4 prints
9.332021131197260700021602241138475068619624389028454081951744553393671746470878427399406755645506475578676563... × 10^6
which matches the second result to its second-to-last digit

Nearly all digits of the first result had nothing to do with 112.2^3.4
Last edited on
ok , i just checked , it is actually using boost::multiprecision. however my final result is 1 not 1.0 , i think it looses the precision somewhere else, maybe at the addition of "sin(1.0)**2+cos(1.0)**2".
Topic archived. No new replies allowed.