Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Boost多精度cpp_int乘以浮点_C++_Boost_Boost Multiprecision - Fatal编程技术网

C++ Boost多精度cpp_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

是否可以将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 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");