以下是以下程序的輸出。執行雙值相等比較時應該是什麼ε值
value is : 2.7755575615628914E-17
Double.compare with zero : 1
isEqual with zero : true
我的問題是,應該是什麼的ε值?有沒有強大的方式來獲得價值,而不是從天空中挑選一個數字。
package sandbox;
/**
*
* @author yccheok
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double zero = 1.0/5.0 + 1.0/5.0 - 1.0/10.0 - 1.0/10.0 - 1.0/10.0 - 1.0/10.0;
System.out.println("value is : " + zero);
System.out.println("Double.compare with zero : " + Double.compare(zero, 0.0));
System.out.println("isEqual with zero : " + isEqual(zero, 0.0));
}
public static boolean isEqual(double d0, double d1) {
final double epsilon = 0.0000001;
return d0 == d1 ? true : Math.abs(d0 - d1) < epsilon;
}
}
@Cheok,你的問題不清楚,你對epsilon有什麼期待? – mhshams 2010-09-16 15:48:19