Converting rationals, long double and integers

Marc Glisse marc.glisse at
Tue Mar 12 20:34:51 CET 2013

On Tue, 12 Mar 2013, edA-qa mort-ora-y wrote:

> I see a function "mpq_get_d", but is there also a function to convert to
> a "long double"?

GMP does not currently handle long long or long double.

> I notice in the todo list a possible mpf_get_ld, but it
> isn't available yet. Is there some other way I can convert a
> rational/float to a long double without losing precision?

If the exponent doesn't overflow, you can convert to a double, subtract 
that double (converted to mpq or mpf), convert what remains to double, 
etc. You can then convert those 2 or 3 double to long double and sum them.

> Is there a function to convert a rational directly to an integer with
> truncating the fraction part?


> Is it enough to do "q.get_num() / q.get_den()" ?

If you are doing C++, just cast to mpz_class.

Marc Glisse

More information about the gmp-discuss mailing list