我正在从事一个关于java的研究项目,其中必须完成一些艰难的计算。然而,我已经完成了大部分工作,但停留在某个点上。我必须计算以下内容:
(2.1-2.3) raised to power 0.3.
但我得到了答案NaN
..我尝试使用float
和double
变量存储结果,但它显示相同的结果。奇怪的是,当我用计算器做同样的事情时,它显示结果 -0.430512
我不知道如何让它与java一起工作
相同的代码是:
Math.pow((provider1[k][a][m]-provider1[k][j][m]),prior[k][m]);.
当上述值为 2.1
、2.3
和 0.3
时,输出为 NaN
。
但是,当值为 2.1
、2.3
和 3
时,它会获得正确的值 (-0.08
)。有人可以帮忙如何做到这一点吗?
最佳答案
- If the first argument is finite and less than zero
- if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
- if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
- if the second argument is finite and not an integer, then the result is NaN.
(强调我的)
那是因为raising a negative number to a non-integral power yields a complex number 。一种情况是无法计算数字的平方根:(-1)^0.5
。结果并不存在于实数中,它启发了虚数i
,在数学中,虚数被定义为-1
的平方根。一般来说,负数的非整数次幂会产生一个复数,它是实数和虚数之和。
关于java - 浮点 - NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491602/