hi guys,
please I have created just very basic program that decides if it is triangle or not, If yes it counts S. But somehow when I write sides 1.1 2.2 3.3 // It does not show"not triangel"For other values it works but not for these . Can you tell me why? Thanks.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout <<"Write sides of triangle a, b, c:"<<endl;
double a, b, c;
double s,S;
cin >>a>>b>>c;
if((a+b)<=c || (b+c)<=a || (c+a)<=b || cin.fail() || a <= 0 || b <= 0 || c <= 0 )
cout <<"Not triangel."<< endl;
else
{
s = (a + b + c) / 2;
S = sqrt(s * (s - a) * (s - b) * (s - c));
cout <<"S triangle is "<<S<<endl;
}
return 0;
}
The triangle inequality is based on the sum of two sides being greater than the third side, not greater than or equal, which is a straight line ( or a degenerate triangle with zero area if you prefer).
The triangle inequality is base on the sum of two sides being greater than the third side
But that's what the OP's code does. If the sum of any two sides is <= the third side, then he says it's not a triangle.
The problem is that lengths 1.1, 2.2 and 3.3 exactly form a line but these base 10 numbers can't be exactly represented in the base 2 floating point format used by your computer.
BTW, for this reason, most scientific calculators use base 10, even though it's slower to do the calculations.