# wrong code

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Sep 30, 2012.

1. ### TAKYMOUNIR Thread Starter Active Member

Jun 23, 2008
351
1
this code should compute sin(x+y) but give wrong answer
can you help me to fix this code
thanks

• ###### cod.txt
File size:
349 bytes
Views:
26
Last edited: Sep 30, 2012
2. ### MrChips Moderator

Oct 2, 2009
16,875
5,184
1) Are you entering angles in degrees or radians?

2) Why are you computing x/pi and y/pi?

3) Why not just add x and y?

TAKYMOUNIR likes this.
3. ### TAKYMOUNIR Thread Starter Active Member

Jun 23, 2008
351
1
how can i enter angles in degree

4. ### MrChips Moderator

Oct 2, 2009
16,875
5,184
By typing in a number?

TAKYMOUNIR likes this.
5. ### TAKYMOUNIR Thread Starter Active Member

Jun 23, 2008
351
1
can you please run this code and see why give wrong answer

6. ### kubeek Expert

Sep 20, 2005
5,265
985
TAKYMOUNIR likes this.
7. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,227
shortbus and TAKYMOUNIR like this.

Jun 23, 2008
351
1
9. ### panic mode Senior Member

Oct 10, 2011
1,622
449
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

const double pi=3.1415926535897932384626433832795;
double a,b,s;

double SIN_of_sum(double x,double y);

int main(int argc, char** argv)
{
do {
printf("enter first angle in degrees: ");
scanf("%lg",&a);
printf("enter second angle in degrees: ");
scanf("%lg",&b);
s=SIN_of_sum(a,b);
printf(" SIN(%lg deg)=%g\n\n",a+b,s);
}
while ((a!=0) && (b!=0));

return 0;
}
double SIN_of_sum(double x,double y)
{
return sin((x+y)*pi/180);
}

10. ### WBahn Moderator

Mar 31, 2012
22,989
6,883
Why define pi to twice as many sig figs as a double can represent?

11. ### kubeek Expert

Sep 20, 2005
5,265
985
Or more specifically, why not use the constant M_PI which is already in math.h?

12. ### panic mode Senior Member

Oct 10, 2011
1,622
449

because i was too lazy and just pasted the value from calculator. also since I am working with variety of languages, I know that constant was defined somewhere in the math library but I didn't use it in a while (at least not in C) so I didn't bother to look it up. he just asked for something that works or compiles and that is what i did. happy...?

Last edited: Oct 2, 2012
13. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,227
Not all compilers do have constants like pi defined in the math.h. This is not a part of the ANSI C standard.

14. ### WBahn Moderator

Mar 31, 2012
22,989
6,883
Lazyness is a reasonable excuse in situations like this -- and your rationalle is just fine.

i don't use the one in math.h because I don't believe it is required to be there by the standard. Also, even if I was going to, I can never remember the name.