C++ Cosine? radian?

May 13, 2009 at 2:04am
Please, I don't get this I did some base functions, but my math equation gives wrong answer. what equation (in code) needs to get the right answer! please help here.
Ex. if you input a number 23. it calculates and ---> 4.15 radians.
cos(23) = -0.533
cos(4.15) = -0.533
And if you put a number -12 it calculates and ---> 5.717 radians.
cos(-12) = 0.844
cos(5.717) = 0.844

(0 - 2π in radians, equivalent to 0 to 360 degrees).
The program will read in an angle θ (in radians) from the keyboard. First, it will call a function to translate the angle to be one in the first period


Normally think of the cosine of an angle as a ratio of the lengths of two sides of a right triangle. If θ is the angle in the picture, then the ratio of B to C (opposite/hypoteneuse) is the sine of θ, the ratio of B to A (opposite/adjacent) is the tangent of θ, and the ratio of A to C (adjacent/hypoteneuse) is the cosine of θ.
This cosine ratio is equivalent to the sum of the terms of the infinite series
cos (θ) = 1 - (θ2 / 2!) + (θ4 / 4!) - (θ6 / 6!) + . . .
By way of example, cos(2) would be equivalent to
( (-1)0 * (2)2*0/(2*0)! ) + ( (-1)1 * (2)2*1/(2*1)! ) + ( (-1)2 * (2)2*2/(2*2)! ) + ...
or
1 + -2 + 2/3 + - 4/45 + ...
May 13, 2009 at 2:13am
I don't get you: I just checked on my computer's calculator and the calculations you put there are perfectly correct.

Second, you do not need to translate your angle in the interval [0,2pi). That is precisely the point: if you evaluate the power series you correctly typed:

cos (θ) = 1 - (θ^2 / 2!) + (θ^4 / 4!) - (θ^6 / 6!) + . . .

it will evaluate to the same number if you put θ or θ+2pi.

So, what exactly are you asking? It seems to me that you perfectly understand what the cosine function is!

Note: to evaluate these power series, just do a for cycle, calculate them up to, say, the 100^th term, and there you go!
Last edited on May 13, 2009 at 2:14am
May 13, 2009 at 2:29am
I meant those are given examples. Those numbers up there are right ones that I must get. If I input a number 23 using my equation it gives wrong answer.
23 ---> 8.2 radians, which is wrong answer
cos(23) = some weird number.
cos(8.2) = totally wrong number shows up.
So my question is can you show me how to get the right answer using C++ code?
Last edited on May 13, 2009 at 2:31am
May 13, 2009 at 2:48am

So my question is can you show me how to get the right answer using C++ code?


Umm....

1
2
double result = cos(23.0);
cout << result;


outputs "-0.532833"

What's the problem here?

EDIT -- or wait... do you mean you have to write your own cos function?
Last edited on May 13, 2009 at 2:49am
May 13, 2009 at 3:13am
do you mean you have to write your own cos function?
yes. and how to get the number 4.15 radians ...input number 23?
23 radian = 4.15???
The program only requires to put one input variable which is number 23.
and It displays
Your input number is 23
it is also 4.15 radians.
cos(23) = -0.533
cos(4.15) = -0.533
Last edited on May 13, 2009 at 3:20am
May 13, 2009 at 4:54am
Remember that cosine is a cyclic function. Your input must be adjusted to fit the domain of the formula.
May 13, 2009 at 5:33am
Try this:
23 rad is same as 4.15 rad in circular notation. [there is only 360° or 2pi rad in a circle]
23 - 6.283185..(2*pi rad)=16.71681..rad - 6.283185..=10.433629..rad- 6.283185..=4.15 rad
Hence from the point of view of the cosine function 23 rad is same as 4.15 rad because the cosine upper bound is 2pi or 6.283185. So cos(23 rad)=-0.532833 = cos (4.15 rad)= -0.532833
comprenez vous?
May 13, 2009 at 6:08am
so how to program the code to get 4.15 rad?
May 13, 2009 at 12:58pm
Remember that cosine is a cyclic function. Your input must be adjusted to fit the domain of the formula.


Mate, precisely because it's a cyclic function you don't care where your input lies. Whether you plug in pi/2 or 5/2pi in that huge infinite series, you will get 0 all the same. In fact, pi is defined as exatly twice the smallest number that makes those infinite power series vanish.

The functions with whose domain you must be careful are the arccos and arcsin (the inverse of the cos and sin).
Last edited on May 13, 2009 at 1:01pm
May 13, 2009 at 1:17pm
your definition of the cosine function is almost correct...

More correctly is to imagine a circle of radius 1 with the origin in 0, and then you rotate r radians around it and you put a dot on the circle. Now cos r is the length from 0 to the projection on the ox axis of that dot and sin r is 0-projection on oy axis.

now if you rotate 2pi on that circle you get in the exact same place as you started so r+n*2pi has the same projection as r

You could get the smaller angle by using the modulo function like: r%(2*pi)

And by the way arccos (acos) only returns values in the range 0- pi so if you use acos(cos(6.28+1)) it will return 1
May 13, 2009 at 2:16pm
Sorry, I was half asleep and I didn't look at the formula he was using...

/me sheepish
Topic archived. No new replies allowed.