According to IEEE 754 NaN equal comparison with any number should be false (both Java and JavaScript languages). But in the following code, using javax scriptEngine for JavaScript, a variable set to NaN compared with itself returns 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;"));
}
}
Output :
neq: true
eq: true
nid: true
id: false
Using 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)
My understanding is the 'eq' expression should return false.
Why is a1 == a1 True instead of False when a1 is NaN ?