Try this
double d = 289358932.0;
double sqrt = Double.longBitsToDouble( ( ( Double.doubleToLongBits( d )-(1l<<52) )>>1 ) + ( 1l<<61 ) );
I haven't benchmarked it, but I'd expect it to be faster. The accuracy isn't extremely good, but try it out and see if it meets your needs. I think you can add an additional bias term a
to the end of the expression to make it more accurate.
EDIT: You can drastically improve the accuracy by passing it through a round or two of Newton's method
double better = (sqrt + d/sqrt)/2.0;
double evenbetter = (better + d/better)/2.0;
The second pass gives you almost the exact value of the square root.
sqrt 17022.533813476562
better 17010.557763511835
evenbetter 17010.553547724947
Math.sqrt() 17010.553547724423
sqrt()
necessary? Can you modify something somewhere else to deal with the squared value? Getting rid of asqrt
for the price of a*
is always a good idea. – Joonsqrt()
but could not. I need to compare two values (saya
andb
), and consider them equal if they are within somedelta
of each other. However, if all I have is the squares ofa
andb
, then I am unable to check if they are equal within that delta. – Yankeeism