请帮忙看一下自己编的Java小程序错在哪里?可编译运行,但得出来的结果好像是错的!! 20
//用sin(x)≈x-x^3/3!+x^5/5!-……+(-1)^(n-1)*(x^(2n-1))/(2n-1)!的公式求近似值。设x=7,n=15publicclas...
//用sin(x) ≈ x-x^3/3!+x^5/5!-……+(-1)^(n-1)*(x^(2n-1))/(2n-1)!的公式求近似值。设x=7,n=15
public class Ts01{
public static void main(String args[]){
double sum;
sum = getsum(7,15);
System.out.println(sum);
}
//总和
public static double getsum(int x,int n){
double sum = 0.0;
for(int i=2;i<=n;i++){
sum = sum + getnum(x,n)/getjiecheng(n);
}
return x-sum;
}
//求分子部分
public static double getnum(int x,int n){
double sum1 = 0.0,sum2 = 0.0;
sum1 = Math.pow(-1,n-1);
sum2 = Math.pow(x,2*n-1);
return sum1*sum2;
}
//求阶乘/分母部分
public static double getjiecheng(int n){
double sum = 1.0;
for(int i = 2;i <= 2*n-1;i++){
sum = sum * i;
}
return sum;
}
} 展开
public class Ts01{
public static void main(String args[]){
double sum;
sum = getsum(7,15);
System.out.println(sum);
}
//总和
public static double getsum(int x,int n){
double sum = 0.0;
for(int i=2;i<=n;i++){
sum = sum + getnum(x,n)/getjiecheng(n);
}
return x-sum;
}
//求分子部分
public static double getnum(int x,int n){
double sum1 = 0.0,sum2 = 0.0;
sum1 = Math.pow(-1,n-1);
sum2 = Math.pow(x,2*n-1);
return sum1*sum2;
}
//求阶乘/分母部分
public static double getjiecheng(int n){
double sum = 1.0;
for(int i = 2;i <= 2*n-1;i++){
sum = sum * i;
}
return sum;
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询