3

我在我的环境中安装了 gmp 和 mpfr。现在我可以成功

#include <gmpxx.h>
#include <mpfr.h>
#include <mpf2mpfr.h>

现在,假设我用一些值初始化了一个 mpf_class:

mpf_class x = 0.73;

我怎样才能使用 mpfr 来获得这个数字的罪孽?我只需要一个 mpf_class 输入,一个 mpf_class 输出。就像是:

mpf_class y = sin(x)

这显然行不通。我注意到有一个 mpfr_sin 函数,我这样称呼它:

mpfr_sin(x, y, MPFR_RNDN);

但这也没有奏效。那我该怎么办?难道我做错了什么?

谢谢

4

1 回答 1

2

mpf2mpfr.h可能不是你想要的。它包含大量在#define随后的所有内容中用 mpfr 名称替换 mpf 名称的内容。如果您希望有机会在您的情况下使用它,则必须包含mpf2mpfr.hbefore gmpxx.h。但是,该文件不会翻译所有内容。以下允许它在 C++03 中编译(只要您不转换为mpq_class):

#include <mpfr.h>
#include <mpf2mpfr.h>
void mpfr_get_q (mpq_ptr, mpfr_srcptr);
#undef mpq_set_f
#define mpq_set_f(x,y) mpfr_get_q(x,y)
#include <gmpxx.h>

int main(){
  mpf_class x=.73;
  mpf_class y;
  mpfr_sin(y.get_mpf_t(),x.get_mpf_t(),MPFR_RNDN);
} 

但是尝试打印operator<<会打印一个指针而不是数字,例如。C++11 中提供的额外功能需要更多调整,禁用它们更容易:#define __GMPXX_USE_CXX11 0在包含gmpxx.h.

解决这个问题的方法主要有两种,都从删除mpf2mpfr.h. 第一个是创建一个临时的mpfr_t

mpf_class x=.73;
mpf_class y;
mpfr_t xx;
mpfr_t yy;
mpfr_init_set_f(xx, x.get_mpf_t(), MPFR_RNDN);
mpfr_init(yy);
mpfr_sin(yy, xx, MPFR_RNDN);
mpfr_get_f(y.get_mpf_t(), yy, MPFR_RNDN);

第二种是完全放弃mpf,只使用mpfr。它的网页列出了它的 6 个 C++ 包装器,其中几个仍然被维护。

于 2014-11-16T08:16:21.883 回答