Problem : I compiled below code and got possible loss of precision error.
int a = Math.pow(3, 5);
System.out.println(a);
Solution : According to the java documentation, Math.pow() expects two doubles, and returns a double. When you pass two int values to this method, it converts an int to double. But when we assign the value to an int, it gives, possible loss of precision error while compiling the code. So you can cast method to an int or assign the returning value to double to resolve the error.
int a = (int)Math.pow (3,5);
or
double a = Math.pow (3,5);
int a = Math.pow(3, 5);
System.out.println(a);
Solution : According to the java documentation, Math.pow() expects two doubles, and returns a double. When you pass two int values to this method, it converts an int to double. But when we assign the value to an int, it gives, possible loss of precision error while compiling the code. So you can cast method to an int or assign the returning value to double to resolve the error.
int a = (int)Math.pow (3,5);
or
double a = Math.pow (3,5);
0 comments:
Post a Comment