Thanks for the answer! (and sorry for my very late reply) Le dimanche 3 mai 2015 18:54:43 UTC+2, Bill Hart a écrit : > > > > On 3 May 2015 at 12:51, <20100.d...@gmail.com <javascript:>> wrote: > >> Hello, >> >> I would like to know whether it it is possible to compare a mpz_t and a >> mpq_t more efficiently than: >> - creating a mpq_t with denominator 1 >> - swap the numerator with the mpz_t >> - call mpq_cmp >> - swap back the numerator with the mpz_t >> - free the memory of the mpq_t >> > > Sure. Take a look at the mpq_numref and mpq_denref functions. There is no > need to create an mpq_t, though you do need a temporary mpz_t for the > result of the multiplication. > > Of course, any function which performed such a comparison would have to > create this internally anyway. So in terms of efficiency, it's about the > best you can do. You can special case denominator = 1 or numerator = 0 and > a few other cases like that. But it's about the best you can do. >
I asked the question after I had a look at mpq_cmp. This function first tries some comparison based on the size of numerators and denominators. So I deduced that calling mpq_cmp would be faster (in simple cases) rather than direct cross multiplication even if I had to initialize an mpq_t with a denominator 1. >> If not, would it be useful to have this as a standalone function (like >> "mpq_cmp_mpz")? There is a need within Sage [1,2] where often integer and >> rational types are mixed. >> > > I'd be inclined to just add such a thing as a macro if you need it often. > It's only about four lines to implement. > What would be this macro? Calling mpq_cmp? Cross multiplication? Vincent -- You received this message because you are subscribed to the Google Groups "mpir-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to mpir-devel+unsubscr...@googlegroups.com. To post to this group, send email to mpir-devel@googlegroups.com. Visit this group at http://groups.google.com/group/mpir-devel. For more options, visit https://groups.google.com/d/optout.