>>> import numpy as np
>>> import math
>>>
>>> a = np.random.random((1000,))
>>>
>>> a -= math.fsum(a)/a.size
# while fsum gives the exact solution rounded to machine precision
# there will be a residual error
# this must be borne by the smallest term
>>> a[np.argmin(np.abs(a))] -= math.fsum(a)
>>>
# now we are zero to machine precision
>>> math.fsum(a)
0.0
下面是一种使用
math.fsum
使平均零到机器精度的方法。你知道吗坏消息是:numpy没有意识到。。。。你知道吗
相关问题 更多 >
编程相关推荐