Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: sine cosine problem

1. ## 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.

```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.
Please help me.  Reply With Quote

3. ## 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.   Reply With Quote

4. ## The Following User Says Thank You to snowguy13 For This Useful Post:

dewet (February 10th, 2012)

5. ## 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?  Reply With Quote

6. ## Re: sine cosine problem

I just tried the rounding off it works. Thx a lot.
You helped me so much. Really thx a lot.  Reply With Quote

7. ## 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!   Reply With Quote