acos not working

Jun 21, 2016 at 10:52am
i was making program calculate basic things about shapes but when i used acos()
for calculating angles between sides it didnt work it only gave the value of angle it just ignored acos()
Jun 21, 2016 at 11:02am
closed account (48T7M4Gy)
<math.h> double acos(double x) returns the arc cosine of x in radians

http://www.cplusplus.com/reference/cmath/acos/
Jun 21, 2016 at 2:59pm
Yeah i also kept that in mind and than also i didnt get the right output
1
2
3
4
5
6
 a1=((s3*s3)+(s2*s2)-(s1*s1))/(2*s3*s2);
                    a2=((s1*s1)+(s3*s3)-(s2*s2))/(2*s1*s3);
                    a3=((s1*s1)+(s2*s2)-(s3*s3))/(2*s1*s2);
                    cout<<"\nAngle Between Sides "<<s3<<" and "<<s2<<" is : "<<(acos(a1)*180)/M_PI
                        <<"\nAngle Between Sides "<<s1<<" and "<<s3<<" is : "<<(acos(a2)*180)/M_PI
                        <<"\nAngle Between Sides "<<s1<<" and "<<s2<<" is : "<<(acos(a3)*180)/M_PI;

This is the concerned region of my code please help me find the mistake which i am not able to figure out.
I am only getting values of a1,a2,a3 as my output when i run the code.
Jun 21, 2016 at 3:24pm
closed account (48T7M4Gy)
What type is s1, s2 etc. If they are integers you are a victim of integer division and all the angles will be 90 degrees. Change them to doubles or change 2 to 2.0 in each denominator.
Jun 22, 2016 at 6:38am
they all are of float type will this effect them. BTW a1,a2 and a3 are calculated correctly but the part in cout is just ignored by the compiler and gives the output of just a1,a2,a3 instead of calculating the full value with acos and the converting it to degrees it just ignores it.
Jun 22, 2016 at 7:01am
closed account (48T7M4Gy)
Show us your code with data enough so we can run it here.
Jun 22, 2016 at 7:06am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include<math.h>

int main()
{
    
    double s1, s2, s3, a1,a2,a3;
    
    s1 = 1;
    s2 = sqrt(3);
    s3 = 2;
    
  a1=((s3*s3)+(s2*s2)-(s1*s1))/(2*s3*s2);
                    a2=((s1*s1)+(s3*s3)-(s2*s2))/(2*s1*s3);
                    a3=((s1*s1)+(s2*s2)-(s3*s3))/(2*s1*s2);
                    std::cout<<"\nAngle Between Sides "<<s3<<" and "<<s2<<" is : "<<(acos(a1)*180)/M_PI
                        <<"\nAngle Between Sides "<<s1<<" and "<<s3<<" is : "<<(acos(a2)*180)/M_PI
                        <<"\nAngle Between Sides "<<s1<<" and "<<s2<<" is : "<<(acos(a3)*180)/M_PI;
}



Angle Between Sides 2 and 1.73205 is : 30
Angle Between Sides 1 and 2 is : 60
Angle Between Sides 1 and 1.73205 is : 90 


No problem here. :)
Last edited on Jun 22, 2016 at 7:08am
Jun 22, 2016 at 7:10am
i figured out the mistake the code here is correct but the cout statement given in the beginning has wrong numbering and the switch statement also followed the same that was the mistake. Sorry!!! for wasting your precious time SORRY
And BTW Thanks For Your Help
Last edited on Jun 22, 2016 at 7:14am
Jun 22, 2016 at 7:15am
closed account (48T7M4Gy)
Cheers, I'm glad you found it. :)
Topic archived. No new replies allowed.