def lsum(iterable):
"Full precision summation using long integers for intermediate values"
# Transform (exactly) a float to m * 2 ** e where m and e are integers.
# Adjust (tmant,texp) and (mant,exp) to make texp the common exponent.
# Given a common exponent, the mantissas can be summed directly.
tmant, texp = 0L, 0
for x in iterable:
mant, exp = frexp(x)
mant, exp = long(mant * 2.0 ** 53), exp-53
if texp > exp:
tmant <<= texp - exp
texp = exp
else:
mant <<= exp - texp
tmant += mant
return float(str(tmant)) * 2.0 ** texp
评论列表
文章目录