您现在的位置是:首页 > 电脑 > 

浮点计算总是偏离1(Float calculation always off by 1)

2025-07-29 11:55:22
浮点计算总是偏离1(Float calculation always off by 1) 根据我的书输入5和6.5这个函数应该返回7.6901。 我一直得到72.6901。 我知道这个公式是完美的,我无法想象它还能是什么。 浮点数是否存在一定程度的不准确性,可能导致此逻辑错误? import math def area(n, side): are
浮点计算总是偏离1(Float calculation always off by 1)

根据我的书输入5和6.5这个函数应该返回7.6901。 我一直得到72.6901。 我知道这个公式是完美的,我无法想象它还能是什么。 浮点数是否存在一定程度的不准确性,可能导致此逻辑错误?

import math def area(n, side): area= (n * (side * side))/( 4 * (math.pi/n)) return area def main(): sideumber = int(input("Enter the number of sides :")) sideLength = float(input("Enter the side: ")) print("The area of the polygon is %f" %(area(sideumber, sideLength))) if __name__ == "__main__": main()

according to my book inputting 5 and 6.5 into this function should return 7.6901. I keep getting 72.6901 though. I know the formula is perfect, I can't imagine what else it could be. Is there a certain degree of inaccuracy with floats that could be causing this logic error?

import math def area(n, side): area= (n * (side * side))/( 4 * (math.pi/n)) return area def main(): sideumber = int(input("Enter the number of sides :")) sideLength = float(input("Enter the side: ")) print("The area of the polygon is %f" %(area(sideumber, sideLength))) if __name__ == "__main__": main()

最满意答案

看起来这是书中的拼写错误。 我只是用mathwords转录了同一个不同的公式:

def area2(n, side): r = 0.5 * side * ((math.pi / n) / math.sin(math.pi / n)) return n * r * r * (math.pi / n)

我也得到72.690170。

Looks like it's a typo in the book. I just transcribed a different formula for the same thing from mathwords:

def area2(n, side): r = 0.5 * side * ((math.pi / n) / math.sin(math.pi / n)) return n * r * r * (math.pi / n)

And I also get 72.690170 .

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/49270.html

相关标签:无
上传时间: 2023-04-13 21:41:55
留言与评论(共有 6 条评论)
本站网友 整形武汉
8分钟前 发表
side)
本站网友 桂平租房
3分钟前 发表
area= (n * (side * side))/( 4 * (math.pi/n)) return area def main()
本站网友 广药集团官网
17分钟前 发表
")) print("The area of the polygon is %f" %(area(sideumber
本站网友 firewall
14分钟前 发表
我只是用mathwords转录了同一个不同的公式: def area2(n
本站网友 天阳美林湾
22分钟前 发表
我知道这个公式是完美的