Python3中浮点的精确累积和
给定Python中的浮点列表,生成该列表的累计和的最准确方法是什么 我所说的精确是指对舍入误差的鲁棒性 特别是,我试图了解我是应该使用listitertools.acgregatemy_list还是应该使用math.fsum成对计算此列表 Numpy不是一个选项 itertools.acgregatemy_list是否在内部为math.fsum之类的浮点使用精确求和函数 在这种情况下,他们的区别是什么 如果可能,可以将math.fsum指定为itertools.accumulate中的加法函数,这有意义吗 itertools.acgregatemy_list是否在内部为math.fsum之类的浮点使用精确求和函数 否,PyNumber\U Add在中使用。PyNumber_Add是python中的 如果可能,可以将itertools.contractive中指定的math.fsum作为要使用的加法函数吗 是的,您可以使用以下选项指定: 这有意义吗 不,就像数学一样 通过跟踪多个中间部分和避免精度损失 因此,一次使用两个以上的浮动才有意义:Python3中浮点的精确累积和,python,python-3.x,math,floating-point,itertools,Python,Python 3.x,Math,Floating Point,Itertools,给定Python中的浮点列表,生成该列表的累计和的最准确方法是什么 我所说的精确是指对舍入误差的鲁棒性 特别是,我试图了解我是应该使用listitertools.acgregatemy_list还是应该使用math.fsum成对计算此列表 Numpy不是一个选项 itertools.acgregatemy_list是否在内部为math.fsum之类的浮点使用精确求和函数 在这种情况下,他们的区别是什么 如果可能,可以将math.fsum指定为itertools.accumulate中的加法函数,
>>> import math
>>> .1 + .2 + .3 == math.fsum([.1, .2, .3])
False
>>> .1 + .2 == math.fsum([.1, .2])
True
你很可能想要这样的东西:
>>> import math
>>>
>>> l = [.1, .2, .3, .4, .5]
>>> [math.fsum(l[:i+1]) for i in range(len(l))]
[0.1, 0.30000000000000004, 0.6, 1.0, 1.5]
你说的准确是什么意思?它在括号中指定,更新了第一行。从某种意义上说,它对舍入误差更具鲁棒性。
>>> import math
>>>
>>> l = [.1, .2, .3, .4, .5]
>>> [math.fsum(l[:i+1]) for i in range(len(l))]
[0.1, 0.30000000000000004, 0.6, 1.0, 1.5]