【问题标题】:How do I square Sin and Cos in Java? [duplicate]如何在 Java 中平方正弦和余弦? [复制]
【发布时间】:2016-06-25 04:18:30
【问题描述】:

我需要这个 java 练习的帮助。说明是

编写一个程序,使用 Math.sin() 和 Math.cos() 来检查 对于任何输入的 θ,sin2θ + cos2θ 的值大约为 1 命令行参数。只需打印值。为什么价值观不 总是正好 1?

到目前为止我的代码(这是我的第一个代码,所以请不要苛刻的判断)。

 public class math {
     public static void main(String[] args) {

         //Changes the data types from string to double
         double a = Double.parseDouble(args[0]);
         double b = Double.parseDouble(args[1]);

         //Does the calculation math functions
         double s = 2*Math.sin(a);
         double c = 2*Math.cos(b);
         double target = .9998; // var for comparing if less than 1
         double answer = s + c;

         // Prints the answer and checks whether its less than 1
         System.out.println(answer < target);
         System.out.println(answer);
     }   
 }

我猜想将 sin 和 cos 平方。如果有人对如何平方 sin 和 cos 有快速建议,如果我的小程序有错误,我将感谢您的帮助。

【问题讨论】:

  • 您将 sin 和 cos 乘以 2。如果你想求平方,你应该使用 Math.pow。更简单的方法是将 sin/cos 与自身相乘。
  • 此外,您应该使用 Math.toRadians() 方法将输入转换为弧度,因为数学三角函数使用弧度值作为参数。

标签: java math


【解决方案1】:

我想你要做的就是这个。你得到了“sin^2θ”的错误含义。它的实际含义是 sin2θ。对于cos也是一样。这是您要查找的代码。

double a = Double.parseDouble(args[0]);

//Does the calculation math functions
double s = Math.pow(Math.sin(a),2);
double c = Math.pow(Math.cos(b),2);
double target = .9998; // var for comparing if less than 1
double answer = s + c;

// Prints the answer and checks whether its less than 1
System.out.println(answer < target);
System.out.println(answer);

【讨论】:

    【解决方案2】:

    你有一个 theta:你只需要 args[0] 而不是 args[1]。
    所以a=b 在您的示例代码中。

    将 sin 和 cos 平方并添加:

    选项1:

    double stheta = Math.sin(theta);
    double ctheta = Math.cos(theta);
    double answer = stheta*stheta + ctheta*ctheta;
    

    选项2:

    double s2 = Math.pow(Math.sin(theta),2);
    double c2 = Math.pow(Math.cos(theta),2);
    double answer = s2 + c2;  
    

    工作示例代码:

    package javaapplication1;
    
    public class JavaApplication1 {
    
        public static void main(String[] args) {
            double theta = Double.parseDouble(args[0]);
            double stheta = Math.sin(theta);
            double ctheta = Math.cos(theta);
            double answer = stheta*stheta + ctheta*ctheta;
            System.out.println(answer);
        }   
    }
    

    看看:
    Java - does double and float percision depend on the machine?
    Test Number For Maximum Precision In Java

    【讨论】:

      【解决方案3】:

      浮点数不是精确的表示。双精度 64 位 IEEE 浮点表示仅给出 17-18 位,因此您始终将差异与容差进行比较。

      我建议使用 Math.pow() 将一个数字乘以自己的平方。

      这是一个 JUnit 测试,展示了我将如何做到这一点:

      package math;
      
      import org.junit.Test;
      import org.junit.Assert;
      
      /**
       * Created by Michael
       * Creation date 6/25/2016.
       * @link https://stackoverflow.com/questions/38024899/how-do-i-square-sin-and-cos-in-java
       */
      public class MathTest {
      
          public static final double TOLERANCE = 1.0e-8;
      
          @Test
          public void testUnitCircleIdentity() {
              int n = 10;
              double t = 0.0;
              for (int i = 0; i < n; ++i) {
                  double s = Math.sin(t);
                  double c = Math.cos(t);
                  double actual = s*s + c*c;
                  Assert.assertEquals(1.0, actual, TOLERANCE);
                  t += Math.PI/n;
              }
          }
      }
      

      【讨论】:

      • 支持解决 OP 测试(近似)相等性方法中的严重错误的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多