Round a mpq_t number to the nearest integer

Paul Zimmermann Paul.Zimmermann at loria.fr
Sun May 17 19:41:49 CEST 2009


> Date: Sun, 17 May 2009 16:52:50 +0200
> From: behnaz.bostanipour at epfl.ch
> 
> Hello;
> I have a number "a" of type mpq_t and I would like
> to round it to the nearest integer.The result can be
> of type mpz_t or another type.Is there any function
> which can help me to do so or should I implement it
> myself?
> Thanks,
> RGDS,
> B.

unfortunately, there is no division with round-to-nearest in mpz.
A workaround is the following: if n/d is the mpq fraction, then
divide 2n+d by 2d with rounding towards -Infinity.

Paul Zimmermann



More information about the gmp-discuss mailing list