简体   繁体   English

不使用 cpp_bin_float_(50/100) 时提升多精度 cpp_float 无法正常工作

[英]Boost multiprecision cpp_float not working properly when not using cpp_bin_float_(50/100)

I need to do calculations with higher precision than doubles and am using boost::multiprecision for that.我需要进行比双精度精度更高的计算,为此我正在使用 boost::multiprecision。 This works perfectly fine when I use boost::multiprecision::cpp_bin_float_50 or boost::multiprecision::cpp_bin_float_100 .当我使用boost::multiprecision::cpp_bin_float_50boost::multiprecision::cpp_bin_float_100时,这非常有效。 So simply doing something like所以简单地做一些像

#include <boost/multiprecision/cpp_bin_float.hpp>
// ...

boost::multiprecision::cpp_bin_float_100 Test1, Test2;
Test1 = 1.0;
Test2 = 2.0;
Test1 = Test1 + Test2;

works.作品。 But I need different amounts of precision.但我需要不同程度的精度。 So for example, I'd simply like to do something like因此,例如,我只想做类似的事情

boost::multiprecision::cpp_bin_float<200> Test1, Test2;
Test1 = 1.0;
Test2 = 2.0;
Test1 = Test1 + Test2;

While the first three lines work fine, I get an error in the forth line saying "no matching operator found".虽然前三行工作正常,但我在第四行中收到一条错误消息,提示“未找到匹配的运算符”。 Also .convert_to<double>() is not found, which I will need later.也没有找到.convert_to<double>() ,我稍后会需要它。 Same thing for cpp_dec_float ... cpp_dec_float ...

I'm sure, I am missing something stupid here.我敢肯定,我在这里遗漏了一些愚蠢的东西。 Can anybody help?有人可以帮忙吗?

Edit:编辑:

Thank you sehe - I just wanted to edit in the exact same thing.谢谢你 sehe - 我只是想编辑完全相同的东西。 It took me ages to find that out.我花了很长时间才发现这一点。 Funny how I couldn't find one single example of how to use arbitrary lengths other than..._50 or..._100...有趣的是我找不到一个例子来说明如何使用任意长度而不是..._50 或..._100...

The boost::multiprecision::cpp_bin_float<200> type is a backend type. boost::multiprecision::cpp_bin_float<200>类型是后端类型。 You want a frontend type, which would be eg你想要一个前端类型,例如

number<cpp_bin_float<200> >

You can compare this with the definitions of the working types :您可以将其与工作类型的定义进行比较:

using cpp_bin_float_50 = number<backends::cpp_bin_float<50> > ;
using cpp_bin_float_100 = number<backends::cpp_bin_float<100> >;

Backends define the implementation, where the frontend provides the usability interfaces that you expect, like operator overloads.后端定义实现,其中前端提供您期望的可用性接口,如运算符重载。

Live Demo现场演示

#include <boost/multiprecision/cpp_bin_float.hpp>
namespace bmp = boost::multiprecision;

int main() {
    bmp::number<bmp::cpp_bin_float<200>> v(0);

    v += 2.0;
    v = pow(v, 156);

    std::cout << std::fixed << v;
}

Prints:印刷:

91343852333181432387730302044767688728495783936.000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM