質問

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.

役に立ちましたか?

解決

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);

他のヒント

  1. try to look for FPU errors

    • the most common is forgotten return of float/double in some function
    • which leads to problems on FPU stack which is really small.
  2. also you can try add this before pow

    asm { fninit; };
    
    • this resets the FPU so if you have problems on stack it will help
    • but of course do not do this in middle of some FPU computation
    • it would destroy its result
    • if you are not on x87 platform than this will not help
  3. the value of j before crash will be a good start to share with us.

  4. try to store the result of pow to some float/double variable

    • cout that variable not temporary heap memory location
    • if it prints -inf look also inside that variable if it is also -inf
    • (could be something wrong with the cout not pow ... )
  5. minimize your code (turn off everything part by part)

    • and see if the problems is suddenly not there
    • hidden memory leaks and code overwrites are evil ...

Let us know what you have found.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top