//This program determines if a triangle is formed given three sides.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
double sideA, sideB, sideC;
cout<<"Enter the lengths of the sides -> ";
cin>>sideA>>sideB>>sideC;
if (sideA + sideB <= sideC || sideA + sideC <= sideB || sideB + sideC <= sideA)
{
cout<<"*** "<<fixed<<setprecision(3)<<sideA<<", "<<sideB<<" and "
<<sideC<<" do not satisfy the triangle inequality. ***"<< endl;
}
else
cout<<"It's a triangle!";
return 0;
}
@rentantude
doubles are only stored to a finite precision. Unlike ints. Comparing doubles for equality - as in both your cases - is fraught with risk. Both your cases are right on the margin of being a triangle (very degenerate in this case; the sides would effectively form a straight line).
@rentandude,
I know of no good "work-around", because that is the nature of finite-precision, floating-point arithmetic: you must be aware of its limitations.
Actually, it is a matter of definition whether you regard 1.1, 2.2, 3.3 as a (rather degenerate) triangle or not - it has three sides and the internal angles add up to 180 degrees, the sine and cosine rules both hold, and the area formula is correct, albeit giving area 0.
Best simply not to worry about the "boundary cases".