Java square root integer operation is not cast?
Math. The sqrt() function takes double as the parameter and returns double I'm using a program that uses a perfect square grid in all cases. I need to get the square root as an integer The only way to do this is to convert the parameter to int and then return an int cast double?
Solution
Casting technology is much better than
The conversion from int to double is accurate
Specify math Sqrt, for a normal positive number, returns "the double value closest to the true mathematical square root of the parameter value" If the input is a perfect square int, the result will be an integer double, which is in the range of int
Similarly, the double conversion of the integer value to int is also accurate
The net effect is that casting technology does not introduce any rounding error in your case
If your program would otherwise benefit from using the guava intmath API, you can use it to perform the square root, but I won't add a dependency on the API to avoid conversion