Question

groeneveldd52 on Thu, 04 Feb 2016 07:22:53


So I'm writing a code to solve for the two angles and missing side of a right triangle. The code has no errors and solves for my side ("c") perfectly. However for the angles it prints out -nan(ind). I have been in this class for a total of maybe 5 class meetings so i don't know what is going on or how to fix it. So if anyone can plainly explain the issue as though i have no clue what any of the C++ terminology actually is. lol! thank you. Any help is great otherwise just don't. 

Here is the code:

#include <iostream>;
#include <cmath>;
using namespace std;
int main()
{
long double a, b, c, A, A1, B1, B, tot1, tot2, tot3;
char done, an;
cout << "input value of a \n";
cin >> a;
cout << "input value of b \n";
cin >> b;
tot1 = a*a + b*b;
c = sqrt(tot1);
cout << "c=" << c <<"\n";
tot2 = (b*b + c*c - a*a) / 2 * b*c;
A1 = acos(tot2);
A = A1 * 180 / 3.14;
cout << A << "\n";
cin >> done;
tot3 = (a*a + c*c - b*b) / 2 * a*c;
B1 = acos(tot3);
B = B1 * 180 / 3.14;
cout << B << "\n";
cin >> done;
cout << "C is equal to 90 degrees \n ";
system("pause");
return 0;

};


Sponsored



Replies

Barry-Schwarz on Thu, 04 Feb 2016 08:32:13


tot2 = (b*b + c*c - a*a) / 2 * b*c;
A1 = acos(tot2);

Your formula for tot2 is incorrect.  You are missing a set of parentheses around the divisor.

Hart Cheng on Thu, 04 Feb 2016 09:04:27


Hi,

I had test the code,  we can reproduce the issue. In order to solve issue. you should do it :

tot2 = ((b*b + c*c - a*a) /( 2 * b*c));

Best Regards,

Hart

Pavel A on Thu, 04 Feb 2016 16:07:15


This "NAN" thing means "Not A Number". Unlike integer numbers, where every combination of bits is some valid value - floating numbers have more complex structure (pardon the pun) and some combination of bits do not represent a valid floating point value. The c++ library knows to detect such things.  

 The "ind" after "nan" means some specific flavor of "non-numberness".  So you either are trying to print an uninitialized variable, or a result of invalid operation.

-- pa