问题
Why is the output of the dResult invalid ?
Env: Visual Studio 2008
int _tmain(int argc, _TCHAR* argv[])
{
double dN = - 0.091023604111478473 ;
double dD = 0.127777777777777;
double dResult = pow( dN,dD );
//dResult = -1.#IND000000000000
return 0;
}
回答1:
See http://www.cplusplus.com/reference/clibrary/cmath/pow/
double pow (double base, double exponent );
"If base is negative and exponent is not an integral value, or if base is zero and exponent is negative, a domain error occurs, setting the global variable errno to the value EDOM."
回答2:
If your dD value was .25 instead of the fraction you've presented then you could see that it is really taking the fourth root instead of an exponential of a negative number. Your fraction is close to the eighth root. You need complex numbers to express the answer that function should give.
回答3:
That's the expected result because dN is negative. The result of pow( dN,dD ); is only defined if either dN is positive or if dD is an integer. Otherwise, the result is a complex number. For example, pow(-1., 0.5) won't work either.
回答4:
Well, 0.127777777 is what number? its smt like 1277777777/(10....0)
1277777777777 is not an even number, therefore -smt to the power of 12777777777 is a negative number, and 10...0th root from that is not a real number.
I'm reffering to a fact that a^(b/c) = (c-th root from)(a^b)
来源:https://stackoverflow.com/questions/4108411/c-pow-function-invalid-result