The problem with using Java's maths.cos()

Reply

Join Date: Jan 2006
Posts: 12
Reputation: dandan is an unknown quantity at this point 
Solved Threads: 1
dandan dandan is offline Offline
Newbie Poster

Re: The problem with using Java's maths.cos()

 
0
  #11
Jan 25th, 2006
System.out.println(Math.cos(Math.toRadians(90)));
System.out.println(Math.cos(Math.toRadians(270)));

gives

6.123233995736766E-17
-1.8369701987210297E-16

good enough for most purposes.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: The problem with using Java's maths.cos()

 
0
  #12
Jan 25th, 2006
P.S. I've done a small test and Math.cos(x) yields correct results which are in line with the ones the Windows calculator produces down to their last decimal place (when the latter is set to radians of course).

The following will print the cosine of every angle between 0 and 2 pi:
  1. double rad = 0.0;
  2. double interval = 0.1;
  3.  
  4. while (rad < 2*Math.PI) {
  5. System.out.print(rad);
  6. System.out.print(" -> ");
  7. System.out.println(Math.cos(rad));
  8. rad += interval;
  9. }

Note that normally you'd format the output to make it look nicer...
42
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 73
Reputation: LiBOC is an unknown quantity at this point 
Solved Threads: 0
LiBOC's Avatar
LiBOC LiBOC is offline Offline
Junior Poster in Training

Re: The problem with using Java's maths.cos()

 
0
  #13
Jan 25th, 2006
Yes, I got that answer too. But I thought 90 and 270 should yield a 0?

That is the thing I am talking about. Why would Cos90 and Cos270 in math.cos() be not 0?

So sorry, for the timebeing, my sourcecode is not with me. I can't post it here.

However my objective is to:

1)Calculate the cosine angle of x
WITHOUT using the math.cos()

2)It allows students or users to type in their desired
Angle in Degrees and Number of Terms to calculate.
More number of terms means more accurate calculation.

3)And error handling which I can handle.

CONCLUSION : The ultimate questions is,
1) Why math.cos() for 90 and 270 is not 0.
2) How do I not use math.cos() to calculate cosine.

I will be posting the cosine maths formula soon. Thank you!


Hope I am not confusing anyone! a BIG thank you for your overwhelming responses!

Originally Posted by dandan
System.out.println(Math.cos(Math.toRadians(90)));
System.out.println(Math.cos(Math.toRadians(270)));

gives

6.123233995736766E-17
-1.8369701987210297E-16

good enough for most purposes.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: The problem with using Java's maths.cos()

 
0
  #14
Jan 26th, 2006
Because, as has been mentioned many times already, Math.cos (and Math.sin and others) are NOT going to convert degrees to radians for you.

Maybe you should learn about trigonometric calculations first before trying to use them in your software, maybe then things might become clear(er) to you.
42
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: The problem with using Java's maths.cos()

 
0
  #15
Jan 26th, 2006
So sorry, for the timebeing, my sourcecode is not with me. I can't post it here.

However my objective is to:

1)Calculate the cosine angle of x
WITHOUT using the math.cos()

2)It allows students or users to type in their desired
Angle in Degrees and Number of Terms to calculate.
More number of terms means more accurate calculation.
Oh i c wat u r trying to do now. Ur simply trying to evaluate the cos(x) using taylor's series?

http://www.maths.abdn.ac.uk/~igc/tch...pl/node59.html

And that's wat u meant by adding all the terms. Silly me?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: The problem with using Java's maths.cos()

 
0
  #16
Jan 26th, 2006
The best thing to do is draw a triangle if you're confused and the unit circle doesn't help. The only problem with using a triangle is you'll have to use some logic, because you'll start with 2 unknowns (from what I remember).
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 73
Reputation: LiBOC is an unknown quantity at this point 
Solved Threads: 0
LiBOC's Avatar
LiBOC LiBOC is offline Offline
Junior Poster in Training

Re: The problem with using Java's maths.cos()

 
0
  #17
Jan 26th, 2006
yes.. with series...

Regarding to converting radians.. i had already converted right?

if("Calculate".equals(actionCommand))
d= Math.cos(Math.toRadians(d));

Originally Posted by iamthwee
Oh i c wat u r trying to do now. Ur simply trying to evaluate the cos(x) using taylor's series?

http://www.maths.abdn.ac.uk/~igc/tch...pl/node59.html

And that's wat u meant by adding all the terms. Silly me?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 73
Reputation: LiBOC is an unknown quantity at this point 
Solved Threads: 0
LiBOC's Avatar
LiBOC LiBOC is offline Offline
Junior Poster in Training

Re: The problem with using Java's maths.cos()

 
0
  #18
Jan 26th, 2006
This is the maths formula I am trying to get students to calculate using Java. Thank you.

http://img.photobucket.com/albums/v6...i/DSC00066.jpg
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: The problem with using Java's maths.cos()

 
0
  #19
Jan 26th, 2006
U no you can use [.tex] tags to write formulae. Which is why cscgal made incorporated them into daniweb!

{\cos x = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!}+ \frac{x^8}{8!} - \cdots}



But yes, that would be simple enuff to do in java.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: The problem with using Java's maths.cos()

 
0
  #20
Jan 26th, 2006
simple enough indeed, easy mathematical sequence.
Program as a loop, several ways to determine whether to add or subtract.

Trickiest is how to determine when to terminate the loop, unless you're using a fixed number of iterations.
42
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 12358 | Replies: 27
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC