Issue
I am trying to run this code in java and getting following error, required 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);
}
}
Solution
If you take a look at the documentation, it says, that Math.pow()
expects two doubles
, and returns a double
. When you pass ints to this function, it means no harm, because casting (converting) an int
to double
means no loss. But when you assign the value to an int
, it means, it can lose precision.
Simply do this:
int x = (int)Math.pow(2,4);
or
double x = Math.pow(2,4);
Answered By - Balázs Édes
Answer Checked By - Gilberto Lyons (JavaFixing Admin)