Logical Error

I'm writing a program where the user inputs the coordinates for 3 vertices of a triangle. Then I determine if it is a right triangle or not. Here is a snippet of my logic, where I made my error.

This is the input that I've been having trouble with: 100,100 550,550 100,550

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
     if(distanceAB > distanceAC && distanceAB > distanceBC)
    {
        //distanceab = hypotenuse
        if(ceil(pow(distanceBC,2)) + ceil(pow(distanceAC,2)) == ceil(pow(distanceBC,2)))
        {
            cout << "Triangle is right!\n";
        }
        else
        {
            cout << "not right\n";
        }
    }
            else if(distanceAC > distanceBC && distanceAC > distanceAB)
            {
                //distanceac = hypotenuse
                if(ceil(pow(distanceBC,2)) + ceil(pow(distanceAB,2)) == ceil(pow(distanceAC,2)))
                {
                    cout << "Triangle is right!\n";
                }
                else
                {
                    cout << "not right\n";
                }
            }
                else if(distanceBC > distanceAC && distanceBC > distanceAB)
                {
                    //distancebc = hypotenuse
                    if(ceil(pow(distanceAC,2)) + ceil(pow(distanceAB,2)) == ceil(pow(distanceBC,2)))
                    {
                        cout << "Triangle is right!\n";
                    }
                    else
                    {
                        cout << "not right\n";
                    }

                }


So, obviously I'm at loss. I'm pretty new to c++, and I'm open to suggestions. Please tell me what's wrong with this.
Edit: Line 4 should be if(ceil(pow(distanceBC,2)) + ceil(pow(distanceAC,2)) == ceil(pow(distanceBC,2))
Hi,

http://en.cppreference.com/w/cpp/numeric/math/ceil

The std::ceil function returns a floating point type, so that makes it unsuitable for use with equality operator. Rounding with ceil is probably not what you want, answers need to be more precise than whole numbers.

You might be better with your own function that tests "equality" within a specified precision. See if the absolute value of the difference between the 2 numbers is less than the precision.

The STL has a std::hypot function:
http://en.cppreference.com/w/cpp/numeric/math/hypot
Ok. How exactly do I use hypot?
1
2
3
4
5
6
    double distanceAB = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));//Finds side lengths
    double distanceAC = sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
    double distanceBC = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
    cout << "Side AB is " << distanceAB << " Long.\n";
    cout << "Side AC is " << distanceAC << " Long.\n";
    cout << "Side BC is " << distanceBC << " Long.\n";

This is my formula for side lengths from coordinate points.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Finds hypotenuse
    if(distanceAB > distanceAC && distanceAB > distanceBC)
    {
        //distanceab = hypotenuse
        if(fabs(pow(distanceBC,2)) + fabs(pow(distanceAC,2)) == fabs(pow(distanceBC,2)))
        {
            cout << "Triangle is right!\n";
        }
        else
        {
            cout << "not right\n";
        }
    }
            else if(distanceAC > distanceBC && distanceAC > distanceAB)
            {
                //distanceac = hypotenuse
                if(fabs(pow(distanceBC,2)) + fabs(pow(distanceAB,2)) == fabs(pow(distanceAC,2)))
                {
                    cout << "Triangle is right!\n";
                }
                else
                {
                    cout << "not right\n";
                }
            }
                else if(distanceBC > distanceAC && distanceBC > distanceAB)
                {
                    //distancebc = hypotenuse
                    if(fabs(pow(distanceAC,2)) + fabs(pow(distanceAB,2)) == fabs(pow(distanceBC,2)))
                    {
                        cout << "Triangle is right!\n";
                    }
                    else
                    {
                        cout << "not right\n";
                    }

                }

This is the logic code to determine which side is a hypotenuse and if it is right.

Sorry if i'm not making sense, this problem is kind of frustrating and I'm sure it's something dumb (:
One can't use equality operators with any type of floating point (FP) value (float, double, long double). They are aren't exact, look at this example:

1
2
3
4
double a = 0.1; // really say 0.09999999999997
double b = 10.0 * a; // 0.9999999999997

if (b == 1.0) //false 


The reason FP numbers are not exact, is that they are basically stored as binary fractions. Given a finite number of significant figures, it's easy to see what they might not be exact.

That is why I suggested writing your own function to determine equality.

TheIdeasMan wrote:
You might be better with your own function that tests "equality" within a specified precision. See if the absolute value of the difference between the 2 numbers is less than the precision.


You can use std::abs rather than the C function fabs

Ok. How exactly do I use hypot?


Did you look at the example in the link I provided? Note this part:

cppreference wrote:
Distance between two points (x1,y1,z1) and (x2,y2,z2)on 3D space can be calculated as std::hypot(x2-x1, y2-y1, z2-z1)


Also note that this might be considered as cheating if it is an assignment.

One can't use equality operators with any type of floating point (FP) value (float, double, long double). They are aren't exact, look at this example:

1
2
3
4
double a = 0.1; // really say 0.09999999999997
double b = 10.0 * a; // 0.9999999999997

if (b == 1.0) //false  


The reason FP numbers are not exact, is that they are basically stored as binary fractions. Given a finite number of significant figures, it's easy to see what they might not be exact.

That is why I suggested writing your own function to determine equality.


Elegant explanation. Thanks for this



You can use std::abs rather than the C function fabs

Ok. How exactly do I use hypot?


Did you look at the example in the link I provided? Note this part:

cppreference wrote:
Distance between two points (x1,y1,z1) and (x2,y2,z2)on 3D space can be calculated as std::hypot(x2-x1, y2-y1, z2-z1)


Also note that this might be considered as cheating if it is an assignment.


Oh gosh. This helped a lot..

Thanks for writing this all out for me, you helped a ton :)
No worries, pleased to help

Regards :+)
Topic archived. No new replies allowed.