mpq_get_int and mpq_get_frac

Kevin Ryde user42@zip.com.au
Sat, 12 Apr 2003 10:00:12 +1000


Michel Bardiaux <mbardiaux@peaktime.be> writes:
>
> 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?

Maybe.  We generally encourage direct mpq_numref etc for anything not
already present.

> 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?

Yes, I don't think you need the copy.

You might like to skip the mpz_set if res==r1 too.

Also, I suspect you don't need the canonicalize (and getting rid of it
is a good thing, because it's very slow), since if n and d have no
common factor, then n-q*d and d have no common factor either.  I think
that's right.  Sounds plausible at least :-).

Don't forget to test for the numerator becoming zero though, since in
that case the denominator must be set to 1.  (0/1 being the canonical
form for 0.)