BigDecimal

时间:2016-08-17 20:16:55

标签: java bigdecimal

我想用Poisson计算一些东西。 问题是,它只适用于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

1 个答案:

答案 0 :(得分:1)

使用BigDecimal做所有事情。例如,使用BigDecimal.pow()而不是Math.pow()。或者清理算法 - 在计算Poisson时不需要使用pow()或exp()(我在今天的Poisson早期版本中展示了如何)