有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    之所以如此,是因为Math.powjavadoc指定它是这样的。上面写着:

    public static double pow​(double a, double b)

    Returns the value of the first argument raised to the power of the second argument. Special cases:

    • If the second argument is positive or negative zero, then the result is 1.0.
    • If the second argument is 1.0, then the result is the same as the first argument.
    • If the second argument is NaN, then the result is NaN.
    • ....

    至于为什么是这样指定的,我能建议的最好方法是询问指定它的人


    更新——经过进一步研究,我现在可以解释这种差异

    Java发布时,IEEE 754的发布版本是1985年的版本。2008年,对754规范进行了重大修订。根据维基百科的IEEE 754-2008 page

    "[Clause 9] is new; it recommends fifty operations, including log, power, and trigonometric functions, that language standards should define.

    然而,Java设计者至少在10年前就设计并实现了Java的Math.pow功能,他们选择按照自己的想法实现NaN行为。他们的想法与IEEE 754-2008标准委员会的想法不同

    幸运的是,IEEE 754-2008规定第9条是可选的,不要求符合标准。因此,Java设计人员采取了对现有Java开发人员干扰最小的路线,并决定不更改pow语义以匹配2008标准。(我认为这是正确的决定。)

    注意:我只是在推断这个原因。好吧,事实上的推理还没有公布