亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個正弦源代碼有什么問題?

這個正弦源代碼有什么問題?

慕碼人8056858 2024-01-05 11:01:01
我非常熟悉 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 - x3/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;       }
查看完整描述

1 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

您的程序中發生了很多事情以及一些不必要的代碼。不過,你走在正確的軌道上。我做了一些更改以簡化計算。你應該能夠跟隨他們。

具體來說。

  1. 交替標志。從 開始sign = 1,然后設置sign = -sign后續術語。

  2. 對于分母和階乘,我只使用了 for 循環,從 1 開始,遞增 2 得到 1,3,5,7

  3. 對于相同值的冪,我只需乘以d一個dSquared常數即可達到相同的效果。

  4. 我重寫了階乘以使其更簡單。

  5. 為了減少較大的值,d我只是使用remainder運算符使它們小于 360。

  6. 我添加了一些打印語句來顯示計算進度并確保一切正常工作。

  7. 最后,適合 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 - x3/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;

    }

}


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號