mpq_get_int and mpq_get_frac

Michel Bardiaux mbardiaux@peaktime.be
Wed, 09 Apr 2003 14:47:46 +0200


(Just beginning with libgmp, so be forgiving...)

I could not find functions to extract the integer and fractional parts 
of an mpq_rat. Am I mistaken? If not, would it be worth adding to the 
library?

Is my own implementation correct:

void
mpq_get_frac(mpq_t res, mpq_t r1)
{
	mpz_t		zr;
	mpz_init(zr);
	mpz_tdiv_r(zr, mpq_numref(r1), mpq_denref(r1));
	mpz_set(mpq_numref(res), zr);
	mpz_set(mpq_denref(res), mpq_denref(r1));
	mpq_canonicalize(res);
	mpz_clear(zr);
}

I am somewhat confused about whether I should take a copy of r1 in case 
res and r1 refer to the same mpq_t (at the call site). Though I realize 
that here, since the denominator of r1 is not used after the /*2*/ the 
code should work even when called as mpq_get_frac(m, m). Correct?

-- 
Michel Bardiaux
Peaktime Belgium S.A.  Bd. du Souverain, 191  B-1160 Bruxelles
Tel : +32 2 790.29.41