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.

Results 1 to 5 of 5

Thread: sine cosine problem

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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.
    Last edited by snowguy13; February 10th, 2012 at 07:09 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

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

    dewet (February 10th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: sine cosine problem

    I just tried the rounding off it works. Thx a lot.
    You helped me so much. Really thx a lot.
    Last edited by dewet; February 10th, 2012 at 08:29 AM.

  6. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. [SOLVED] Sine function, can't get factorial to work
    By Actinistia in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 27th, 2012, 12:24 PM
  2. [SOLVED] Quick logic question (Sine function)
    By Actinistia in forum Java Theory & Questions
    Replies: 6
    Last Post: March 24th, 2011, 02:08 PM
  3. Opposites of Sine Cosine and Tangent
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 28th, 2011, 01:59 PM
  4. Trouble with law of Cosine program
    By Delstateprogramer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 07:07 PM
  5. cosine in java
    By etidd in forum Java Theory & Questions
    Replies: 4
    Last Post: January 22nd, 2010, 09:33 AM