简体   繁体   中英

Possible loss of precision

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);

 }
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM