英文:
What does this error mean? "error: incompatible types: possible lossy conversion from double to int" Am I doing something wrong with the syntax?
问题
这是一个用于计算Fermat's Last Theorem的Java程序:
费马大定理指出,不存在整数a、b和c使得:
an + bn = cn
其中,除非n≤2。
编写一个名为Fermat.java的程序,该程序输入四个整数(a、b、c和n),并检查费马定理是否成立。
如果n大于2且an + bn = cn,程序应显示:
“Holy smokes,Fermat was wrong!”
否则,程序应显示:
“No,that doesn’t work。”
尝试将checkFermat
中的参数从int
更改为double
,但未成功。
这是我的代码:
import java.util.Scanner;
public class Fermat {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
checkFermat(1, 2, 4, 1);
}
public static void checkFermat(int a, int b, int c, int n) {
int result = ((Math.pow(a, n)) + (Math.pow(b, n)));
if ((((Math.pow(a, n)) + (Math.pow(b, n))) == (Math.pow(c, n))) && (n != 2)) {
System.out.println("Holy smokes, Fermat was wrong!");
} else {
System.out.println("No, that doesn't work.");
}
}
}
以下是错误信息:
code/Fermat.java:11: error: incompatible types: possible lossy conversion from double to int
int result = ((Math.pow(a, n)) + (Math.pow(b, n)));
^
1 error
英文:
This is a java program to calculate Fermat's Last Theorem:
Fermat’s Last Theorem says that there are no integers a , b , and c such that
> a<sup>n</sup>+b<sup>n</sup>=c<sup>n</sup>
except when n≤2 .
Write a program named Fermat.java that inputs four integers (a, b, c, and n) and checks to see if Fermat’s theorem holds.
If n is greater than 2 and a<sup>n</sup>+b<sup>n</sup>=c<sup>n</sup> , the program should display
“Holy smokes, Fermat was wrong!”
Otherwise the program should display
“No, that doesn’t work.”
Tried making the parameters in checkFermat
from int
to double
with no success.
This is my code:
import java.util.Scanner;
public class Fermat{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
checkFermat(1,2,4,1);
}
public static void checkFermat(int a,int b,int c,int n){
int result = ((Math.pow (a,n))+(Math.pow(b,n)));
if ((((Math.pow(a, n)) + (Math.pow(b, n))) == (Math.pow(c, n))) && ((n != 2))) {
System.out.println("Holy smokes, Fermat was wrong!");
}else{
System.out.println("No, that doesn't work.");
}
}
}
Here is the error:
code/Fermat.java:11: error: incompatible types: possible lossy conversion from double to int
int result = ((Math.pow (a,n))+(Math.pow(b,n)));
^
1 error
答案1
得分: 1
你所使用的方法位于java.lang.Math
中,使用方式为public static double pow(double, double)
。它期望底数和指数都是double
类型。在这种情况下(传递整数),由于隐式类型转换从int
到double
,它可以正常工作。因为double
具有更高的层次结构,更多的字节数,更高的精度,因此可以存储更大范围的数字。
事情变得非常棘手的是当你反转操作时,这在这个例子中可以注意到。
code/Fermat.java:11: error: incompatible types: possible lossy conversion from double to int
int result = ((Math.pow(a, n)) + (Math.pow(b, n)));
^
1 error
在这里,类型需要从double
转换为int
,因为Math.pow(double, double)
的返回类型是double
。类型应该是double
,或者任何其他在层次结构中比double
更高的数据类型。然而不是这样,存储值是int
,远小于double
,可能会导致数据丢失,就精度而言。也许返回值是浮点值,在涉及整数的情况下是不可能的,但是在范围问题上,一些数的平方可能会得到一个非常大的值,无法在int
中存储,因此你看到的错误消息是为了通知你类型不合适,可能会导致精度丢失。
如何解决这个问题?
-
要么使用适当的数据类型
(推荐)使用预期用于保存返回数据的数据类型。
例如:double result = ((Math.pow(a, n)) + (Math.pow(b, n)));
-
显式类型转换
明确告诉编译器将数据从double
转换为int
,不太关心数据。
例如:int result = (int)((Math.pow(a, n)) + (Math.pow(b, n)));
英文:
The method you use lies within java.lang.Math
which goes as public static double pow(double, double)
. It expects both the base and exponent to be of the type double
. Which works fine in this case (of passing integers) due to implicit type converging form int
→ double
, as double has higher hierarchy, more number of bytes, greater precision and hence can store larger range of numbers.
Things become quite tricky when you reverse the operation. Which in this can can be noticed.
code/Fermat.java:11: error: incompatible types: possible lossy conversion from double to int
int result = ((Math.pow (a,n))+(Math.pow(b,n)));
^
1 error
Here, the type needs to be converted from double
→ int
as the return type of Math.pow(double, double)
is of type double
. The type should be either double
or any other data type which will be more in hierarchy position than double
. Which is not, the storing value is int
which is way less than double
and can cause loss of data, in terms of accuracy. Maybe returning value is a floating value which is not possible when it comes to integer numbers but range issue, some squaring of numbers may take a very large value incapable of storing within int
, hence the error message you see is to inform you that the types is inappropriate and might be a cause for loss in precision.
How to overcome this ? <br>
1. Either use an appropriate data type <br>
(Recommended) Use the data type as expected for holding the returned data.<br>
For example: double result = ((Math.pow (a,n))+(Math.pow(b,n)));
2. Explicit type casting <br>
Explicitly say the compiler to cast down the data from double
→ int
caring less about the data. <br>
For example: int result = (int)((Math.pow (a,n))+(Math.pow(b,n)));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论