<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Python on DataLeaf</title><link>https://dataleaf-642346.gitlab.io/tags/python/</link><description>Recent content in Python on DataLeaf</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Tue, 03 Jun 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://dataleaf-642346.gitlab.io/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Adding up ten million numbers</title><link>https://dataleaf-642346.gitlab.io/posts/compensated-summation/</link><pubDate>Tue, 03 Jun 2025 00:00:00 +0000</pubDate><guid>https://dataleaf-642346.gitlab.io/posts/compensated-summation/</guid><description>&lt;p&gt;A floating-point sum looks like the most innocent computation in a program. It is computed one addition at a time, &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi mathvariant="normal"&gt;fl&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\hat s_k = \operatorname{fl}(\hat s_{k-1} + x_k)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;, and every addition rounds its result to the nearest representable number. Each rounding is tiny, but a long sum performs millions of them, and their errors do not have to cancel.&lt;/p&gt;&#10;&lt;div class="dataleaf-heading"&gt;&lt;h2 id="how-large-can-the-error-get"&gt;How large can the error get?&lt;/h2&gt;&#10; &lt;a&#10; class="dataleaf-heading-anchor"&#10; href="#how-large-can-the-error-get"&#10; aria-label="Permalink to How large can the error get?"&#10; data-heading-anchor&#10; data-heading-copy="Copy link to How large can the error get?"&#10; data-heading-copied="Copied link to How large can the error get?."&gt;#&lt;/a&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;For recursive summation, the classic bound is&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;</description></item></channel></rss>