So I'm taking an online Java course and the task is as follows:
In this program we are going to practice using the Math class by computing some important values on the unit circle. Starting at 0 and going up by PI/4 radians (or 45 degrees), print out information of the format below.
Radians: (cos, sin)
0.0: 1.0, 0.0
0.79: 0.7, 0.71
1.57: 0.0, 1.0
2.36: -0.71, 0.7
3.14: -1.0, 0.0
3.93: -0.7, -0.71
4.71: 0.0, -1.0
5.5: 0.71, -0.71
Hint: You’ll need to use the Math.sin, Math.cos methods and the Math.PI constant! You’ll also need to loop from 0 to 2*PI
Here is what I came up with:
public class UnitCircle extends ConsoleProgram
{
public void run()
{
System.out.println("Radians: (cos, sin)");
for(double count = 0; count <= Math.PI * 2; count += Math.PI / 4)
{
System.out.println(count + ": " + Math.cos(count) + ", " + Math.sin(count));
}
}
}
However when I run it, this is what I get:
Radians: (cos, sin)
0.0: 1.0, 0.0
0.7853981633974483: 0.7071067811865476, 0.7071067811865475
1.5707963267948966: 6.123233995736766E-17, 1.0
2.356194490192345: -0.7071067811865475, 0.7071067811865476
3.141592653589793: -1.0, 1.2246467991473532E-16
3.9269908169872414: -0.7071067811865477, -0.7071067811865475
4.71238898038469: -1.8369701987210297E-16, -1.0
5.497787143782138: 0.7071067811865474, -0.7071067811865477
6.283185307179586: 1.0, -2.4492935982947064E-16
What's up with that? Could it be that I need to limit the calculations to round to 2 decimal places? What else am I doing wrong? Any help would be greatly appreciated.