[英]Possible loss of precision
我试图在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);
}
}
如果您看一下文档 ,它说, Math.pow()
需要两个doubles
,并返回一个double
。 当你将ints传递给这个函数时,它意味着没有坏处,因为将int
转换(转换)为double
意味着没有损失。 但是当你将值赋给int
,就意味着它会失去精度。
只需这样做:
int x = (int)Math.pow (2,4);
要么
double x = Math.pow (2,4);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.