gpt4 book ai didi

BigDecimal 上的 java.lang.NumberFormatException

转载 作者:行者123 更新时间:2023-11-30 07:12:29 25 4
gpt4 key购买 nike

我想用泊松计算一些东西。问题是,它只适用于 183 中的值...

我的代码:

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;


public class CopyOfPoisson {

public static void main(String[] args) {

double anrufe = 360;
double minuten = 30;
double dauer = 240;

double agenten = 0; //m


for(int i = 49; i<=500; i++)
{
agenten = i;
double callsPerSecond = anrufe/(minuten*60);

//u
double trafficIntensitiy = callsPerSecond*dauer;
//p
double p = trafficIntensitiy / agenten;
System.out.println("Index: "+ i + " "+ erlangC(agenten,trafficIntensitiy, p));
}




}

public static BigDecimal erlangC (double m, double u, double p)
{
BigDecimal zaehler = berechnePoisson(m, u, false);
BigDecimal nenner = berechnePoisson(m, u, false).add((BigDecimal.ONE.subtract(new BigDecimal(p))).multiply( berechnePoisson(m, u, true)));
return zaehler.divide(nenner,100,RoundingMode.HALF_EVEN);
}

public static BigDecimal berechnePoisson(double m, double u, boolean va)
{

BigDecimal answer = BigDecimal.ZERO;
if(!va)
{
BigDecimal myOwn = new BigDecimal(Double.toString(Math.exp(-u)* Math.pow(u, m)));
answer = myOwn.divide(fakultaet2(m),100,RoundingMode.HALF_EVEN);
}
if(va)
{
for(int i = 0; i < m; i++)
{
BigDecimal myOwn2 = new BigDecimal(Double.toString(Math.exp(-u)*Math.pow(u, i)));
answer = answer.add(myOwn2.divide(fakultaet2(i),100,RoundingMode.HALF_EVEN));
}
}

return answer;
}


public static BigDecimal fakultaet2 (double n)
{
BigDecimal fct = BigDecimal.valueOf(1);
for(int i = 1; i<=n; i++)
{
fct = fct.multiply(BigDecimal.valueOf(i));
}

return fct;
}

}

循环达到 184 后,出现错误:

Exception in thread "main" java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:494)
at java.math.BigDecimal.<init>(BigDecimal.java:383)
at java.math.BigDecimal.<init>(BigDecimal.java:806)
at CopyOfPoisson.berechnePoisson(CopyOfPoisson.java:47)
at CopyOfPoisson.erlangC(CopyOfPoisson.java:36)
at CopyOfPoisson.main(CopyOfPoisson.java:26)

上线

BigDecimal myOwn = new BigDecimal(Double.toString(Math.exp(-u)* Math.pow(u, m)));

有什么问题吗?我想计算到 i <= 500 ,但仅计算为 i = 183 .

最佳答案

使用 BigDecimal 完成所有操作。例如,使用 BigDecimal.pow() 而不是 Math.pow()。或者清理算法 - 计算泊松时不需要使用 pow() 或 exp() (我在今天关于泊松的早期线程中展示了如何操作)

关于BigDecimal 上的 java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39005535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com