-5

I need help with this java exercise. The instructions are

Write a program that uses Math.sin() and Math.cos() to check that the value of sin2θ + cos2θ is approximately 1 for any θ entered as a command-line argument. Just print the value. Why are the values not always exactly 1?

My code so far (It is my first code so please no harsh judgement).

 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);
     }   
 }

I guessed on squaring the sin and cos. If anyone has quick suggestion on how to square sin and cos and if there is an error in my little program I will appreciate your help.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
TheCaptain
  • 135
  • 2
  • 8
  • 4
    You are multiplying sin and cos by two. You should use Math.pow if you want to square it. An easier way is you multiply sin/cos with itself. – Jeremy Jun 25 '16 at 04:29
  • Additionally, you should convert the input to Radians using Math.toRadians() method, because Math trigonometric functions work with Radians values as parameters. – chespinoza Apr 16 '17 at 23:25

3 Answers3

1

I think what you are going to do is this. You got this wrong meaning of "sin^2θ". It's actual meaning is sin2θ. And same for cos. This is the code you are looking for.

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);
Lasitha Yapa
  • 4,309
  • 8
  • 38
  • 57
1

you have one theta so: you need just args[0] and not args[1].
so a=b in your sample code.

squaring the sin and cos and adding:

Option 1:

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

Option 2:

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

working sample code:

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);
    }   
}

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

Community
  • 1
  • 1
1

Floating point numbers are not exact representations. Double precision 64 bit IEEE floating point representation only gives 17-18 digits, so you always compare differences to a tolerance.

I would recommend multiplying a number by itself for squaring over using Math.pow().

Here's a JUnit test that shows how I'd do it:

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;
        }
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561