Message314079
[Uncle Timmy]
> I doubt `fsum()` would add much value here: all the addends have the
> same sign, so cancellation is impossible
fsum() may be overkill for this problem. I mentioned it because the math module already had the requisite code and because it improved accuracy with high dimensional data in machine learning examples I've encountered:
>>> from math import fsum, sqrt
>>> n = 1000
>>> sum([0.1] * n)
99.9999999999986
>>> fsum([0.1] * n)
100.0
>>> sqrt(sum([0.1] * n) / n)
0.3162277660168357
>>> sqrt(fsum([0.1] * n) / n)
0.31622776601683794
# fsum() version exactly matches the decimal crosscheck
>>> getcontext().prec = 40
>>> (sum([Decimal(0.1)] * n) / n).sqrt()
Decimal('0.3162277660168379419769730258850242641698')
If we care about those little differences (about 80 ulp in this example), the single-rounding dot products seems like a better way to go. |
|
Date |
User |
Action |
Args |
2018-03-19 07:04:43 | rhettinger | set | recipients:
+ rhettinger, tim.peters, mark.dickinson, steven.daprano, skrah, serhiy.storchaka |
2018-03-19 07:04:43 | rhettinger | set | messageid: <1521443083.91.0.467229070634.issue33089@psf.upfronthosting.co.za> |
2018-03-19 07:04:43 | rhettinger | link | issue33089 messages |
2018-03-19 07:04:43 | rhettinger | create | |
|