Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

My program uses a perfect square grid in all circumstances with Math.sqrt() function. But Math.sqrt() takes double as an argument and returns a double. Can anyone tell me how to obtain the square root in Integer without casting? 

1 Answer

0 votes
by (19.7k points)

You can use Guava intMath API to do the square root. But it’s not a wise idea to add dependency on an API just to avoid the cast. In casting, when you give the input as a perfect square int, you’ll get an integer-valued double which is in the int range. So casting doesn’t introduce any rounding error when you try to convert int to double or integer-valued double back to an int. 

Interested in Java? Check out this Java tutorial by Intellipaat.

Related questions

0 votes
1 answer
asked Feb 12, 2021 in Python by adhiraj (4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...