[英]Why does this cout << pow(-3, (1.0 / 3)) return -nan(ind)?
Why does this:为什么会这样:
cout << pow(-3, (1.0 / 3))
return返回
-nan(ind)
whereas然而
pow(-3, 3)
and和
pow(3, (1.0 / 3))
both work fine ?两者都工作正常吗?
The pow function is used only for positive numbers. pow 函数仅用于正数。
See the man page of pow function :请参阅pow 功能的手册页:
If x is a finite value less than 0, and y is a finite noninteger, a domain error occurs, and a NaN is returned.
如果 x 是小于 0 的有限值,并且 y 是有限非整数,则会发生域错误,并返回 NaN。
To get the power of number you can try like this:要获得数字的力量,您可以这样尝试:
int main() {
int exp;
float base, power = 1;
cout << "Base and exponent : ";
cin >> base >> exp;
while (exp != 0) {
power *= base;
--exp;
}
cout << "Output = " << power;
return 0;
}
pow(x, y)
for floating point types is probably implemented as exp(y * ln(x))
.浮点类型的
pow(x, y)
可能实现为exp(y * ln(x))
。
ln(x)
fails for negative numbers, or zero. ln(x)
对于负数或零失败。
For a full reference, see http://en.cppreference.com/w/cpp/numeric/math/pow有关完整参考,请参阅http://en.cppreference.com/w/cpp/numeric/math/pow
This is because mathematically the fractional power of a negative number delivers a complex number.这是因为在数学上,负数的分数幂会产生一个复数。
Reference for std::pow states that " pow(base, exp)
returns NaN and raises FE_INVALID if base is finite and negative and exp is finite and non-integer." std::pow 的参考说明“如果 base 为有限且为负且 exp 为有限且非整数,则
pow(base, exp)
返回 NaN 并引发 FE_INVALID。”
This is plain mathemetics:这是简单的数学:
You can find the list of special case on the cppreference.com pow page:您可以在cppreference.com pow 页面上找到特殊情况列表:
Errors are reported as specified in math_errhandling.按照 math_errhandling 中的规定报告错误。
If base is finite and negative and exp is finite and non-integer, a domain error occurs and a range error may occur.如果 base 有限且为负,exp 有限且非整数,则会出现域错误并可能出现范围错误。
If base is zero and exp is zero, a domain error may occur.如果 base 为零且 exp 为零,则可能会出现域错误。
If base is zero and exp is negative, a domain error or a pole error may occur.如果 base 为零且 exp 为负,则可能会出现域错误或极点错误。
If the implementation supports IEEE floating-point arithmetic (IEC 60559),如果实现支持 IEEE 浮点运算 (IEC 60559),
pow(+0, exp)
, where exp is a negative odd integer, returns +∞ and raises FE_DIVBYZERO pow(+0, exp)
,其中 exp 是负奇数,返回 +∞ 并引发 FE_DIVBYZEROpow(-0, exp)
, where exp is a negative odd integer, returns -∞ and raises FE_DIVBYZERO pow(-0, exp)
,其中 exp 是负奇数,返回 -∞ 并引发 FE_DIVBYZEROpow(±0, exp)
, where exp is negative, finite, and is an even integer or a non-integer, - returns +∞ and raises FE_DIVBYZERO pow(±0, exp)
,其中 exp 为负、有限且为偶数或非整数, - 返回 +∞ 并引发 FE_DIVBYZEROpow(±0, -∞)
returns +∞ and may raise FE_DIVBYZERO pow(±0, -∞)
返回 +∞ 并可能提高 FE_DIVBYZEROpow(+0, exp)
, where exp is a positive odd integer, returns +0 pow(+0, exp)
,其中 exp 是一个正奇数,返回 +0pow(-0, exp)
, where exp is a positive odd integer, returns -0 pow(-0, exp)
,其中 exp 是一个正奇数,返回 -0pow(±0, exp)
, where exp is positive non-integer or a positive even integer, returns +0 pow(±0, exp)
,其中 exp 是非正整数或正偶数,返回 +0pow(-1, ±∞)
returns 1 pow(-1, ±∞)
返回 1pow(+1, exp)
returns 1 for any exp, even when exp is NaN pow(+1, exp)
为任何 exp 返回 1,即使 exp 是 NaNpow(base, ±0)
returns 1 for any base, even when base is NaN pow(base, ±0)
为任何基数返回 1,即使基数为 NaNpow(base, exp)
returns NaN and raises FE_INVALID if base is finite and negative and exp is finite and non-integer. pow(base, exp)
返回 NaN 并在 base 为有限且为负且 exp 为有限且非整数时引发 FE_INVALID。pow(base, -∞)
returns +∞ for any |base|<1 pow(base, -∞)
为任何 |base|<1 返回 +∞pow(base, -∞)
returns +0 for any |base|>1 pow(base, -∞)
对任何 |base|>1 返回 +0pow(base, +∞)
returns +0 for any |base|<1 pow(base, +∞)
对任何 |base|<1 返回 +0pow(base, +∞)
returns +∞ for any |base|>1 pow(base, +∞)
为任何 |base|>1 返回 +∞pow(-∞, exp)
returns -0 if exp is a negative odd integerpow(-∞, exp)
返回 -0pow(-∞, exp)
returns +0 if exp is a negative non-integer or even integerpow(-∞, exp)
返回 +0pow(-∞, exp)
returns -∞ if exp is a positive odd integer pow(-∞, exp)
如果 exp 是正奇数,则返回 -∞pow(-∞, exp)
returns +∞ if exp is a positive non-integer or even integer pow(-∞, exp)
如果 exp 是正的非整数或偶数,则返回 +∞pow(+∞, exp)
returns +0 for any negative exp pow(+∞, exp)
对于任何负 exp 返回 +0pow(+∞, exp)
returns +∞ for any positive exp except where specified above, if any argument is NaN, NaN is returned pow(+∞, exp)
对于任何正 exp 返回 +∞,除非上面指定,如果任何参数是 NaN,则返回 NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.