我试图计算用户以弧度输入的角度的正弦函数。我还需要在给定的容差范围内打印该值。我花了很多时间,而且进展很少。任何帮助将不胜感激!谢谢!我的主要问题是返回给定容差范围内的值。
//容差是一个epsilon值 恩。 0.00001
public static void sineCalc(double angle, double tolerance) {
int power = 1;
double currentAnswer = 0.0;
int count = 0;
double answer = 0.0;
double difference = 0.0;
int i = 1;
while (difference > tolerance || difference == 0) {
if (i % 2 == 0) {
currentAnswer = -Math.pow(angle, power) / getfactorial(power);
} else {
currentAnswer = Math.pow(angle, power) / getfactorial(power);
}
answer = answer + currentAnswer;
power = power + 2;
difference = Math.sin(angle) - answer;
difference = Math.abs(difference);
count++;
i++;
}
System.out.println(answer);
}