【问题标题】:Getting 'Infinity' output instead of actual numbers获得“无限”输出而不是实际数字
【发布时间】:2016-08-06 14:26:34
【问题描述】:

我正在练习一些 Java,我正在编写的一个应用程序要求输出未来 75 年的世界人口。

我正在使用人口增长模型。我的问题是我的应用程序在应该输出估计人口的列中输出“无限”。

这是我的代码:

import java.util.Calendar;
import java.util.regex.Matcher;

public class WorldPopulationGrowth {
    public static void main(String[] args) {
        double currentWorldPopulation = 7.4e9;
        double worldPopulationGrowthRate = 1.13;
        double anticipatedWorldPopulation;
        int initialYear = Calendar.getInstance().get(Calendar.YEAR);

        System.out.println("Year\tAnticipated World Population (in billions)\tPopulation " +
                "increase since last year");
        System.out.println(String.format("%d\t%.1e\t\t\t\t\t\t\t\t\t\t\tNA", initialYear, currentWorldPopulation) );


        for(int i=1; i < 76; i++){
            int year = initialYear + i;
            double growthExponential = worldPopulationGrowthRate*year*1.0;
            anticipatedWorldPopulation =  currentWorldPopulation * Math.pow(Math.E, growthExponential);

            System.out.println(String.format("%d\t%.1e\t\t\t\t\t\t\t\t\t\t\t", year, anticipatedWorldPopulation));

            currentWorldPopulation = anticipatedWorldPopulation;
        }

    }
}

【问题讨论】:

  • 你试过通过它调试吗?对于每次迭代,您的 growthExponential 正乘以当前年份,这肯定会影响您的数学运算
  • 您似乎还通过使用Math.E 对您的数学做其他不正确的事情,这本身将在每次迭代中增加近三倍currentPopulation,但您将其提高到每个数千的幂时间..
  • 增长应该由公式pow(GrowthRate, i)exp(log(GrowthRate)*i)

标签: java math infinity


【解决方案1】:

让我们仔细看看您的代码的第一次迭代,就好像我们正在调试它一样(确保您以后尝试这样做!)

  • currentWorldPopulation = 7.4e9
  • worldPopulationGrowthRate 是 1.13
  • initialYear 是 2016 年
  • 你的循环开始了,i 是 1
    • year 设置为 2017
    • growthExponential 设置为 1.13 * 2017 = 2279.21(这是问题的开始)
    • anticipatedWorldPopulation 设置为 7.4e9 * e^2279.21
    • 这大约是 7.4e9 * 7.05e989... KABOOM

重新审视您的计算,并逐步检查您的应用程序(最好在调试器中)以查看您的问题。

【讨论】:

  • 这是正确答案。我建议 OP 接受并支持这个。好眼力,@Krease
  • 谢谢,搞定了。我使用百分比增长作为 1.13 而不是正确的 0.0113,而不是年份,我用计数器 i 替换年份。
  • 如果我有足够的声望,我会支持这个! :)
【解决方案2】:

@Krease 发现了你的问题。

我重新编码了。一旦你解决了问题,他发现它很好。我使用了 JDK 8 lambda,并为您提供了百分比和指数增长模型。代码打印两者进行比较:

import java.util.function.DoubleFunction;


/**
 * Simplistic population growth model
 * @link  https://stackoverflow.com/questions/38805318/getting-infinity-output-instead-of-actual-numbers/38805409?noredirect=1#comment64979614_38805409
 */
public class WorldPopulationGrowth {

    public static void main(String[] args) {
        double currentWorldPopulation = 7.4e9;
        double worldPopulationGrowthRate = 1.13;
        int numYears = 76;
        int startYear = 1976;

        double populationExponential = currentWorldPopulation;
        ExponentialGrowthModel modelExpGrowth = new ExponentialGrowthModel(worldPopulationGrowthRate);
        double populationPercentage = currentWorldPopulation;
        PercentageGrowthModel modelPercentGrowth = new PercentageGrowthModel(worldPopulationGrowthRate);

        System.out.println(String.format("%10s %20.3e %20.3e", startYear, currentWorldPopulation, currentWorldPopulation));
        for (int i = 1; i < numYears; ++i) {
            populationExponential = modelExpGrowth.apply(populationExponential);
            populationPercentage = modelPercentGrowth.apply(populationPercentage);
            System.out.println(String.format("%10s %20.3e %20.3e", startYear+i, populationExponential, populationPercentage));
        }
    }
}

class ExponentialGrowthModel implements DoubleFunction<Double> {

    private double exponent;

    ExponentialGrowthModel(double exponent) {
        this.exponent = exponent;
    }

    private double getExponent() {
        return exponent;
    }

    public void setExponent(double exponent) {
        this.exponent = exponent;
    }

    @Override
    public Double apply(double value) {
        return value*Math.exp(this.getExponent());
    }
}

class PercentageGrowthModel implements DoubleFunction<Double> {

    private double percentageIncrease;

    PercentageGrowthModel(double percentageIncrease) {
        this.percentageIncrease = percentageIncrease;
    }

    private double getPercentageIncrease() {
        return percentageIncrease;
    }

    public void setPercentageIncrease(double percentageIncrease) {
        this.percentageIncrease = percentageIncrease;
    }

    @Override
    public Double apply(double value) {
        return value*(this.getPercentageIncrease());
    }
}

【讨论】:

  • 数学差的远不止这些 ;) 看看我的 cmets 的问题。
  • 现在我更仔细地阅读了代码,我可以看出你是对的。 OP 将功率模型与致命效应的年度百分比增加混淆了。
猜你喜欢
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2013-11-16
相关资源
最近更新 更多