特定的直角三角形在 Cpp 中未被识别为直角

Dan*_*mad 2 c++ cmath sqrt pythagorean

我们的 OOP 老师给了我一个作业。尽管我的代码看起来不错,但我正面临这个问题。

我必须从用户那里获取三角形顶点的坐标,并且必须判断它是否是直角三角形。所以我简单地使用毕达哥拉斯定理来找出它,众所周知使用条件:h * h = b * b + p * p

但令人惊讶的是,这不适用于某些特定的直角三角形。这是一个这样的三角形:

顶点 A: (x, y) = (1, 3)

顶点 B: (x, y) = (1, 1)

顶点 C: (x, y) = (5, 1)

计算完美,我通过打印计算得出了这一点,但仍然不起作用

然后我尝试通过这种方式使用库中的sqrt()函数cmath: h = sqrt(b * b + p * p)

逻辑上是一样的,但它起作用了。

我想明白,为什么之前的方法不起作用?

这是我的代码的简化版本

#include <iostream>
#include <cmath>

using namespace std;

class Vertex {

    double x, y;

public:
    void take_input(char obj) {
        cout << endl << "   Taking Coordinates of Vertex " << obj << ": " << endl;

        cout << "       Enter the x component: ";
        cin >> x;
        cout << "       Enter the y component: ";
        cin >> y;
    }

    double distance(Vertex p) {
        double dist = sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y));

        return dist;
    }
};

class Triangle {
    Vertex a, b, c;

public:

    void take_inp(string obj) {
        cout << endl << "Taking Vertices of the Triangle " << obj << ": " << endl;
        cout << "   Verteces should be in a counter clockwise order (as per convention)." << endl;

        a.take_input('A');
        b.take_input('B');
        c.take_input('C');
    }

    void is_rt_ang() {

        double h = a.distance(c)*a.distance(c);
        double bp = a.distance(b)*a.distance(b) + b.distance(c)*b.distance(c);

        /*
            // Strangely this attempt works which is logically the same: 
            double h = a.distance(c);
            double bp = sqrt(a.distance(b)*a.distance(b) + b.distance(c)*b.distance(c));
        */

        if (h == bp) {
            cout << "Angle is 90" << endl;
            cout << h << " = " << bp << endl;
            cout << "It is Right-Angled" << endl;
        }
        else {
            cout << "Angle is not 90!" << endl;
            cout << h << " != " << bp << endl;
            cout << "It is Not a Right-Angled" << endl;
        }
    }
};

int main()
{
    Triangle tri1, tri2;

    tri1.take_inp("tri1");

    tri1.is_rt_ang();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

And*_*dov 5

线

double dist = sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y));
Run Code Online (Sandbox Code Playgroud)

在该Vertex::distance方法中为您提供了平方根的近似值,该近似值很少与精确答案重合。这是因为大多数实数不能用浮点算术表示。

但是在给定的代码示例中,您可以不用sqrt. 用Vertex::distance方法替换方法

 double distance_square(Vertex p) {
    double dist_square = (x-p.x)*(x-p.x) + (y-p.y)*(y-p.y);
    return dist_square;
}
Run Code Online (Sandbox Code Playgroud)

并像这样调用它Triangle::is_rt_ang

    double h = a.distance_square(c);
    double bp = a.distance_square(b) + b.distance_square(c);
Run Code Online (Sandbox Code Playgroud)

该解决方案仍然存在缺陷,因为浮点乘法也存在舍入误差。但是,如果保证您只使用整数坐标,您可以用整数替换代码中的所有双精度数,并且对于它们,乘法没有问题(除了可能超出大数的界限)。

编辑:也是对印刷的评论

它计算完美,我通过打印计算得出了这一点,但仍然不起作用。

当您打印双打时,您需要手动设置精度以避免四舍五入。如果在你的代码中我替换了一行

cout << h << " != " << bp << endl;
Run Code Online (Sandbox Code Playgroud)

cout << std::setprecision(std::numeric_limits<double>::digits10) << std::fixed << h << " != " << bp << endl;
Run Code Online (Sandbox Code Playgroud)

然后例如问题中的三角形我得到输出

角度不是90!
20.000000000000004 != 20.000000000000000
这不是直角

为此编译,您需要添加#include <limits>#include <iomanip>

  • @DaniyalAhmad 值不相同,因为涉及浮点算术的两个不同表达式会产生略有不同的舍入误差。如果这种效果对您来说是新的,我建议您查看 Alex Riveron 的答案中的链接 - [浮点数学是否已损坏?](/sf/ask/41160311/) 有很多很好的答案和有用的链接。 (2认同)
  • 旁注: `sqrt(a*a+b*b)` 几乎总是应该被 `hypot(a,b)` 替换。 (2认同)