2

考虑下面的代码,它使用 boost 创建了一个多精度浮点数“ a ”。

如何使用 boost 库来调用三角函数?例如,我希望计算sin(a)

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

using namespace std;
using namespace boost::multiprecision;

typedef number<backends::cpp_bin_float<24, backends::digit_base_2, void, boost::int16_t, -126, 127>, et_off> float32;

int main (void) {
  float32 a("0.5");

  return 0;
}
4

2 回答 2

2

库中似乎存在限制。当精度降得太低时,sin实现不再编译。

一些中间计算正在double精确进行。分配到结果类型将是有损的,因此无法编译。

您选择的类型实际上对应cpp_bin_float_single. 那不编译。

只要您选择cpp_bin_float_double(精度为 53 位二进制数字)或更高,就可以了。


我想这个限制在某些方面可以被视为一个错误。您可以将它报告给库开发人员,他们将能够判断相关代码是否可以在那里使用单精度浮点数,而不会损害sin近似值的收敛性。

于 2015-05-26T23:10:57.583 回答
0
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <iostream>
using namespace std;
using namespace boost::multiprecision;
int main() {
    cpp_bin_float_100 a = 1;
    cout << setprecision(50) << endl;
    cout << sin(a) << endl;
    return 0;
}

我已经用 Wolfram Mathematica 验证了数字,它们是正确的:

在此处输入图像描述

于 2018-11-30T23:30:49.183 回答