根据我的书输入5和6.5进入此功能应该返回73.6901.我一直得到72.6901.我知道公式是完美的,我无法想象它还能是什么.浮点数是否存在一定程度的不准确性,可能导致此逻辑错误?
import math
def area(n, side):
area= (n * (side * side))/(
4 * math.tan(math.pi/n))
return area
def main():
sideNumber = int(input("Enter the number of sides :"))
sideLength = float(input("Enter the side: "))
print("The area of the polygon is %f" %(area(sideNumber, sideLength)))
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
看起来这是书中的拼写错误.我只是用mathwords转录了同一个不同的公式:
def area2(n, side):
r = 0.5 * side * (math.cos(math.pi / n) / math.sin(math.pi / n))
return n * r * r * math.tan(math.pi / n)
Run Code Online (Sandbox Code Playgroud)
我也得到72.690170.