c++ - Boost Using pow function with cpp_dec_float -
i using pow()
function , trying compare return cpp_dec_float
getting error
code:
pow(sqrt(172.601), 2) != n);
error:
userpath\main.cpp:21: error: no match 'operator!=' (operand types '__gnu_cxx::__promote_2<double, int, double, double>::__type {aka double}' , 'boost::multiprecision::cpp_int {aka boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<> >}') pow(sqrt(172.601), 2) != n)) ^
there number of pitfalls here. see added links @ bottom.
something tells me you've been using backend type, not frontend adaptor (like number<>
or rational_adaptor<>
).
the thing works without change:
#include <boost/multiprecision/cpp_dec_float.hpp> #include <iostream> int main() { boost::multiprecision::cpp_dec_float_50 n = 3; bool ok = pow(sqrt(172.601), 2) != n; std::cout << std::boolalpha << ok; }
prints
true
however
you mixing double
, cpp_dec_float
. means not gain - if in scenario - enhanced accuracy or decimal representation of boost multiprecision.
instead consider going whole way:
#include <boost/multiprecision/cpp_dec_float.hpp> #include <iostream> int main() { typedef boost::multiprecision::cpp_dec_float_50 decimal; decimal n("172.601"); decimal other = pow(sqrt(decimal("172.601")), 2); std::cout << std::setprecision(50) << n << "\n"; std::cout << std::setprecision(50) << other << "\n"; bool equal = (abs(other - n) < std::numeric_limits<decimal>::epsilon()); std::cout << std::boolalpha << equal; }
prints:
172.601 172.601 true
note crucial initialization text, not double
literal!
background info:
Comments
Post a Comment