【发布时间】:2022-11-17 14:50:28
【问题描述】:
void isRight(float sideA, float sideB, float sideC){
float aSquared = sideA * sideA;
float bSquared = sideB * sideB;
float cSquared = sideC * sideC;
round(aSquared);
round(bSquared);
round(cSquared);
if (aSquared + bSquared == cSquared || cSquared + bSquared == aSquared || aSquared + cSquared == bSquared){
cout << "This is a right triangle" << endl;
}
}
int main() {
float sideA, sideB, sideC;
cout << "Enter the lengths of the three sides of a triangle -- ";
cin >> sideA >> sideB >> sideC;
isRight(sideA,sideB,sideC);
}
}
我有这个程序可以检查我的三角形的边并判断它是否是直角三角形。当我有像 3 5 4 这样的整数时它会工作,它会给我一个消息,它是一个直角三角形。但是如果输入 1 1.4142 1,它不会给我消息,这很奇怪,因为 1,4142 的平方等于 2,这与 1 和 1 相同。有人能明白为什么会这样吗?我猜是因为它没有四舍五入,但我有四舍五入的功能
【问题讨论】:
-
round(x);没有作用,你要x = round(x);。 -
你误解了 round 函数的工作原理,它回报四舍五入的价值。所以如果你想使用一个四舍五入的值,那么你需要像
aSquared = round(aSquared);或float aSquared = round(sideA * sideA);这样的东西。这个版本round(aSquared);什么都不做,因为它忽略了返回值。 -
那就是说我不认为 round 函数是你问题的答案,你需要类似下面的答案。而且您需要了解浮点运算本质上是不准确的。
标签: c++