Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Math.sqrt() outputs wrong value in java?

Tags:

java

math

long mynum = Long.parseLong("7660142319573120");
long ans = (long)Math.sqrt(mynum) // output = 87522239
long ans_ans = ans * ans;

In this case, I am getting ans_ans > mynum where it should be <=mynum. Why such behavior? I tried this with node js as well. There also result is same.

like image 472
Abhilash Mandaliya Avatar asked Jun 10 '26 08:06

Abhilash Mandaliya


1 Answers

Math.sqrt operates on doubles, not longs, so mynum gets converted to a double first. This is a 64-bits floating point number, which has "15–17 decimal digits of precision" (Wikipedia).

Your input number has 16 digits, so you may be losing precision on the input already. You may also be losing precision on the output.

If you really need an integer square root of long numbers, or generally numbers that are too big for accurate representation as a double, look into integer square root algorithms.

You can also use LongMath.sqrt() from the Guava library.

like image 133
Thomas Avatar answered Jun 13 '26 04:06

Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!