mpq_mul_ui

Marc Glisse marc.glisse at inria.fr
Sun Jan 23 01:34:20 CET 2022


Hello,

What would you think of adding mpq_mul_ui, mpq_div_ui, mpq_ui_div, and 
also the _z versions?

Something like (untested)

void
mpq_mul_ui (mpq_ptr prod, mpq_srcptr op, unsigned long m)
{
   unsigned long gcd_ui;

   if (SIZ (NUM (op)) == 0 || m == 0)
     {
       /* We special case this to simplify allocation logic; gcd(0,x) = x
 	 is a singular case for the allocations.  */
       SIZ (NUM (prod)) = 0;
       MPZ_NEWALLOC (DEN (prod), 1)[0] = 1;
       SIZ (DEN (prod)) = 1;
       return;
     }

#if 0
   if (m == 1)
     {
       if (prod != op)
 	mpq_set (prod, op);
       return;
     }
#endif

   gcd_ui = mpz_gcd_ui (0, DEN (op), m);

#if 0
   if (gcd_ui == 1)
     {
       mpz_mul_ui (NUM (prod), NUM (op), m);
       if (prod != op)
 	mpz_set (DEN (prod), DEN (op));
     }
#endif

   mpz_mul_ui (NUM (prod), NUM (op), m / gcd_ui);
   mpz_divexact_ui (DEN (prod), DEN (op), gcd_ui);
   // mpz_divexact_gcd (DEN (prod), DEN (op), gcd_z);
}


mpq_add_ui is easy to emulate with mpz_addmul_ui, but mpq_mul_ui is a bit 
harder (3 operations plus the need to handle 0 explicitly).

I can add similar code directly in gmpxx.h, but I first wanted to check if 
a C version was desirable.

-- 
Marc Glisse


More information about the gmp-devel mailing list