# sine cosine problem

• February 10th, 2012, 06:18 AM
dewet
sine cosine problem
Hope this is the right place for the post.
I'm programming a program to help my at my work.
this 1 part I just can't get right.
Here is the code. I used the 'degrees * Math.PI / 180' in place 'Math.toRadians (degrees)'
that doesn't make a difference.

Code :

```public class Polar { public static void main (String [] args) { double degrees45 = 45.0; double radians45 = Math.toRadians (degrees45);   System.out.println ("The sine of " + degrees45 + "degrees is " + (Math.sin (radians45))); System.out.println ("The cosine of " + degrees45 + "degrees is " + (Math.cos (radians45)));   double degrees90 = 90.0; double radians90 = Math.toRadians (degrees90);   System.out.println ("\nThe sine of " + degrees90 + "degrees is " + (Math.sin (radians90))); System.out.println ("The cosine of " + degrees90 + "degrees is " + (Math.cos (radians90)));   double degrees135 = 135.0; double radians135 = Math.toRadians (degrees135);   System.out.println ("\nThe sine of " + degrees135 + "degrees is " + (Math.sin (radians135))); System.out.println ("The cosine of " + degrees135 + "degrees is " + (Math.cos (radians135)));   double degrees180 = 180.0; double radians180 = Math.toRadians (degrees180);   System.out.println ("\nThe sine of " + degrees180 + "degrees is " + (Math.sin (radians180))); System.out.println ("The cosine of " + degrees180 + "degrees is " + (Math.cos (radians180)));   double degrees270 = 270.0; double radians270 = Math.toRadians (degrees270);   System.out.println ("\nThe sine of " + degrees270 + "degrees is " + (Math.sin (radians270))); System.out.println ("The cosine of " + degrees270 + "degrees is " + (Math.cos (radians270))); } }```

The display
The sine of 45.0degrees is 0.7071067811865475
The cosine of 45.0degrees is 0.7071067811865476

The sine of 90.0degrees is 1.0
The cosine of 90.0degrees is 6.123031769111886E-17

The sine of 135.0degrees is 0.7071067811865476
The cosine of 135.0degrees is -0.7071067811865475

The sine of 180.0degrees is 1.2246063538223773E-16
The cosine of 180.0degrees is -1.0

The sine of 270.0degrees is -1.0
The cosine of 270.0degrees is -1.836909530733566E-16

If you look at the cosine of 90 degrees, why does it give me 6.123031769111886E-17 and not 0.
Same as sine of 180 and cosine of 270.
The sine and cosine of 45 and 135 works fine.

Is it something I'm missing or what.
• February 10th, 2012, 06:52 AM
snowguy13
Re: sine cosine problem
It's nothing you are missing; rather, it's the limits of how much data we can store. In radians, two pi is equal to 360 degrees, so the sine of 180 degrees is equal to the sine of pi, and so on. However, thus far, no end to pi's decimals have been found. Pi has billions of decimal places, and obviously our computers can't hold that much information.

So, while the exact conversion of 180 degrees to radians is pi, the static constant that the Math class uses for pi is actually 3.14159265358979323846, which is far from exact. This would be the culprit for you non-zero answers.

Also, note that 6.123...E-17 is actually 0.00000000000000006123.... I'd just count that as zero. ;)
• February 10th, 2012, 07:05 AM
dewet
Re: sine cosine problem
O ok, so what should I do so that java use 0 and not 6.123...E-17, round it of?
• February 10th, 2012, 07:14 AM
dewet
Re: sine cosine problem
I just tried the rounding off it works. Thx a lot.
You helped me so much. Really thx a lot.
• February 10th, 2012, 06:09 PM
snowguy13
Re: sine cosine problem
Sorry I couldn't reply sooner (school is just always in the way ;D )! You're very welcome; I'm glad you got it to work! :)