patches/python: use math.fsum for the sum of numeric values.
https://docs.python.org/3.12/library/math.html#math.fsum
math.fsum(iterable)
Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums.
The algorithm’s accuracy depends on IEEE-754 arithmetic guarantees and the typical case where the rounding mode is half-even. On some non-Windows builds, the underlying C library uses extended precision addition and may occasionally double-round an intermediate sum causing it to be off in its least significant bit.
For further discussion and two alternative approaches, see the ASPN cookbook recipes for accurate floating point summation.
>>> sum([0.1 for _ in range(10)])
0.9999999999999999
>>> from math import fsum
>>> fsum([0.1 for _ in range(10)])
1.0
This patch brutally cast to int
for int-ish result, that broke one test :
======================================================================
FAIL: test_13_getFormatedData (erp5.component.test.erp5_version.testERP5Commerce.TestCommerce)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<portal_components/test.erp5.testERP5Commerce>", line 661, in test_13_getFormatedData
sale_order.SaleOrder_getFormattedTotalPrice())
AssertionError: '20.0 None' != '20 None'
----------------------------------------------------------------------
Except that, I had no failures/errors with this change.
If overridden sum()
always returns float
for numeric values, we need to fix some code, like :
product/ERP5Form/ListBox.py
def query(self):
...
self.total_size = end = sum(s.object_list_len for s in report_section_list)
...
for i in xrange(start, end):
TypeError: integer argument expected, got float