Double.Min_Value无法按预期工作

时间:2014-10-14 10:38:18

标签: java

我有以下代码,但当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;
            }

3 个答案:

答案 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.因此您会注意到这种行为。