mpq conversion to mpz: this method is reliable?
Julien D Arques
acc.gccquestions at gmail.com
Fri Mar 24 09:51:54 CET 2023
My numbers are always positive, so it should do, if I am myself not mistaken
Le jeu. 23 mars 2023 à 20:48, Marc Glisse <marc.glisse at inria.fr> a écrit :
> 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
> > with ?
> Marc Glisse
More information about the gmp-discuss