这个正弦源代码有什么问题?
What is wrong with this source code for sine?
我非常擅长使用 Java 语法,所以我决定根据我之前创建的算法创建一个正弦代码来使用它。我知道 Math.sin 可以帮助您评估正弦,但我只是为了好玩,决定继续创建我自己的源代码。
然而,60° 和 120° 之间以及 240° 和 300° 之间的角度会给出错误的答案,我不知道为什么。有人可以帮我找到错误吗?我已经尝试了所有方法来检测它,但都失败了。
import java.util.Scanner;
public class Sine {
public static void main(String[] args) {
// This code solves sine according yo the general expansion of sine
// sin x = x - x³/3! +x^5/5! - x^7/7! +...
Scanner scanner = new Scanner(System.in);
double answer = scanner.nextDouble();
scanner.close();
answer = simplify(answer);
answer = converttoradian(answer);
answer = continued(answer);
System.out.println(answer);
}
// This Makes all the angles that are more than 360
// To become less than 360 and Generates the simplified
// Angles for obtuse and reflex angles
static double simplify(double x) {
if (x >= 360) {
x = x - 360;
return simplify(x);
}
else if (x <= -360) {
x = x + 360;
return simplify(x);
}
else if (x > 90 && x <= 270) {
x = 180 - x;
return x;
}
else if (x >= 270) {
x = x - 360;
return x;
}
else if (x <= -90 && x > -270) {
x = -x - 180;
return x;
}
else if (x <= -270) {
x = x + 360;
return x;
}
else {
return x;
}
}
// Simple enough and explains itself
// Converts the angles to radian
static double converttoradian(double d) {
d *= Math.PI;
d /= 180.0;
return d;
}
// This Method about to open generates each term and adds them together
// The number of terms solved in this case is 33
static double continued(double d) {
double answer = 0.0;
int index = 1;
double one = d;
for (int i = 0; i < 33; i++) {
double result = 0.0;
for (int x = 1; x < index; x++) {
d = d * one;
}
long here = factorial(index);
result = d / here;
if ((index - 1) % 4 == 0) {
answer = answer + result;
index = index + 2;
}
else {
answer = answer - result;
index = index + 2;
}
}
return answer;
}
// Evaluates factorials
static long factorial(int n) {
long one = 1;
long m = (long) n;
if (m == 0 || m == 1) {
one = 1;
return one;
}
else {
while (m > 1) {
one *= m;
m--;
}
return one;
}
}
}
您的程序中发生了很多事情和一些不必要的代码。不过,您走在正确的轨道上。我做了一些更改以简化计算。你应该可以关注他们。
具体来说。
- 交替符号。从
sign = 1
开始,然后为后续项设置 sign = -sign
。
- 对于分母和阶乘,我只是用了for循环,从1开始递增2得到1,3,5,7
- 对于相同值的幂,我只是将
d
乘以 dSquared
常数来达到相同的效果。
- 我重写了阶乘以使其更简单。
- 为了减少
d
的大值,我只是使用 remainder
运算符使它们小于 360。
- 我添加了一些打印语句来显示计算进度并确保一切正常。
- 最后,适合 long 的最大阶乘是
20!
。之后,由于溢出,它们变为负值。所以需要减少术语的数量。
public class Sine {
public static void main(String[] args) {
// This code solves sine according yo the general expansion of sine
// sin x = x - x³/3! +x^5/5! - x^7/7! +...
for (double degrees = 0; degrees < 700; degrees += 17) {
double simplified_degrees = simplify(degrees);
System.out.println("simplified_degrees = " + simplified_degrees);
double radians = converttoradian(simplified_degrees);
System.out.println("radians = " + radians);
double sin = continued(radians);
System.out.println(sin);
System.out.println(Math.sin(radians));
System.out.println("------------------------------------------");
}
}
// This Makes all the angles that are more than 360
// To become less than 360 and Generates the simplified
// Angles for obtuse and reflex angles
static double simplify(double x) {
x = x % 360;
return x;
}
// Simple enough and explains itself
// Converts the angles to radian
static double converttoradian(double d) {
return Math.PI / 180. * d;
}
// This Method about to open generates each term and adds them together
// The number of terms solved in this case is 33
static double continued(double d) {
double result = 0;
double sign = 1;
double dSquared = d * d;
int pow = 1;
for (int pow = 1; pow < 21; pow += 2) {
long fact = factorial(pow);
System.out.println("d = " + d + ", fact = " + fact + ", pow = " + pow
+ ", sign = " + sign);
result = result + (d / fact) * sign;
d *= dSquared; // effective powers 3, 5, 7,9
sign = -sign; // alternate sign for every other term
}
return result;
}
// Evaluates factorials
static long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
long fact = 1;
for (long i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
}
我非常擅长使用 Java 语法,所以我决定根据我之前创建的算法创建一个正弦代码来使用它。我知道 Math.sin 可以帮助您评估正弦,但我只是为了好玩,决定继续创建我自己的源代码。 然而,60° 和 120° 之间以及 240° 和 300° 之间的角度会给出错误的答案,我不知道为什么。有人可以帮我找到错误吗?我已经尝试了所有方法来检测它,但都失败了。
import java.util.Scanner;
public class Sine {
public static void main(String[] args) {
// This code solves sine according yo the general expansion of sine
// sin x = x - x³/3! +x^5/5! - x^7/7! +...
Scanner scanner = new Scanner(System.in);
double answer = scanner.nextDouble();
scanner.close();
answer = simplify(answer);
answer = converttoradian(answer);
answer = continued(answer);
System.out.println(answer);
}
// This Makes all the angles that are more than 360
// To become less than 360 and Generates the simplified
// Angles for obtuse and reflex angles
static double simplify(double x) {
if (x >= 360) {
x = x - 360;
return simplify(x);
}
else if (x <= -360) {
x = x + 360;
return simplify(x);
}
else if (x > 90 && x <= 270) {
x = 180 - x;
return x;
}
else if (x >= 270) {
x = x - 360;
return x;
}
else if (x <= -90 && x > -270) {
x = -x - 180;
return x;
}
else if (x <= -270) {
x = x + 360;
return x;
}
else {
return x;
}
}
// Simple enough and explains itself
// Converts the angles to radian
static double converttoradian(double d) {
d *= Math.PI;
d /= 180.0;
return d;
}
// This Method about to open generates each term and adds them together
// The number of terms solved in this case is 33
static double continued(double d) {
double answer = 0.0;
int index = 1;
double one = d;
for (int i = 0; i < 33; i++) {
double result = 0.0;
for (int x = 1; x < index; x++) {
d = d * one;
}
long here = factorial(index);
result = d / here;
if ((index - 1) % 4 == 0) {
answer = answer + result;
index = index + 2;
}
else {
answer = answer - result;
index = index + 2;
}
}
return answer;
}
// Evaluates factorials
static long factorial(int n) {
long one = 1;
long m = (long) n;
if (m == 0 || m == 1) {
one = 1;
return one;
}
else {
while (m > 1) {
one *= m;
m--;
}
return one;
}
}
}
您的程序中发生了很多事情和一些不必要的代码。不过,您走在正确的轨道上。我做了一些更改以简化计算。你应该可以关注他们。
具体来说。
- 交替符号。从
sign = 1
开始,然后为后续项设置sign = -sign
。 - 对于分母和阶乘,我只是用了for循环,从1开始递增2得到1,3,5,7
- 对于相同值的幂,我只是将
d
乘以dSquared
常数来达到相同的效果。 - 我重写了阶乘以使其更简单。
- 为了减少
d
的大值,我只是使用remainder
运算符使它们小于 360。 - 我添加了一些打印语句来显示计算进度并确保一切正常。
- 最后,适合 long 的最大阶乘是
20!
。之后,由于溢出,它们变为负值。所以需要减少术语的数量。
public class Sine {
public static void main(String[] args) {
// This code solves sine according yo the general expansion of sine
// sin x = x - x³/3! +x^5/5! - x^7/7! +...
for (double degrees = 0; degrees < 700; degrees += 17) {
double simplified_degrees = simplify(degrees);
System.out.println("simplified_degrees = " + simplified_degrees);
double radians = converttoradian(simplified_degrees);
System.out.println("radians = " + radians);
double sin = continued(radians);
System.out.println(sin);
System.out.println(Math.sin(radians));
System.out.println("------------------------------------------");
}
}
// This Makes all the angles that are more than 360
// To become less than 360 and Generates the simplified
// Angles for obtuse and reflex angles
static double simplify(double x) {
x = x % 360;
return x;
}
// Simple enough and explains itself
// Converts the angles to radian
static double converttoradian(double d) {
return Math.PI / 180. * d;
}
// This Method about to open generates each term and adds them together
// The number of terms solved in this case is 33
static double continued(double d) {
double result = 0;
double sign = 1;
double dSquared = d * d;
int pow = 1;
for (int pow = 1; pow < 21; pow += 2) {
long fact = factorial(pow);
System.out.println("d = " + d + ", fact = " + fact + ", pow = " + pow
+ ", sign = " + sign);
result = result + (d / fact) * sign;
d *= dSquared; // effective powers 3, 5, 7,9
sign = -sign; // alternate sign for every other term
}
return result;
}
// Evaluates factorials
static long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
long fact = 1;
for (long i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
}