Reputation: 15
I am supposed to get the cosine and sin of PI and PI/2 and angle 0. All my numbers have been correct except for the cosine of PI/2 .
Expected Output:
Radians: (cos, sin)
0.0: 1.0, 0.0
1.5707963267948966: 0.0, 1.0
3.141592653589793: -1.0, 0.0
My Output:
Radians: (cos, sin)
0.0: 1.0, 0.0
1.5707963267948966: 1.0, 1.0
3.141592653589793: -1.0, 0.0
public class UnitCircle
{
public static void main(String[] args)
{
System.out.println("Radians: (cos, sin)");
double angle = 0.0;
double piDivideTwo = Math.PI/2.0;
double pi = Math.PI;
System.out.println(angle + ": " + Math.cos(angle) + ", " + Math.sin(angle) );
double cosine = Math.cos(piDivideTwo);
cosine = Math.round(cosine * 100) / 100.0;
System.out.println(piDivideTwo + ": " + Math.cos(cosine) + ", " + Math.sin(piDivideTwo) );
double sin = Math.sin(pi);
sin = Math.round(sin *100) / 100.0;
System.out.println(pi + ": " + Math.cos(pi) + ", " + Math.sin(sin) );
}
}
Upvotes: 0
Views: 230
Reputation: 54168
The reason why it seems to work using twice cos
or sin
it's just because it's on the value 0
You just need to compute once cos,sin
and then print with format because PI/2 and PI can't be the perfect value
static void cosSin(double angle) {
double cos = Math.cos(angle);
double sin = Math.sin(angle);
System.out.printf("%.4f : %.1f %.1f \n", angle, cos, sin);
}
public static void main(String[] args) {
System.out.println("Radians: (cos, sin)");
double angle = 0.0;
double piDivideTwo = Math.PI / 2.0;
double pi = Math.PI;
cosSin(angle); // 0,0000 : 1,0 0,0
cosSin(piDivideTwo); // 1,5708 : 0,0 1,0
cosSin(pi); // 3,1416 : -1,0 0,0
}
Upvotes: 1