Search code examples
javataylor-series

Java sigma notation


I'm working on a project for school. The project is to solve x for the formula ex, which can be defined as:

e^x formula sigma notation

This is my code (it's important to note the value for "x" is 5 for debugging purposes):

private static double sigmaNotation(double x) {
    double ans = 0;
    double calc = 0;

    for (int n = 0; n <= 15; n++) {
        ans = calc + ans;
        calc = Math.pow(x, n)/factorial(n);
        System.out.println(n+": ans: "+ans+",          calc: "+calc);
    }
    return ans;
}
private static int factorial(double x) {
    int factorial = 1;
    for(double i = x; i > 1; i--) {
        factorial *= i;
    }
    return factorial;
}

and output is:

0: ans: 0.0,          calc: 1.0
1: ans: 1.0,          calc: 5.0
2: ans: 6.0,          calc: 12.5
3: ans: 18.5,          calc: 20.833333333333332
4: ans: 39.33333333333333,          calc: 26.041666666666668
5: ans: 65.375,          calc: 26.041666666666668
6: ans: 91.41666666666667,          calc: 21.70138888888889
7: ans: 113.11805555555556,          calc: 15.500992063492063
8: ans: 128.61904761904762,          calc: 9.68812003968254
9: ans: 138.30716765873015,          calc: 5.3822889109347445
10: ans: 143.68945656966488,          calc: 2.6911444554673722
11: ans: 146.38060102513225,          calc: 1.2232474797578965
12: ans: 147.60384850489015,          calc: 0.5096864498991235
13: ans: 148.1135349547893,          calc: 0.568434188872778
14: ans: 148.68196914366206,          calc: 2.8421709443638896
15: ans: 151.52414008802594,          calc: 14.210854721819448

but it should look like this:

11: ans: ...,          calc: 1.2232474797578965
12: ans: ...,          calc: 0.5096864498991235
13: ans: ...,          calc: 0.19603324996120136
14: ans: ...,          calc: 0.07001187498614334
15: ans: ...,          calc: 0.02333729166204778

My code produces perfectly correct numbers from iteration 0 - 12. What is happening at the 13th iteration that's causing it to suddenly produce a strange number?


Solution

  • At iteration 13 you had an overflow.

    13! = 6227020800 > 2^31 which is the largest valid int value in Java. To work around this, you can use long or even BigInteger and BigDecimal.

    Also, observe that the computation x^n/n! is equivalent to (x^(n-1)/(n-1)!) * (x/n). This gives an iterative approach that wont overflow at all.