-3
那麼我有以下程序。這是我的老師給的樂趣。我對結果感到驚訝。Double vs Float - Java
代碼:
public class Testing {
public static void main(String[] args) {
float piF = 3.141592653589793f; // value assigned has float precision
double piD = 3.141592653589793; // value assigned has double precision
final double THRESHOLD = .0001;
if(piF == piD)
System.out.println("piF and piD are equal");
else
System.out.println("piF and piD are not equal");
if(Math.abs(piF - (float) piD) < THRESHOLD)
System.out.println("piF and piD are considered equal");
else
System.out.println("piF and piD are not equal");
}
}
結果:
piF and piD are not equal
piF and piD are considered equal
那麼爲什麼PIF和PID不相等?而實際上Math.abs()
這樣做是否使兩者相同?
@Pooya它不重複......請閱讀我的問題,它是多樣的。 –
我不知道我的問題爲什麼被低估。請告訴我爲什麼它被低估了。 –