标准遵从性为什么在Java pow(1,qNaN)中是qNaN,而在IEEE 7542008 pow(1,qNaN)中是1?
为什么在Java中pow(1, qNaN)
是qNaN
,而在IEEE 754中pow(1, qNaN)
是1
爪哇:
System.out.println(Math.pow(1, Double.NaN)); // prints NaN (which is qNaN)
$ javac --version
javac 11.0.11
IEEE 754-2008,9.2.1特殊值:
pow (+1, y) is 1 for any y (even a quiet NaN)
# 1 楼答案
之所以如此,是因为
Math.pow
的javadoc指定它是这样的。上面写着:至于为什么是这样指定的,我能建议的最好方法是询问指定它的人
更新——经过进一步研究,我现在可以解释这种差异
Java发布时,IEEE 754的发布版本是1985年的版本。2008年,对754规范进行了重大修订。根据维基百科的IEEE 754-2008 page:
然而,Java设计者至少在10年前就设计并实现了Java的
Math.pow
功能,他们选择按照自己的想法实现NaN
行为。他们的想法与IEEE 754-2008标准委员会的想法不同幸运的是,IEEE 754-2008规定第9条是可选的,不要求符合标准。因此,Java设计人员采取了对现有Java开发人员干扰最小的路线,并决定不更改
pow
语义以匹配2008标准。(我认为这是正确的决定。)注意:我只是在推断这个原因。好吧,事实上的推理还没有公布