how do i implement the cosine trig function in java?
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.
how do i implement the cosine trig function 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.
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:
edit: changed from int to doubledouble x = Math.cos(Math.PI/6);
API:
Math (Java 2 Platform SE v1.4.2)
Hope that helps.
Last edited by olemagro; January 22nd, 2010 at 08:37.
casting to integers for cosine/sine is a bad idea. You'll almost always get zero because of truncation.
olemagro (January 22nd, 2010)
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 16:12.