如何在Python中计算平方根? [英] How do I calculate square root in Python?
本文介绍了如何在Python中计算平方根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么Python会给出错误"的答案?
Why does Python give the "wrong" answer?
x = 16
sqrt = x**(.5)
returns 4
sqrt = x**(1/2)
returns 1
是的,我知道import math
并使用sqrt
.但我正在寻找上述答案.
Yes, I know import math
and use sqrt
. But I'm looking for an answer to the above.
推荐答案
sqrt=x**(1/2)
正在做整数除法. 1/2 == 0
.
sqrt=x**(1/2)
is doing integer division. 1/2 == 0
.
因此,您在第一个实例中计算x (1/2),在第二个实例中计算x (0).
So you're computing x(1/2) in the first instance, x(0) in the second.
所以没错,这是对其他问题的正确答案.
So it's not wrong, it's the right answer to a different question.
这篇关于如何在Python中计算平方根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文