Behavior of mpz_root with negative op argument

Jason Quinn jason.lee.quinn at gmail.com
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?

Cheers,
Jason

#include<stdio.h>
#include<gmp.h>

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

    mpz_init(rop);
    mpz_init(op);

    mpz_set_si(rop,1);
    mpz_set_si(op,-3);

    puts("before call");
    mpz_root(rop,op,n);
    puts("after call");

    return 0;
    }


More information about the gmp-discuss mailing list