1

My question was:

Write a program that reads three non-zero double values and determines and prints whether they are sides of a right triangle. The program should verify the results up to 4 decimal places. [Hint: Use Pythagoras' theorem to determine whether the three sides form right triangle.]

The sample output is:

Enter length of three sides: 3 4 5
The sides represents right triangle.

Enter length of three sides: 4 5 6.403
The sides don’t represents right triangle.

Enter length of three sides: 4 5 6.4031
The sides represents right triangle.

I used this approach but can't understand how to verify up to 4 decimal places. Please help with this or at least give a hint.

My Code:



#include <iostream>

using namespace std;

int main() {
    double s1, s2, s3;

    cout<<"Enter length of three sides: ";
    cin>>s1>>s2>>s3;

    s1 *= s1;
    s2 *= s2;
    s3 *= s3;

    if ((s1 == s2 + s3) || (s2 == s1 + s3) || (s3 == s1 + s2)) {
        cout<<"The sides represents right triangle."<<endl;
        
    }
    else {
        cout<<"The sides don't represents right triangle."<<endl;
        
    }
    
}


Someone told me use for setprecision, but how?

  • This entire program is flawed due to `if ((s1 == s2 + s3) || (s2 == s1 + s3) || (s3 == s1 + s2))`. See [is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Comparing doubles using `==` is just one such flaw. – PaulMcKenzie Oct 12 '22 at 08:01
  • my program won't have a float right? can you give me a solution? – Huzaifa Khan Oct 12 '22 at 08:11
  • `double s1, s2, s3;` -- These are floating point types. As stated, your code is flawed, way before you consider how to display a potentially flawed result. Did you read the link about floating point types not being exact? Even `0.1` is not exact. Whoever gave you this assignment must have been well aware of this issue. – PaulMcKenzie Oct 12 '22 at 08:11
  • got it now. what about the comparison now? how to do it? – Huzaifa Khan Oct 12 '22 at 08:14
  • 1
    There are plenty of links as to how to compare double values for "equality", like [this one](https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison). So basically it is `if (s1 is close to s2 + s3, then they are equal)`. It is the `is close to` that you need to write code for. Or the other alternative is to use another type that does do exact math, but that would need a third-party library (or you write your own code). – PaulMcKenzie Oct 12 '22 at 08:14
  • 1
    ohkay let me check – Huzaifa Khan Oct 12 '22 at 08:29

1 Answers1

0

It is all about rounding and also about the precision of double numbers. Comparison for equality is basically not possible.

As you have read in the comments and as for example shown here, there are ways to somehow compare doubles.

And here your 4th decimal place will help.

Comparison can basically done with the following formula:

bool equal(double a, double b) {
    return fabs(a - b) < EPSILON;
}

And here we can set the epsilon to 0.001, which gives you the desired result.

Do not forget to find the hypotenuse! This is the longest side.

Please see the following example:

#include <iostream>
#include <iomanip>
#include <limits>
#include <array>
#include <algorithm>

int main() {
    // Tell user what to do
    std::cout << "\nEnter length of three sides: ";

    // Get length of the 3 sides of the triangle
    std::array<double, 3> side{};
    if ((std::cin >> side[0] >> side[1] >> side[2]) and (side[0] > 0) and (side[1] > 0) and (side[2] > 0)) {

        // Get Hypotenuse
        std::sort(side.begin(), side.end());

        // You may enable debug output by setting to 1
#if 0
        // Debug output
        std::cout << std::setprecision(30) <<"\n\nDebug output\na:                   " << side[0] << '\n';
        std::cout << "b:                    " << side[1] << '\n';
        std::cout << "c:                    " << side[2] << '\n';
        std::cout << "a*a:                  " << side[0] * side[0] << '\n';
        std::cout << "b*b:                  " << side[1] * side[1] << '\n';
        std::cout << "c*c:                  " << side[2] * side[2] << '\n';
        std::cout << "a*a + b*b:            " << side[0] * side[0] + side[1] * side[1] << '\n';
        std::cout << "abs((a*a+b*b)-(c*c)): " << std::fabs(((side[0] * side[0]) + (side[1] * side[1])) - (side[2] * side[2])) << "\n\n\n";
#endif

        // Phythagoras and comparison
        if (std::fabs(((side[0] * side[0]) + (side[1] * side[1])) - (side[2] * side[2])) < 0.001)
            std::cout << "The sides represents right triangle.\n";
        else
            std::cout << "The sides don’t represents right triangle.\n";
    }
    else std::cerr << "\n\n*** Error: Invalid input\n\n";
}
A M
  • 14,694
  • 5
  • 19
  • 44