I am using GMP, and I want to be able to quickly convert an mpz to an mpf. I looked through the library and couldn’t find much. The best thing I could think of was this:
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 repeated conversion to and from a string. Also, it is limited by SIZE, and I see no way to predetermine how large SIZE needs to be. Is there any better way to do this conversion?
What about using
mpf_set_z (mpf_t rop, mpz_t op)?Also (I assume you’ve done this) your mpz and mpf variables will need initialising with
mpf_init(mpf_t x)andmpz_init(mpz_t x).So you’d do: