这个正弦源代码有什么问题?

我非常熟悉 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;

       }

慕码人8056858
浏览 92回答 1
1回答

qq_花开花谢_0

您的程序中发生了很多事情以及一些不必要的代码。不过,你走在正确的轨道上。我做了一些更改以简化计算。你应该能够跟随他们。具体来说。交替标志。从 开始sign = 1,然后设置sign = -sign后续术语。对于分母和阶乘,我只使用了 for 循环,从 1 开始,递增 2 得到 1,3,5,7对于相同值的幂,我只需乘以d一个dSquared常数即可达到相同的效果。我重写了阶乘以使其更简单。为了减少较大的值,d我只是使用remainder运算符使它们小于 360。我添加了一些打印语句来显示计算进度并确保一切正常工作。最后,适合 long 的最大阶乘是20!。之后,它们会因溢出而变为负数。因此需要减少项数。public class Sine {&nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; // This code solves sine according yo the general expansion of sine&nbsp; &nbsp; &nbsp; // sin x = x - x³/3! +x^5/5! - x^7/7! +...&nbsp; &nbsp; &nbsp; for (double degrees = 0; degrees < 700; degrees += 17) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double simplified_degrees = simplify(degrees);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("simplified_degrees = " + simplified_degrees);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double radians = converttoradian(simplified_degrees);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("radians = " + radians);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double sin = continued(radians);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(sin);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(Math.sin(radians));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("------------------------------------------");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;// This Makes all the angles that are more than 360&nbsp; &nbsp;// To become less than 360 and Generates the simplified&nbsp; &nbsp;// Angles for obtuse and reflex angles&nbsp; &nbsp;static double simplify(double x) {&nbsp; &nbsp; &nbsp; x = x % 360;&nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp;}&nbsp; &nbsp;// Simple enough and explains itself&nbsp; &nbsp;// Converts the angles to radian&nbsp; &nbsp;static double converttoradian(double d) {&nbsp; &nbsp; &nbsp; return Math.PI / 180. * d;&nbsp; &nbsp;}&nbsp; &nbsp;// This Method about to open generates each term and adds them together&nbsp; &nbsp;// The number of terms solved in this case is 33&nbsp; &nbsp;static double continued(double d) {&nbsp; &nbsp; &nbsp; double result = 0;&nbsp; &nbsp; &nbsp; double sign = 1;&nbsp; &nbsp; &nbsp; double dSquared = d * d;&nbsp; &nbsp; &nbsp; int pow = 1;&nbsp; &nbsp; &nbsp; for (int pow = 1;&nbsp; pow < 21; pow += 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;long fact = factorial(pow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("d = " + d + ", fact = " + fact + ", pow = " + pow&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ", sign = " + sign);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = result + (d / fact) * sign;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d *= dSquared; // effective powers 3, 5, 7,9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sign = -sign; // alternate sign for every other term&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp;}&nbsp; &nbsp;// Evaluates factorials&nbsp; &nbsp;static long factorial(int n) {&nbsp; &nbsp; &nbsp; if (n == 0 || n == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; long fact = 1;&nbsp; &nbsp; &nbsp; for (long i = 2; i <= n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fact *= i;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return fact;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java