Behavior of mpz_root with negative op argument

Jason Quinn jason.lee.quinn at
Sun Jan 17 11:38:19 UTC 2016

I noticed that mpz_root causes a Floating exception if its second
argument is a negative mpz_t number. (Below is a simple test case.)
The (6.0.1) documentation for mpz_root is:

    Function: int mpz_root (mpz_t rop, const mpz_t op, unsigned long int n)

    Set rop to the truncated integer part of the nth root of op.
Return non-zero if the computation was exact, i.e., if op is rop to
the nth power.

The documentation doesn't say what the behaviour is if the second
argument is negative. When the argument is negative, the n-th root
will generally have an imaginary component. Since there are no caveats
mentioned, the straightforward way to interpret the documentation is
that truncation returns the real part (ie integer part) of one of the
complex roots, which would allow a negative argument. It appears
however that negative op arguments were not intended as part of
mpz_root's input domain, which is sensible too. In that case, the
documentation should state so explicitly. (And any other functions
whose domains are limited this way.)

What is the intended behaviour of mpz_root when its second argument is negative?



int main(void) {
    unsigned long int n=2;
    mpz_t rop, op;



    puts("before call");
    puts("after call");

    return 0;

More information about the gmp-discuss mailing list