我正在使用GMP,我希望能够快速转换mpz为mpf.我查看了图书馆,找不到多少.我能想到的最好的事情就是:
mpz_t x;
/* Insert code here that assigns some value to x */
char buf[SIZE];
gmp_sprintf(buf, "%Zd", x);
mpf_t y;
mpf_set_str(y, buf);
Run Code Online (Sandbox Code Playgroud)
此解决方案需要重复转换为字符串.此外,它受到限制SIZE,我认为没有办法预先决定SIZE需要多大.有没有更好的方法来进行这种转换?
小智 7
怎么用mpf_set_z (mpf_t rop, mpz_t op)?
另外(我假设你已经这样做了)你的mpz和mpf变量需要用mpf_init(mpf_t x)和初始化mpz_init(mpz_t x).
所以你要这样做:
mpz_t x;
mpz_init(x);
/* Insert code here that assigns some value to x */
mpf_t y;
mpf_init(y);
mpf_set_z(y,x);
Run Code Online (Sandbox Code Playgroud)