[英]Possible loss of precision
I am trying to run this code in java and getting following error, required int found double. 我试图在java中运行此代码并获得以下错误,需要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
. 如果您看一下文档 ,它说,
Math.pow()
需要两个doubles
,并返回一个double
。 When you pass ints to this function, it means no harm, because casting (converting) an int
to double
means no loss. 当你将ints传递给这个函数时,它意味着没有坏处,因为将
int
转换(转换)为double
意味着没有损失。 But when you assign the value to an int
, it means, it can lose precision. 但是当你将值赋给
int
,就意味着它会失去精度。
Simply do this: 只需这样做:
int x = (int)Math.pow (2,4);
or 要么
double x = Math.pow (2,4);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.