Nan在Javascript中与JDK8上的javax.scriptEngine的比较



根据IEEE 754 NaN,与任何数字的相等比较都应为false(Java和JavaScript语言)。但是在下面的代码中,使用javaxscriptEngine for JavaScript,与自身相比,设置为NaN的变量返回true。

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
    public static void main(String[] args) throws ScriptException {
        final ScriptEngineManager mgr = new ScriptEngineManager();
        final ScriptEngine engine = mgr.getEngineByName("JavaScript");
        System.out.println("neq: " + engine.eval("a1 = NaN; a1!=a1;"));
        System.out.println("eq: " + engine.eval("a1 = NaN; a1==a1;"));
        System.out.println("nid: " + engine.eval("a1 = NaN; a1!==a1;"));
        System.out.println("id: " + engine.eval("a1 = NaN; a1===a1;"));
    }
}

输出:

neq: true
eq: true
nid: true
id: false

使用oracle JDK:

java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

我的理解是"eq"表达式应该返回false。

当a1是NaN时,为什么a1==a1为True而不是False?

导致这种行为的原因似乎是赋值的副作用,或者更具体地说,是重新分配的副作用(在同一脚本中)。我将您的测试用例修改为

final ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
System.out.println("neq: " + engine.eval("a1 = NaN; a1!=a1;"));
System.out.println("eq: " + engine.eval("a1 = NaN; a1==a1;"));
System.out.println();
engine = mgr.getEngineByName("JavaScript");
System.out.println("eq: " + engine.eval("a1 = NaN; a1==a1;"));
System.out.println("neq: " + engine.eval("a1 = NaN; a1!=a1;"));
System.out.println();
engine = mgr.getEngineByName("JavaScript");
engine.eval("a1 = NaN;");
System.out.println("neq: " + engine.eval("a1!=a1;"));
System.out.println("eq: " + engine.eval("a1==a1;"));
System.out.println();
engine = mgr.getEngineByName("JavaScript");
engine.eval("a1 = NaN;");
System.out.println("eq: " + engine.eval("a1==a1;"));
System.out.println("neq: " + engine.eval("a1!=a1;"));

得到:

neq: true
eq: true
eq: false
neq: false
neq: false
eq: true
eq: true
neq: false

因此,当a1没有在引擎中重新分配时,它会表现出一致(不一定正确)的行为,而重新分配的结果不仅是错误的,而且是完全不一致的。

如果我添加这段代码

    engine.eval("a1 = NaN;");
    System.out.println("eq: " + engine.eval("a1==a1;"));
    System.out.println("neq: " + engine.eval("a1!=a1;"));
    System.out.println();
    engine.eval("a1 = NaN;a2 = NaN");
    System.out.println("eq: " + engine.eval("a1==a2;"));
    System.out.println("neq: " + engine.eval("a1!=a2;"));
    System.out.println();

它表明,即使没有重新分配,结果也是不一致的。我得到:

eq:真neq:错误

eq:falseneq:真实

相关内容

  • 没有找到相关文章

最新更新