java - 浮点 - NaN

标签 java floating-point nan floating-point-precision

我正在从事一个关于java的研究项目,其中必须完成一些艰难的计算。然而,我已经完成了大部分工作,但停留在某个点上。我必须计算以下内容:

(2.1-2.3) raised to power 0.3.

但我得到了答案NaN..我尝试使用floatdouble变量存储结果,但它显示相同的结果。奇怪的是,当我用计算器做同样的事情时,它显示结果 -0.430512

我不知道如何让它与java一起工作
相同的代码是:

Math.pow((provider1[k][a][m]-provider1[k][j][m]),prior[k][m]);. 

当上述值为 2.12.30.3 时,输出为 NaN

但是,当值为 2.12.33 时,它会获得正确的值 (-0.08 )。有人可以帮忙如何做到这一点吗?

最佳答案

根据Math.pow Javadocs :

  • 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/

相关文章:

java - 有关带有递归方法的代码的问题

java - 在 MVC 中从 WCF 服务反序列化 Json 时出错

python - 在 Python 中仅打印有效数字

python - 计算由给定 float 近似的最短十进制数

python - 期望值中的 NaN,即使被屏蔽,也会在权重矩阵中引入 NaN

Javascript:解析 csv 中的字符串返回 NaN (不是数字)

java - 在 Java Runtime Exec 中使用重定向运算符

Java,使类不可变

python - 浮点到 IEEE-754,分为两个 16 位字

python - 使用 Python 用全 0 值填充空白数据框列