Search code examples
c++square-rootcmath

Taking a root higher than 2


I'm trying to take the 11th root of an expression and I'm getting a return of -inf.

std::cout << pow(j,(1.0/11.0)) << std::endl;

where j is just some log expression. I've checked that number to make sure it's valid, and it is. I'm thinking it's the way that the power expression is being run. Is there a better way to do this? Thanks.

And yes, I've included cmath into my work.


Solution

  • I can't think of a valid reason for pow to return -inf, if your inputs are marginally sane. However in case you're passing in a negative number, something that may be worth trying is:

    if(j==0) return 0;
    if(j<0) return -pow(-j, 1.0/11.0);
    return pow(j,1.0/11.0);