Why does Java 8 Nashorn (JavaScript) modulo returns 0.0 (double) instead of 0 (integer)? -
consider following code sample:
import javax.script.scriptengine; import javax.script.scriptenginemanager; public class tester { public static void main( string[] args ) throws exception { scriptengine se = new scriptenginemanager().getenginebyname( "nashorn" ); object eval = se.eval( "5%5" ); system.out.println( "eval = " + eval ); system.out.println( "eval.getclass() = " + eval.getclass() ); } }
why produces following output?
eval = 0.0
eval.getclass() = class java.lang.double
the result type java.lang.double
weird.
in case remainder different 0 correctly returns java.lang.integer
, e.g. 5%2
returns java.lang.integer' value
1`.
only 0 somehow special.
trying same javascript expression in firefox 32.0.2 (findbugs console) works fine , returns plain 0.
is there way force nashorn return integer type instead of double?
there no integers in javascript.
start ecmascript section 8: types:
the ecmascript language types undefined, null, boolean, string, number, , object.
then see ecmascript section 8.5: number type:
the number type has 18437736874454810627 (that is, 264−253+3) values, representing double-precision 64-bit format ieee 754 values ..." (emphasis added)
the fact firefox displays floating point value 1 "1" rather "1.0" irrelevant, , confusing you.
Comments
Post a Comment