-
peng111说:
使用Python语言,给定x和n的值,求sin(x),公式如下
siin x=x-x立方/3!+x的五次方/5!一直到+(-1)的n-1次方*x的2n-1次方/(2n-1)!解释:n为阶乘
import math
x = int(input('输入x值:'))
n = int(input('输入n值:'))result = x
for i in range(1, n):
result += ((-1)(i+1))*(x(2i+1))/math.factorial(2i+1)print("sin(x) = %.2f" %result)
-
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; }
}
1181/11523