Return type of gcd in C++

Marc Glisse marc.glisse at inria.fr
Tue Jun 17 15:12:51 UTC 2014


On Tue, 17 Jun 2014, Torbjörn Granlund wrote:

> Marc Glisse <marc.glisse at inria.fr> writes:
>
>  the C++ standard is likely to add a gcd function for builtin integer
>  types, and that reminds me that gmpxx still doesn't provide such a
>  function. As long as both arguments are mpz_class (or expressions),
>  things are relatively clear. But what about gcd(mpz_class,short), what
>  should it return? It could return an expression that evaluates to
>  mpz_class, but we could also make it return unsigned short (or
>  unsigned long so they all return the same thing). On the other hand,
>  that might be a bit confusing. Since mpz_class%short returns an mpz
>  expression, I think I will do the same for gcd, to be consistent, but
>  if people have opinions on this, I am listening.
>
> Keep in mind that (a,0) = a from the definition of gcd.  This suggests
> that the result of gcd(mpz,short) need an mpz for the result.

Thanks, that makes the answer obvious indeed...

I got confused because mpz_gcd_ui returns an unsigned long, but looking 
more closely it also writes the result in a mpz_t (if one is provided) and 
returns 0 when the result doesn't fit.

-- 
Marc Glisse


More information about the gmp-discuss mailing list