# C++ question

Discussion in 'OT Technology' started by Rick, Oct 3, 2003.

1. ### RickNew Member

Joined:
Feb 20, 2002
Messages:
1,735
0
Location:
Henderson, NV
What am I doing wrong here? I commented some stuff out to test set values. The values entered are a right triangle but for some reason my if statements might be mixed up some and it's saying it's not a right triangle. It's probably something simple, but I must be overlooking it.

Code:
```#include <iostream>
#include <cmath>
using namespace std;

int main()
{

// double x, y, z;
double hypot, side1, side2;

double x = 1;
double y = 1;
double z = sqrt(2.0);

//cout<<"Enter a value for the first side:"<<endl;
//cin>>x;
//cout<<"Enter a value for the second side:"<<endl;
//cin>>y;
//cout<<"Enter a value for the third side:"<<endl;
//cin>>z;

if(x < 0.0 || y < 0.0 || z < 0.0)
cout<<"ERROR: The sides of the triangle must be greater than 0.0";
else
if(x > y && y > z)
{
hypot = x;
side1 = y;
side2 = z;
}
else if(y > x && y > z)
{
hypot = y;
side1 = x;
side2 = z;
}
else
{
hypot = z;
side1 = x;
side2 = y;
}

if(pow(hypot,2) == pow(side1,2) + pow(side2,2))
cout<<"This IS a right triangle."<<endl;
else
cout<<"This IS NOT a right triangle."<<endl;

cout<<pow(hypot,2)<<endl;
cout<<pow(side1,2) + pow(side2,2)<<endl;
return 0;
}
```

2. ### Penguin ManProtect Your Digital Liberties

Joined:
Apr 27, 2002
Messages:
21,696
0
Location:
Edmonton, AB
Works for me testing with 3,4,5 and 6,8,10. I don't see a problem in the code, could be a rounding problem with the sqrt() statement.

3. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
0
Location:
Aztlán
Most likely rounding error.

#include <iomanip>

and place this line before each of the couts at the end of the program:

cout << setiosflags(ios_base::fixed) << setw(20) << setprecision(20);

This should show you if they are truly equal. Floating point sucks.

4. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
0
Location:
Aztlán
comparing the equality of floating point numbers using '==' is usually a bad idea because of rounding error. Look up Knuth's algorithm for floating point comparison. It basically checks to see if the numbers are 'close enough' to be considered equal.

5. ### crotchfruitGuest

your algorithm for finding the hyp is wrong.

for example, what if my (x,y,z) is (5,3,4)? in this case, you want hypot=x, but this won't happen because your "y > z" is false. you should have put "if(x > y && x > z)" - i.e. is x the greatest number?

here is substitute comparison for the equality:

Code:
```if(pow(hypot,2) == pow(side1,2) + pow(side2,2))
```
try

Code:
```if ( fabs( pow(hypot,2) - (pow(side1,2) + pow(side2,2)) ) < 0.0001 )
...
```
you can replace the 0.0001 with the accuracy that you desire. remember that floating point is crap (7 sigdigs i think..?).. you should use doubles instead.

6. ### RickNew Member

Joined:
Feb 20, 2002
Messages:
1,735
0
Location:
Henderson, NV
That's what it was.

2.00000000000000044
2.00000000000000000

These were the outputs.

Joined:
Feb 20, 2002
Messages:
1,735