C++ Boost多精度cpp_int乘以浮点
是否可以将boost multiprecision int乘以浮点数?这是否不受支持C++ Boost多精度cpp_int乘以浮点,c++,boost,boost-multiprecision,C++,Boost,Boost Multiprecision,是否可以将boost multiprecision int乘以浮点数?这是否不受支持 using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>; boost::multiprecision::bigint x(12345678); auto result = x * 0.26 // << THIS LINE DOES NOT COMPIL
using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>;
boost::multiprecision::bigint x(12345678);
auto result = x * 0.26 // << THIS LINE DOES NOT COMPILE
使用bigint=boost::multiprecision::number;
boost::multiprecision::bigint x(12345678);
自动结果=x*0.26/不支持,因为它是有损的
您可以显式执行转换:
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
//using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>;
using bigint = boost::multiprecision::cpp_int;
using bigfloat = boost::multiprecision::cpp_dec_float_50;
int main() {
bigint x(12345678);
bigfloat y("0.26");
std::cout << "x: " << x << "\n";
std::cout << "y: " << y << "\n";
bigfloat result = x.convert_to<bigfloat>() * y;
//bigint z = result; // lossy conversion will not compile
bigint z1 = static_cast<bigint>(result);
bigint z2 = result.convert_to<bigint>();
std::cout << "Result: " << result << "\n";
std::cout << "z1: " << z1 << "\n";
std::cout << "z2: " << z2 << "\n";
}
警告
一个常见的陷阱是延迟计算表达式模板。使用临时表时,它们是一个陷阱:
auto result = x.convert_to<bigfloat>() * bigfloat("0.26");
auto result=x.将_转换为()*bigfloat(“0.26”);
此后使用结果
是,因为临时表已被销毁。分配给bigfloat
会强制进行计算。如果尝试,会发生什么?如果出现编译错误,是什么?
auto result = x.convert_to<bigfloat>() * bigfloat("0.26");