首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用java的maclaurin级数arccos计算出了什么问题?

使用java的maclaurin级数arccos计算出了什么问题?
EN

Stack Overflow用户
提问于 2016-09-08 04:46:40
回答 2查看 225关注 0票数 0

我正在使用mclaurins级数来计算arccos(x^2-1),但当我将其与math.acos的结果进行比较时,它就不同了。下面是我的代码:

代码语言:javascript
复制
    public class Maclaurin {

    public static int factorial(int fact) {
        if(fact==0)
            return 1;
        return fact*factorial(fact-1);
    }

    public static void main(String[] args) {
    int i, j;
    double function = 0, x,result;

    x=0;

        for (int n = 0; n < 8; n++) {

            function=((factorial(2*n))/(Math.pow(4, n)*Math.pow(factorial(n),2)*(2*n+1)))*Math.pow((Math.pow(x, 2)-1),2*n+1);
            result=(Math.PI/2)-function;

            System.out.println("x= "+x+" y= "+result);
            System.out.println("Test "+Math.acos(Math.pow(x, 2)-1));
            x+=0.13;
        }




    }


    }

Programm output. test is a value calculated with Math.arccos and it differs from y calculated with mclaurins formula:

    x= 0.0 y= 2.5707963267948966
    Test 3.141592653589793
    x= 0.13 y= 1.7291549939933966
    Test 2.9574849820283498
    x= 0.26 y= 1.6236496851024964
    Test 2.771793621843802
    x= 0.39 y= 1.5848621264898726
    Test 2.5828078861333155
    x= 0.52 y= 1.5725761587226181
    Test 2.3885331918392687
    x= 0.65 y= 1.5708496332463704
    Test 2.1864594293995867
    x= 0.78 y= 1.570796415168701
    Test 1.9731661516473589
    x= 0.91 y= 1.5707963267948972
    Test 1.7435543826662978

编辑:新的代码,其中计算在maclaurin函数中,我从main函数调用它。适用于除前3: package maclaurin之外的所有值;

代码语言:javascript
复制
public class Maclaurin {
    private static double x;

//public static int factorial(int fact) {
//    if(fact==0)
//        return 1;
//    return fact*factorial(fact-1);
//}
   public static double factorial(int fact) {
    if(fact==0)
        return 1;
    return fact*factorial(fact-1);
}

public static void main(String[] args)
    {
         x = 0;
        for (int i=0;i<8;i++)
            {
                maclaurin(x);
                x=x+0.14;
            }

    }

public static void maclaurin(double value){
    double function = 0, x, result;

    x =value;

    for (int n = 0; n < 20; n++) {
        function += ((factorial(2 * n)) / (Math.pow(4, n) * Math.pow(factorial(n), 2) * (2 * n + 1)))
                * Math.pow((Math.pow(x, 2) - 1), 2 * n + 1);
    }

    result = (Math.PI / 2) - function;
    System.out.println("x= " + x + " y= " + result);
    System.out.println("Test " + Math.acos(Math.pow(x, 2) - 1));
}




}
EN

Stack Overflow用户

发布于 2016-09-08 05:10:47

出现在您的循环中的16的阶乘大于MAX_INT

我得到了:

代码语言:javascript
复制
 >> factorial(16)
 2.0923e+013
 >> 2^31
 2.1475e+009

在八度音阶。您需要进行分析简化以使其保持在范围内,或者在那里使用double而不是int

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39378733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档