Search code examples
cgmp

GMP: Are self-assignments forbidden?


I'm using the GMP library for arbitrary precision in C. All usage examples that I've seen seem to avoid self assignments such as:

Syntax : void mpz_add (mpz_t rop, const mpz_t op1, const mpz_t op2)

mpz_add(a, a, b); // Assign a+b to a

Is this usage allowed or should I only resort to assigning to a third variable?


Solution

  • There is nothing wrong with self-assignment as such. In fact, the documentation states exactly opposite. According to 3.4 Variable Conventions:

    GMP lets you use the same variable for both input and output in one call. For example, the main function for integer multiplication, mpz_mul, can be used to square x and put the result back in x with

     mpz_mul (x, x, x);