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! :)

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

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.

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

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

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?

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?

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 =75

1/2cos(3x) -5cos = 45

:sad:

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.

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.

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

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.

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.

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? ;)

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

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? ;)

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.

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.

The good thing is you don't have to worry about order of operations. An equation like that would be much nicer in summation notation..... Also, the sequence looks arithmetic, so if you want to add all that crap up on the right side of the equation you can simply use the sum formula:

Sn = (n\2)(a1+a2)

If you don't know what I'm talking about, then think about when your teacher asks you to sum all the numbers from 1 to a 100...That's the shortcut.

Also, the sequence looks arithmetic, so if you want to add all that crap up on the right side of the equation you can simply use the sum formula:

Sn = (n\2)(a1+a2)

I don't really understand that. Can u explain it to me with an example. how does it work 4 the infinite series of cos(x)?

:o

So I can't use Math.cos() method anyway?

How do I put the formula into code? Must it be very complicating? Or other java methods and calculate summation notations.

When finding on net, I found a class for SummationParser

parser.add(new SummationParser());". :mrgreen:

The good thing is you don't have to worry about order of operations. An equation like that would be much nicer in summation notation..... Also, the sequence looks arithmetic, so if you want to add all that crap up on the right side of the equation you can simply use the sum formula:

Sn = (n\2)(a1+a2)

If you don't know what I'm talking about, then think about when your teacher asks you to sum all the numbers from 1 to a 100...That's the shortcut.

I don't really understand that. Can u explain it to me with an example. how does it work 4 the infinite series of cos(x)?

:o

It's only for the right side, but I'm not sure that it will even work right yet. For an infinite series, you can allways get the sum of like the first 100 or so values and that is pretty much the answer, because it won't change much even though it's infinite.

Oh ok, only for the right side.. haha.. thanks alot!!

You'll still need the cos stuff. The formulas I was only meaning for the right of the equation.

Oh ok, only for the right side.. haha.. thanks alot!!

Ok let's not get carried away here. That stuff quoted by server_crash, may or may not work? Until he proves it, we don't really no.

You don't need math.cos. After all the infinite series, gives u an approximation of cos(x) . That is what ur homework is!

Ur next job is to understand how to make an
accumulator, which is basically a summation thingie.

For starters, as an exercise I might ask you how to sum all the numbers from 1 to 100...

1+2+3+4+5+6...+100

Once u have understood how to do this, a similiar principle will be needed for the summation of the cosine series.

The only caveat I can see is with the factorial function. Since it grows rapidly, u may have to limit the number of terms u allow the user to enter. 100 is probably too much?

Oh and u probably have to write a factorial method. So once u have discovered how to write an accumulator get back.

:lol:

One other thing. Converting an angle from radians to degrees can be done, by dividing by (360/2pi) or more crudely, dividing by 57.3

Thanks! Anyway Java has a method of converting into radians.

I will get back IF I got it. haha. :lol:

Ok let's not get carried away here. That stuff quoted by server_crash, may or may not work? Until he proves it, we don't really no.

You don't need math.cos. After all the infinite series, gives u an approximation of cos(x) . That is what ur homework is!

Ur next job is to understand how to make an
accumulator, which is basically a summation thingie.

For starters, as an exercise I might ask you how to sum all the numbers from 1 to 100...

Once u have understood how to do this, a similiar principle will be needed for the summation of the cosine series.

The only caveat I can see is with the factorial function. Since it grows rapidly, u may have to limit the number of terms u allow the user to enter. 100 is probably too much?

Oh and u probably have to write a factorial method. So once u have discovered how to write an accumulator get back.

:lol:

One other thing. Converting an angle from radians to degrees can be done, by dividing by (360/2pi) or more crudely, dividing by 57.3

This article has been dead for over six months. Start a new discussion instead.