我正在开发一个程序,该程序获取三角形上每个长度的值。并应打印该三角形的中值。我使用三角形的阿波罗定理编写了代码。程序运行并给出值。但是当我手动计算它们时。我发现了完全不同的价值。谁能帮我找到这个错误吗?
我在代码中尝试了三角形的阿波罗定理。但在我的代码中找不到任何问题。但它仍然给出了错误的值。
#not working
#m = median of triangle
#formula ==> p^2 + h^2 = 2(m^2 + h^2/2)
import math
import time
for _ in range(1,100):
b = float(input("Enter the value of the Base : "))
p = float(input("Enter the value of the Perpendicular : "))
h = float(input("Enter the value of the Hypertenuse : "))
lhs = (p**2)+(h**2)
print("lhs "+str(lhs))
half_of_hypertenuse_with_square = (h/2)**2
print("half H "+str(half_of_hypertenuse_with_square))
pre_result = lhs/half_of_hypertenuse_with_square
print("pre_result "+str(pre_result))
semi_result = pre_result/2
print("semi_result "+str(semi_result))
final_result = math.sqrt(semi_result)
print("result "+str(final_result))
这是一个固定版本;我重命名了边(垂直和斜边只有在三角形是矩形时才有意义,在这种情况下毕达哥拉斯定理会更容易使用),修复了公式和计算:
# formula ==> a^2 + c^2 = 2(m^2 + (b/2)^2)
import math
import time
for _ in range(1, 100):
b = float(input("Enter the length of the Base : "))
a = float(input("Enter the length of the second side : "))
c = float(input("Enter the length of the third side : "))
lhs = (a ** 2) + (c ** 2)
print(f"lhs : {lhs}")
squared_half_base = (b / 2) ** 2
print(f"Squared half base : {squared_half_base}")
pre_result = lhs / 2 - squared_half_base
print(f"pre_result : {pre_result}")
final_result = math.sqrt(pre_result)
print(f"result {final_result}")