-7

I've been writing a code for a simple scientific calculator. Now, I've used math.h library but it gives the values of sin,cos,tan in radians while i want in degrees. I've tried using *180/PI but it isn't working. on the other hand, it's working (*180/PI) with the inverse values.

cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Sin = "<<sin(a)*180.0/PI <<endl;
break;
case 8:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Cos = "<<cos(a)*180.0/PI <<endl;
break;
case 9:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Tan = "<<tan(a)*180.0/PI <<endl;

I expect the output in degrees but it doesn't show that correctly. Meanwhile, here's the code for inverse in which it's working correctly.

case 10:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of Sin = "<<asin(a)*180.0/PI<<endl;
break;

case 11:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of Cos = "<<acos(a)*180.0/PI<<endl;
break;
case 12:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of tan = "<<atan(a)*180.0/PI<<endl;
break;
Zain Abedin
  • 149
  • 9
  • 5
    Sin doesn't give an angle as a result, it takes the angle as its argument. You have to convert the input to sin from degrees to radians, not convert the result. – Useless Feb 09 '19 at 11:32
  • How? could you elaborate please? – Zain Abedin Feb 09 '19 at 11:34
  • 3
    I'm voting to close this question as off-topic because you misunderstood the trig, not the C++ – Useless Feb 09 '19 at 11:34
  • Seriously, man? you aren't even telling me what's the problem and you say I dont understand the trig. okay, well, then how am I getting correct answers for the inverse values? – Zain Abedin Feb 09 '19 at 11:36
  • 2
    The `asin` code works because `asin` takes an input in the range `-1..1` and gives back an angle in radians. This is why the conversion worked. However for `sin` you multiplied the **result** in the range `-1..1` by `180/PI`, which does not make sense (you want to convert the input angle). – asu Feb 09 '19 at 11:36
  • 2
    @ZainAbedin The first comment told you exactly what the problem was. – melpomene Feb 09 '19 at 11:39
  • Yes, seriously. The sin function takes an angle as input. Angles can be measured in degrees or radians. It expects the angle in radians and you gave it degrees. The result of sin is not an angle and it makes no sense to convert it to/from degrees. – Useless Feb 09 '19 at 11:41
  • @melpomene I think Tom Atix's answer helped me. – Zain Abedin Feb 09 '19 at 11:42
  • 2
    @ZainAbedin How? Tom's answer literally doesn't explain anything. It's just code. – melpomene Feb 09 '19 at 11:46

2 Answers2

2

I expect the output in degrees

No you don't. You expect your input to be interpreted as degrees.

sin and cos don't return angles, so you can't speak about their return values as of degrees or radians.

Not only you need to apply the radians<->degrees conversion to the angle before applying sin or cos to it, you also need to convert degrees to radians, not the other way around. Thus you need *PI/180, not *180/PI.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
-1

Instead of

cout<<"Sin = "<<sin(a)*180.0/PI <<endl;

You want

cout<<"Sin = "<<sin(a*PI/180.0) <<endl;
Tom Atix
  • 381
  • 1
  • 21