为什么会发生?
因此,我已经启动了一个程序来计算一个数字的第n个词:
*x = float(input('Enter a number: '))
n = float(input('Enter the root you would like to calculate: '))
result=x**(1/float(n))
print(f'The {n}th root of = {x} is:',result)*
该程序可与每个数字和根一起使用,但是当我输入x = 64和n = 3时,它显示了以下内容: 3.0 = 64.0的根是:3.9999999999999996
So, I've started a program to calculate the nth root of a number:
*x = float(input('Enter a number: '))
n = float(input('Enter the root you would like to calculate: '))
result=x**(1/float(n))
print(f'The {n}th root of = {x} is:',result)*
The program works with every number and root, but when I enter as x=64 and n=3 it shows this:
The 3.0 root of = 64.0 is: 3.9999999999999996
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如评论中所述,这是浮点数学的性质。
就您而言,您可以在F弦中使用指定符来控制您的打印内容:
As noted in comments, this is the nature of floating point math.
In your case, you can control what you print with a specifier in an f-string:
它是因为浮点被存储的方式。通常,您会看到这样的东西,一个解决方案是始终将结果归结为最近的千分之一。
Its because of the way floating point is stored. Often you'll see stuff like this, a solution is to always round result to the nearest like, thousandth.