-
peng111说:
使用java语言,给定x和n的值,求sin(x),公式如下
siin x=x-x立方/3!+x的五次方/5!一直到+(-1)的n-1次方*x的2n-1次方/(2n-1)!解释:
public class Sine {
public static double calcSine(double x, int n) {
double result = x;
int sign = -1;
for (int i = 2; i <= n; i++) {
sign *= -1;
result += sign * Math.pow(x, 2 * i - 1) / factorial(2 * i - 1);
}
return result;
}public static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }
}