mpq conversion to mpz: this method is reliable?

Marc Glisse marc.glisse at inria.fr
Thu Mar 23 20:48:26 CET 2023


On Thu, 23 Mar 2023, Julien D Arques wrote:

> Hi,
> Usign the c++ interface to gmp.
>
> Is the following code correct to round the current rational to a floored
> integer? (eg 5.9999 => 5 and 5.0001 => 5 as well)

If I am not mistaken, converting a mpq_class to mpz_class truncates, i.e. 
it rounds towards 0, not -infinity (which would be the floor).

> mpq_class mpq_var;
> double a_double = 0.90;
> mpq_var /= a_double;
> mpq_var.get_num() = mpq_var;
> mpq_var.get_den() = 1;
>
> As it seems to work, is there any failure to expect / special cases to deal
> with ?

-- 
Marc Glisse


More information about the gmp-discuss mailing list