diff --git a/calculateEx.java b/calculateEx.java new file mode 100644 index 0000000..cd308ea --- /dev/null +++ b/calculateEx.java @@ -0,0 +1,24 @@ + +/** + * e的x次方的泰勒展开式为 + * e^x=1+x/1!+x^2/2!+x^3/3!+…+x^n/n!  +**/ + public class CalculateEx { + + public static double calcalate(int x){ + int n=1; //n用来算阶乘 + long f=1; //总阶乘 + double sum=1.0,temp=1.0; //sum为最终值,temp算分子的次方 + + do{ + f= f*n ;//阶乘 + n++ ; + temp *= x; //计算次方 + sum += temp / f; + } while(1.0/f>1e-6); + + //System.out.println("e^"+x+"="+sum); + return sum; + } + +}