Possible loss of accuracy

I try to run this code in Java and get the following error message: int found double.

public class UseMath{ public static void main(String[]args){ int x = Math.pow (2,4); System.out.println ("2 to the power of 4 is" + x); } } 
+4
source share
1 answer

If you look at the documentation , it says that Math.pow() expects two doubles and returns double.When you pass ints to this function, it means no harm, because casting (converting) int to double means no loss. But when you assign an int value, it means that it may lose precision.

Just do the following:

 int x = (int)Math.pow (2,4); 

or

 double x = Math.pow (2,4); 
+12
source

All Articles