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: cosine in java

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post cosine in java

    how do i implement the cosine trig function in java?


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: cosine in java

    Do you need to make an implementation, or do you just need a cosine function?

    If you just need cosine for somthing else, then check the Math class. It contains a cos method.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: cosine in java

    Unless you intend to draw the graph using Graphics2D, the math class contains the sine and cosine functions. They do however only work with radians, so you'll need a converted variable if you intend to have degrees as input.

    Example of the cosine:
     double x = Math.cos(Math.PI/6);
    edit: changed from int to double

    API:
    Math (Java 2 Platform SE v1.4.2)

    Hope that helps.
    Last edited by olemagro; January 22nd, 2010 at 09:37 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: cosine in java

    casting to integers for cosine/sine is a bad idea. You'll almost always get zero because of truncation.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    olemagro (January 22nd, 2010)

  6. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: cosine in java

    Ah, didn't know that. Noted to self!

    Edit: Come to think of it, I knew it but didn't consider the fact that int doesn't allow decimals which is crucial in most sine/cosine functions.
    Last edited by olemagro; January 22nd, 2010 at 05:12 PM.