为什么Java中Math.pow(-0.9,0.8)的结果会产生NaN? [英] Why does the result of Math.pow(-0.9, 0.8) in Java produce NaN?
本文介绍了为什么Java中Math.pow(-0.9,0.8)的结果会产生NaN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当Java中涉及Double
或Float
数据类型的操作的结果产生一些不可预测的输出时,将其指定为NaN(不是数字).
为什么使用Math.pow()
的语句会产生NaN?
When the result of the operations involving Double
or Float
data types in Java produces some unpredicatable output, it is designated by NaN (Not a Number).
Why does the statement that uses Math.pow()
produce NaN?
System.out.println(Math.pow(-0.9, 0.8));
当然,这可能对您来说很容易,我无法弄清楚.
Of course, it may be easy for you, I can''t figure it out, please.
推荐答案
这就是它的工作原理.在这里,请阅读详细信息: Java.lang.Math.pow()方法 [ ^ ]
如果第一个参数是有限的并且小于零:
-如果第二个参数是有限偶数整数,则结果等于将第一个参数的绝对值提高到第二个参数的幂的结果
-如果第二个参数是一个有限的奇数整数,则结果等于将第一个参数的绝对值提高到第二个参数的幂的结果的负数
-如果第二个参数是有限的而不是整数,则结果为NaN.
That''s how it works. Here, read at the details: Java.lang.Math.pow() Method[^]
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.
您听说过复数吗?在不是复数的情况下,您曾听说过在实数中找不到负数的平方根吗?换句话说,方程式x² = −1
在读数上没有解?而且,当然,代表0.8和-0.9.
这就像是中学……而这种学校应该是开始计算的先决条件.
NaN表示非数字",这是IEEE 754浮动数字表示形式保留的重要对象,用于表示无效数字的概念.请参阅:
http://en.wikipedia.org/wiki/IEEE_754 [
Have you ever heard of complex number? In not complex number, have you ever heard that, in real numbers, square root of negative number cannot be found; in other words, the equationx² = −1
has no solutions in read numbers?! And, of course, the same stands for 0.8 and −0.9.
This is something like middle school… and such school should be a prerequisite for starting with computing.
And NaN means "not a number", important object reserved to IEEE 754 floating number representation to represent the concept of invalid number. Please see:
http://en.wikipedia.org/wiki/IEEE_754[^].
(Sigh…)
—SA
我猜这是因为结果很复杂,根据 ^ ],并且float/double类型无法表示它.
I''m guessing it''s because the result is complex, according to WolframAlpha[^], and the float/double type isn''t able to represent it.
这篇关于为什么Java中Math.pow(-0.9,0.8)的结果会产生NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文