为什么 np.sqrt(1-0.5**2) 不是预期的 return 0.75?

Why does np.sqrt(1-0.5**2) not return 0.75 as expected?

考虑 Python 代码:

import numpy as np
print(np.sqrt(1 - 0.5**2))

这个 return 是一个从 0.86 开始的长小数,而我希望它是 return 0.75。为什么会出现差异?

因为 1-0.5**20.750.75 的平方根是 ~0.86

我不知道您为什么期望它达到 return 0.75。仔细考虑你在做什么:

1 - 0.5**2

平方 0.5,然后用 1 减去它。结果如预期的那样为 0.75。

np.sqrt(1 - 0.5**2)
np.sqrt(0.75)

现在,numpy 取 0.75 的平方根,returns 0.8660254037844386。应该的,因为那是 0.75 的平方根。

循序渐进,发生的事情:

import numpy as np

exp = (0.5**2)

print(exp)

diff = 1 - exp

print(diff)

print(np.sqrt(diff))