Search code examples
ctype-conversiongmp

How to convert two gmpz_t to a mpq_t?


I have two integers (gmpz_t), and I need to express them as a fraction (one being the numerator, the other the denominator). The only solution I found is using char* mpz_get_str (char *str, int base, mpz_t op) to convert them both to char*, then concatenate them with a "/" inbetween, and then use int mpq_set_str (mpq_t rop, char *str, int base) to put the value into a rational. But that seems like a lot of effort and I imagine there must be a better way.


Solution

  • Why not convert each to mpq_t via mpq_set_z, then divide?