美文网首页
Java erf函数、erfc函数代码实现

Java erf函数、erfc函数代码实现

作者: 向祥祥 | 来源:发表于2020-04-13 11:23 被阅读0次

高斯误差函数是一个非基本函数,定义为




代码实现根据的公式


public class NumeralCaculate {
    public static void main(String[] args) {
        System.out.println(erfc(0.5));
    }
    //erfc函数
    public static double erfc(double x)
    {
        return 1-erf(x);
    }
    //erf函数
    public static double erf(double x)
    {
        double result = 0;
        int index = 0;
        do
        {
            index++;
        } while (x / Math.pow(10, index) > 1e-3);//设置计算精度
        int maxIndex =(int) Math.pow(10, index);
        double deltaX = x / maxIndex;
        for (int i = 0; i <=maxIndex; i++)
        {
            if (i > 0 && i<maxIndex)
            {
                result += 2 * Math.exp(-Math.pow(deltaX * i, 2));
                continue;
            }
            else if (i == maxIndex)
            {
                result += Math.exp(-Math.pow(deltaX * i, 2));
                continue;
            }
            else if(i==0){
                result += Math.exp(-Math.pow(deltaX * i, 2));
                continue;
            }
        }
        return result*deltaX/Math.pow(Math.PI,0.5);
    }
}

相关文章

网友评论

      本文标题:Java erf函数、erfc函数代码实现

      本文链接:https://www.haomeiwen.com/subject/akhgmhtx.html