Python| Numpy np.assert_almost_equal() 方法
在np.assert_almost_equal()
方法的帮助下,如果两个项目不等于所需的精度值,我们可以使用np.assert_almost_equal()
() 方法得到断言错误。
Syntax : np.assert_almost_equal(actual, desired, decimal)
Return : Return the assertion error if two values are not equal.
示例 #1:
在这个例子中,我们可以看到通过使用np.assert_almost_equal()
方法,如果两个值不等于使用该方法的精度值,我们能够得到断言错误。
# import numpy and assert_almost_equal
import numpy as np
import numpy.testing as npt
# using np.assert_almost_equal() method
gfg = npt.assert_almost_equal(1.2222222222, 1.2222222222, decimal = 5)
print(gfg)
输出 :
Nope
示例 #2:
# import numpy and assert_almost_equal
import numpy as np
import numpy.testing as npt
# using np.assert_almost_equal() method
gfg = npt.assert_almost_equal(1.2222222222, 1.2223422222, decimal = 5)
print(gfg)
输出 :
AssertionError:
Arrays are not almost equal to 5 decimals
ACTUAL: 1.2222222222
DESIRED: 1.2223422222