我在我的环境中安装了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);但这并不是很有效。那我该怎么做?我做错了什么吗?
谢谢
发布于 2014-11-16 16:16:22
mpf2mpfr.h可能不是您想要的。它包含大量的#define,可以在后面的所有内容中用mpfr名称替换mpf名称。如果你想让它在你的情况下工作,你必须在gmpxx.h之前包含mpf2mpfr.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中提供的额外函数需要更多的调整,在包含gmpxx.h之前先禁用它们:#define __GMPXX_USE_CXX11 0会更容易。
解决这个问题的方法主要有两种,而且都是从删除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。它的webpage为它列出了6个C++包装器,其中几个仍然在维护。
https://stackoverflow.com/questions/26937202
复制相似问题