How to take input and verify the result up to 4 decimal places?

165 Views Asked by At

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?

1

There are 1 best solutions below

0
A M On

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";
}