我必须存储几个概率值确实很低的乘积(例如1E-80)。由于下溢,使用原始java double将导致零。我不希望该值变为零,因为稍后会出现一个更大的数字(例如1E100),它将使这些值处于double可以处理的范围内。

因此,我自己创建了一个不同的类(MyDouble),用于保存基本部分和指数部分。在进行计算(例如乘法)时,我将基础部分相乘,然后加上指数。

该程序使用原始double类型快速运行。但是,当我使用自己的类(MyDouble)时,程序确实很慢。我认为这是因为我每次必须创建新对象才能创建简单的操作,并且当不再需要这些对象时,垃圾收集器必须执行大量工作。

我的问题是,您是否认为有更好的方法可以解决此问题?如果没有,是否有办法让我可以使用自己的类(MyDouble)加快程序运行速度?

[注意:获取日志并稍后获取指数并不能解决我的问题]

MyDouble类:

public class MyDouble {
    public MyDouble(double base, int power){
    this.base = base;
    this.power = power;
    }

    public static MyDouble multiply(double... values) {
    MyDouble returnMyDouble = new MyDouble(0);
    double prodBase = 1;
    int prodPower = 0;
    for( double val : values) {
            MyDouble ad = new MyDouble(val);
            prodBase *= ad.base;
            prodPower += ad.power;
        }
        String newBaseString = "" + prodBase;
        String[] splitted = newBaseString.split("E");
        double newBase = 0; int newPower = 0;
        if(splitted.length == 2) {
            newBase = Double.parseDouble(splitted[0]);
            newPower = Integer.parseInt(splitted[1]);
        } else {
            newBase = Double.parseDouble(splitted[0]);
            newPower = 0;
        }
        returnMyDouble.base = newBase;
        returnMyDouble.power = newPower + prodPower;
        return returnMyDouble;
    }
}

最佳答案

速度慢可能是由于在拆分和字符串连接中创建的中间字符串对象。

试试这个:

/**
 * value = base * 10 ^ power.
 */

public class MyDouble {

    // Threshold values to determine whether given double is too small or not.
private static final double SMALL_EPSILON = 1e-8;
private static final double SMALL_EPSILON_MULTIPLIER = 1e8;
private static final int    SMALL_EPSILON_POWER = 8;

private double myBase;
private int    myPower;

public MyDouble(double base, int power){
    myBase  = base;
    myPower = power;
}

public MyDouble(double base)
{
    myBase  = base;
    myPower = 0;
    adjustPower();
}

/**
 * If base value is too small, increase the base by multiplying with some number and
 * decrease the power accordingly.
 * <p> E.g 0.000 000 000 001 * 10^1  => 0.0001 * 10^8
 */
private void adjustPower()
{
    // Increase the base & decrease the power
    // if given double value is less than threshold.
    if (myBase < SMALL_EPSILON) {
        myBase = myBase * SMALL_EPSILON_MULTIPLIER;
        myPower -= SMALL_EPSILON_POWER;
    }
}

/**
 * This method multiplies given double and updates this object.
 */
public void multiply(MyDouble d)
{
    myBase  *= d.myBase;
    myPower += d.myPower;
    adjustPower();
}

/**
 * This method multiplies given primitive double value with this object and update the
 * base and power.
 */
public void multiply(double d)
{
    multiply(new MyDouble(d));
}

@Override
public String toString()
{
    return "Base:" + myBase + ", Power=" + myPower;
}

/**
 * This method multiplies given double values and returns MyDouble object.
 * It make sure that too small double values do not zero out the multiplication result.
 */
public static MyDouble multiply(double...values)
{
    MyDouble result = new MyDouble(1);
    for (int i=0; i<values.length; i++) {
        result.multiply(values[i]);
    }
    return result;
}

public static void main(String[] args) {
    MyDouble r = MyDouble.multiply(1e-80, 1e100);
    System.out.println(r);
}

}

如果这样做仍然很慢,则可以修改multiple()方法以直接对原始double进行操作,而不用创建MyDouble对象。

09-28 07:27