954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

There are two things which I didn't or could not achieve with using maths.cos() from Java.

First - When I calculate cos90 or cos270, it gives me absurb values. It should be 0. Meaning it cannot calculate if the angle lies on X axis.

I tested with 91 or 271, gives a near 0 which is correct.

Second - I was figuring how do I include the number of terms to be calculated with my Cosine formula....

Lastly, I am using JFrames as my GUI and I can only properly exit from my menu bar. And not from the X button at top RH corner.

I would appreciate hints or guidance in solving the above 3 issues. Thank you so much people! :)

LiBOC
Junior Poster in Training
73 posts since Jul 2005
Reputation Points: 10
Solved Threads: 1
 

Math.cos is using angles in radians. Try Math.cos(Math.toRadians(90)).

Second - I don't understand.

Try jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

dandan
Newbie Poster
12 posts since Jan 2006
Reputation Points: 10
Solved Threads: 1
 

If you properly set up the application you can exit the application normally (with JVM shutdown) by using the operating system supplied features (like the close icon in the window corner) as well.

Something like

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


in your main application frame should work wonders.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Hi eveyone,

Second - I was figuring how do I include the number of terms to be calculated with my Cosine formula

Use loops and arrays

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

Yea, I had convert it into radians before putting it back into the default math.cos() method... but only 270 and 90 are wrong values! :D

Math.cos is using angles in radians. Try Math.cos(Math.toRadians(90)).

Second - I don't understand.

Try jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

LiBOC
Junior Poster in Training
73 posts since Jul 2005
Reputation Points: 10
Solved Threads: 1
 

no, the rest is wrong as well (though it is possible that by some chance a few distinct values might happen to be identical it's unlikely).
But they're less obviously wrong unless you know what to expect. Do you know the exact cosine of every angle to 10 decimal places without looking them up?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Thanks.

I used the calculator program on my WinXP to counter check with my Java App. It is exactly correct. Only for 90 and 270 degrees. I also had converted them to radians.

And also, math.cos() method do not include terms if I am not wrong?

What if I want to write my own cosine formula that include a user-defined number of terms? Rewrite the formula into a java language? Solve the maths equation yourself?

can math.cos() do that?

no, the rest is wrong as well (though it is possible that by some chance a few distinct values might happen to be identical it's unlikely). But they're less obviously wrong unless you know what to expect. Do you know the exact cosine of every angle to 10 decimal places without looking them up?
LiBOC
Junior Poster in Training
73 posts since Jul 2005
Reputation Points: 10
Solved Threads: 1
 

What are the values you are getting for 90 and 270? Shouldn't they both be 0 according to the unit circle?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
What if I want to write my own cosine formula that include a user-defined number of terms? Rewrite the formula into a java language? Solve the maths equation yourself?

What do u want to do? I still have no clue? What do u mean by number terms? You aren't making much sense. Please give us an example. R u trying to solve generic equasions like:1/2cos x =751/2cos(3x) -5cos = 45

:sad:

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
What are the values you are getting for 90 and 270? Shouldn't they both be 0 according to the unit circle?

yes, and when using radials they won't be 0 but roughly -.45 and +.98 respectively.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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.

dandan
Newbie Poster
12 posts since Jan 2006
Reputation Points: 10
Solved Threads: 1
 

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:

double rad = 0.0;
            double interval = 0.1;
            
            while (rad < 2*Math.PI) {
                System.out.print(rad);
                System.out.print(" -> ");
                System.out.println(Math.cos(rad));
                rad += interval;
            }


Note that normally you'd format the output to make it look nicer...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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!

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.

LiBOC
Junior Poster in Training
73 posts since Jul 2005
Reputation Points: 10
Solved Threads: 1
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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/ma1002/appl/node59.html

And that's wat u meant by adding all the terms. Silly me? ;)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

yes.. with series...

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

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

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/ma1002/appl/node59.html

And that's wat u meant by adding all the terms. Silly me? ;)

LiBOC
Junior Poster in Training
73 posts since Jul 2005
Reputation Points: 10
Solved Threads: 1
 

This is the maths formula I am trying to get students to calculate using Java. Thank you. :)

[IMG]http://img.photobucket.com/albums/v670/davisli/DSC00066.jpg[/IMG]

LiBOC
Junior Poster in Training
73 posts since Jul 2005
Reputation Points: 10
Solved Threads: 1
 

U no you can use [.tex] tags to write formulae. Which is why cscgal made incorporated them into daniweb!

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

;)

But yes, that would be simple enuff to do in java.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You