-2
$\begingroup$

I was trying to calculate cosine series : cos x = 1 - x^2/2! + x^4/4! - x^8/8! .... , where x is in radians: include iostream

include math.h

using namespace std;

long factorial (long num)

{

if (num >1)

return num*factorial(num-1);

else return 1;

}

int main()

{int X,sum=0;

cout<<"Enter value of x in radians : ";

cin>>X;

for (int i = 0; i<=4; ++i)

{

    int z = pow(-1,i);

    int p = (pow(X,2*i)*z)/factorial(2*i);

    sum += p;


}

cout<

return 0;

} on putting x as 1.57( 90 degrees), I am getting 1, instead of 0. Can anyone explain why?

$\endgroup$
  • 1
    $\begingroup$ Take a look at your question. Would you be able to read your code? If not, how do you expect us to? $\endgroup$ – Yuval Filmus Oct 16 '15 at 9:55
  • $\begingroup$ @YuvalFilmus That's not very constructive. I'm pretty sure the asker would have formatted it legibly if they knew how to do that. However, since the question is off-topic (I know you know that, Yuval; I'm pointing it out for the asker's sake), the formatting doesn't make a lot of difference. $\endgroup$ – David Richerby Oct 16 '15 at 10:17
  • 1
    $\begingroup$ @DavidRicherby I disagree. There are quite a few questions sporting this kind of illegible code, which for me shows a lack of respect for our community. The OP should try a bit harder. $\endgroup$ – Yuval Filmus Oct 16 '15 at 10:26
  • 1
    $\begingroup$ @YuvalFilmus The policy is vote to close and comment in a constructive way. Please try to adhere to that. Even if you perceive a lack of respect (I'd recommend Hanlon's razor instead), it's a bad idea to respond in kind. $\endgroup$ – Raphael Oct 16 '15 at 10:30
  • 3
    $\begingroup$ This question is offtopic here, as are all programming questions. If this is actually an algorithms question, please get rid of the source code, use pseudo code instead and explain your ideas, what you've tried to isolate the issue, and what questions remain. Regarding formatting, note the little question mark above the text box; it sends you so a detailed introduction to Markdown. $\endgroup$ – Raphael Oct 16 '15 at 10:31
1
$\begingroup$

You are using integer arithmetic rather than floating point arithmetic. Make sure you understand the difference.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.