java - Implementation of Math.isFinite() method -


to check if value nan,
can use double.isnan().
in grepcode,
implementation this.

public static boolean isnan(double v){     return (v != v); } 

double.isinfinite() this.

public static boolean isinfinite(double v){     return (v == double.positive_infinity) || (v == double.negative_infinity); }   

however, double.isfinite() this.

public static boolean isfinite(double v){     return math.abs(v) <= double.max_value; } 

so first question is,
why not returns !( double.isnan(v) || double.isinfinite(v) ) ?
there performance reasons?

i think faster b. wrong?

a. 1 != , 2 ==s, 2 ||s, , 1 !
b. abs() , <=

my second question is,
" identical ?"
is,
" method return true ? "

public static boolean test(){     long x = long.min_value;     for(;;){ //iterate long.min_value long.max_value         double d = double.longbitstodouble(x);         boolean = ! ( double.isnan(d) || double.isinfinite(d) );         boolean b = double.isfinite(d);         if(a != b) return false;         if(x == long.max_value) break;         x++;     }     return true; } 

math.abs(double) faster appears

return (a <= 0.0d) ? 0.0d - : a; 

as methods recognised intrinsic in hotspot , many other jvms. intrinsic replaced code same thing, more efficiently.

see vmsymbols.hpp list of symbols replaced hand coded machine code.

so, believing grepcode dangerous.

grepcode tells source looks like. hard second guess optimisations performed on java code when converted native code. jit has clever optimisations, dead code elimination can make optimisation faster/smarter run slower.

if in doubt, stick simplest code, , jit has special optimisation it. try tricky , might find, jit doesn't have optimisation , slower.


Comments