我用JAVA编写了以下方法:
public static float surface(float r)
{
return(4*Math.PI*Math.pow(r,2));
}
当我运行该类时,出现以下错误:
possible loss of precision
required: float; found: double
我在文档中看到 Math.pow 需要 double
才能工作。如果我需要使用 float
,我该怎么办?
为了测试,我尝试了以下方法,但给出了相同的错误:
public static float surface(float r)
{
return(4*Math.PI*r*r);
}
感谢您的帮助。
最佳答案
您需要将 double
类型转换为 float
。
public static float surface(float r)
{
return (float)(4*Math.PI*Math.pow(r,2));
}
关于JAVA : possible loss of precision,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965744/