我有以下代码,但当mutuaInformation
为零时,它不会通过if。知道出了什么问题吗?
int largestEntropyIndex = Integer.MIN_VALUE;
double largestMutualInfomation = Double.MIN_VALUE;
for (int i = 0; i < attributes.size(); i++) {
String attrName=this.attributes.get(i);
double conditionalEntropy = calcConditionalEntropy(i,
instances,
this.attributeValues.get(attrName).size(),
this.labels.size());
System.out.println("conditional entropy is: "+conditionalEntropy);
double mutualInformation = entropy - conditionalEntropy;
if (mutualInformation > largestMutualInfomation){
largestMutualInfomation = mutualInformation;
largestEntropyIndex = i;
}
答案 0 :(得分:2)
Double.MIN_VALUE
包含正数,因此0 < Double.MIN_VALUE
。
/**
* A constant holding the smallest positive nonzero value of type
* <code>double</code>, 2<sup>-1074</sup>. It is equal to the
* hexadecimal floating-point literal
* <code>0x0.0000000000001P-1022</code> and also equal to
* <code>Double.longBitsToDouble(0x1L)</code>.
*/
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
您应该使用一些任意负值(或者Double.NEGATIVE_INFINITY
)。
答案 1 :(得分:2)
常数Double.MIN_VALUE
等于4.9E-324
。虽然非常小,但仍然大于绝对0。
答案 2 :(得分:2)
执行简单的代码,你应该得到它
public static void main(String args[]) {
System.out.println("Min value for Double : " + Double.MIN_VALUE);
if(0 > Double.MIN_VALUE) {
System.out.println("0 is greater than Double.MIN_VALUE");
}
else {
System.out.println("0 is less than Double.MIN_VALUE");
}
}
,输出
Min value for Double : 4.9E-324
0 is less than Double.MIN_VALUE
如您所见,Double.MIN_VALUE
的值超过0.因此您会注意到这种行为。