泰勒展開式輸入超過16就會超出cos的范圍,純新手,是資料溢位了還是什么問題呢
public static void main(String []args) {
if(args.length==0) {
System.out.println("ERROR");
return;
}
else {
double n = 0.0 ;
double max = 20.0 ;
double a = -1.0 ;
double sum = 0.0 ;
double cos = 0.0 ;
double z = Double.parseDouble(args[0]);
for (n=0.0;n<=max;n++) {
double b = 1.0 ;
double x = 1.0 ;
if (n==0.0) {
a = 1.0 ;
x = 1.0 ;
b = 1.0 ;
}
else {
if (n%2==0) {
a = 1.0 ;
}
else {
a = -1.0 ;
}
for (double t = 1.0;t<=2*n;t++) {
b = b*t ;
x = x*z ;
}
}
cos = (a/b)*x ;
sum = sum + cos ;
double sum1 = (long)(sum*100)/100.0 ;
if (n+1>max) {
System.out.println(sum1);
}
}
}
}
}
要求撰寫一個泰勒展開式計算cos值(保留兩位),但是出現輸入不超過16的時候資料準確,超過16后開始不準確的情況
20
cos(x)=∑ ((-1)^n/(2n)!)*x^2n
n=0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/230780.html
標籤:Java相關
