当a是负数而b是非整数时,为什么Math.pow(a,b)是NaN? [英] Why Math.pow(a, b) is NaN when a is a negative number and b is a non-integer?
问题描述
我观察到以下Javascript行为:
I observed the following Javascript behavior:
> Math.pow(4, 2)
16
> Math.pow(4, 2.1)
18.37917367995256
> Math.pow(4, 0.5)
2
> Math.pow(-4, 2)
16
> Math.pow(-4, 2.1)
NaN
> Math.pow(-4, 0.5)
NaN
为什么给出一个负数和一个非整数但有理数,使 Math.pow
返回 NaN
?
Why giving a negative number and a non-integer but rational number, makes Math.pow
to return NaN
?
例如,为什么 Math.pow(4,0.5)
不 NaN
但是 Math.pow(4,-0.5)
NaN
?
For example, why Math.pow(4, 0.5)
is not NaN
but Math.pow(4, -0.5)
is NaN
?
推荐答案
Imaginary (Complex) Number Alert!
我也有一个疯狂的结果。所以我在这里有不同编程语言中的相同逻辑。
I have got a crazy result too. So what I have here is the same logic in different programming languages.
我的感觉是,当计算功率时,得到的数字是-1的根,是虚数。这些程序无法处理想象中的数字是我的猜测。
What I feel is, when the power is calculated, the resulting number is root of -1, which is an imaginary number. These programs cannot handle imaginary numbers is my guess.
结果如下:
C#
Math.Pow(-1.1, -1.1); // Returns NaN
Java 输出
Math.pow(-4, 2.1); // Returns NaN
JavaScript
Math.pow(-4, 2.1); // Returns NaN
这篇关于当a是负数而b是非整数时,为什么Math.pow(a,b)是NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!