Python乘以浮点数

时间:2020-07-14 18:04:51

标签: python numpy floating-point

我正在尝试通过将矩阵与转置本身相乘来检查正交矩阵。

import numpy as np


matrix = np.array([[np.sqrt(2)/2, -np.sqrt(2)/2],
                 [np.sqrt(2)/2, np.sqrt(2)/2]])

dot = matrix.T.dot(matrix)

print(dot)

预期产量

[[1. 0.]
 [0. 1.]]

相反,我得到了

[[ 1.00000000e+00 -4.26642159e-17]
 [-4.26642159e-17  1.00000000e+00]]

我尝试使用不同的类型:float64,float128,complex128。但是答案仍然不正确。

1 个答案:

答案 0 :(得分:1)

这来自python除数。这是用二进制文件完成的,它产生了这样的结果。您正在寻找round()函数

matrix = np.array([[round(np.sqrt(2)/2, 2), round(-np.sqrt(2)/2, 2)],
                 [round(np.sqrt(2)/2, 2), round(np.sqrt(2)/2), 2]])