GMP converts mpz to mpf

I am using GMP and I want to be able to quickly convert mpzto mpf. I looked through the library and could not find much. The best I could think of was:

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);

This solution requires re-conversion to and from the string. In addition, it is limited SIZE, and I see no way to determine how large SIZE. Is there a better way to do this conversion?

+3
source share
2 answers

How about using mpf_set_z (mpf_t rop, mpz_t op)?

Also (suppose you did) the mpz and mpf variables will need to be initialized with mpf_init(mpf_t x)and mpz_init(mpz_t x).

So you would do:

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);
+5
source

, sprintf, , ​​.

mpz_sizeinbase , , . , , mpf_set_z , 10 .

+2

Source: https://habr.com/ru/post/1729737/


All Articles