泰勒级数展开求解e的x次方

泰勒级数展开求解e的x次方

根据公式

ex=limn+1+x+x22!++xnx!e^x = \lim\limits_{n\to+\infty} 1 + x + \frac{x^2}{2!} + ··· +\frac{x^n}{x!}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main {
public static double CalE(int x) {
double ans = 1, s = x;
for (int i = 2; i < 100; i++) {
ans += s;
s = s * x * 1.0 / i;
}
return ans;
}

public static void main(String[] args) {
System.out.printf("%.8f", CalE(1)); //CalE(x)即为e的x次方
}
}

加入判断条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.math.*;

public class Main {
public static double CalE(int x) {
double ans = 1, s = x;
for (int i = 2; s > 1e-8; i++) {
ans += s;
s = s * x * 1.0 / i;
}
return ans;
}

public static void main(String[] args) {
System.out.printf("%.8f", CalE(1));
}
}