Reputation: 3
I want to find three angle of a Triangle using Given three sides but it gives me 'nan' value.
I have tried law of cosines to find the angles but it ain't working. It gives 'nan' value.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
using namespace std;
double findDegree(double setDegree)
{
return setDegree * M_PI / 180;
}
double findRadian(double setRadian)
{
return acos(setRadian);
}
int main()
{
cout << endl;
cout << "Give three sides of the triangle: ";
int a, b, c;
cin >> a >> b >> c;
double angleA, angleB, angleC, getRadian, getDegree;
angleA = ((b * b) + (c * c) - (a * a)) / 2 * b * c;
angleB = ((a * a) + (c * c) - (b * b)) / 2 * a * c;
angleC = ((a * a) + (b * b) - (c * c)) / 2 * a * b;
// A
getRadian = findRadian(angleA);
getDegree = findDegree(getRadian);
angleA = getDegree;
// B
getRadian = findRadian(angleB);
getDegree = findDegree(getRadian);
angleB = getDegree;
// A
getRadian = findRadian(angleC);
getDegree = findDegree(getRadian);
angleC = getDegree;
cout << "Angle A is: " << angleA << endl;
cout << "Angle B is: " << angleB << endl;
cout << "Angle C is: " << angleC << endl;
}
Upvotes: 0
Views: 990
Reputation: 11
here is second main mistake:
double findDegree(double setDegree)
{
return setDegree * M_PI / 180;
}
Change to:
double findDegree(double setDegree)
{
return setDegree * 180 / M_PI;
}
Upvotes: 1
Reputation: 20262
Radian to degrees is "rad * 180 / pi", you're doing it the other way around in findDegree
. Why are you doing it anyway?
You also need to put the denominator in parenthesis, for example:
angleA = ((b * b) + (c * c) - (a * a)) / (2 * b * c);
a
, b
, c
should be double
probably, as well.
Upvotes: 1