<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Matteo Foglieni</title>
<link>https://foglienimatteo.com/blog/</link>
<atom:link href="https://foglienimatteo.com/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Personal website of Matteo Foglieni, posting every now and then about physics and computer science (especially Julia)</description>
<generator>quarto-1.11.9</generator>
<lastBuildDate>Sun, 01 Nov 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Why 0.1 isn’t 0.1: floating-point rounding error (part 1)</title>
  <link>https://foglienimatteo.com/blog/floating-point-rounding-error/</link>
  <description><![CDATA[ 





<p>References:</p>
<ul>
<li>[1] D. Goldberg: <em>“What Every Computer Scientist Should Know About Floating-Point Arithmetic”</em>, ACM Computing Surveys, Vol. 23 Num. 1 pag. 5–48 (1991), DOI <a href="https://doi.org/10.1145/103162.103163">10.1145/103162.103163</a></li>
</ul>
<section id="tldr" class="level2">
<h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<p>Notation: <img src="https://latex.codecogs.com/png.latex?%5Coplus,%20%5Cominus,%20%5Cotimes,%20%5Coslash">, etc are the computed operations; <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D">, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BSQRT%7D">, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BEXP%7D">, etc are the computed functions. For a real number <img src="https://latex.codecogs.com/png.latex?x"> approximated by <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D">, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20%5Clvert%20%5Ctilde%7Bx%7D%20-%20x%20%5Crvert"> is the absolute error and <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20%5Clvert%20%5Ctilde%7Bx%7D%20-%20x%20%5Crvert/%5Clvert%20x%20%5Crvert"> the relative error (Section 2.1).</p>
<p>A floating-point number <img src="https://latex.codecogs.com/png.latex?x"> with base <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and precision <img src="https://latex.codecogs.com/png.latex?p"> can be written normalized (i.e.&nbsp;with first significant digit non-zero) as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%20%5Cpm%5C,%20d_0.d_1%20d_2%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee%20%5C%5C%5B5pt%5D%0A&amp;=%20%5Cpm%5Cleft(d_0%20+%20d_1%5C,%5Cbeta%5E%7B-1%7D%20+%20%5Cdots%20+%20d_%7Bp-1%7D%5C,%5Cbeta%5E%7B-(p-1)%7D%5Cright)%5Cbeta%5Ee%20%5C,%20,%20%5Cquad%200%20%5Cle%20d_i%20%3C%20%5Cbeta%0A%5Cend%7Balign*%7D%0A%7D%0A%5Ctag%7B1.1%7D%0A"></p>
<p><strong>Ulps</strong>: rounding a real number <img src="https://latex.codecogs.com/png.latex?x"> to the nearest floating-point number <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20=%20d_0.d_1%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee"> commits an absolute error of at most half a <strong>unit in the last place</strong> (<strong>ulp</strong>), <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D%20=%20%5Cbeta%5E%7Be-p+1%7D">. Relative to <img src="https://latex.codecogs.com/png.latex?x">, the exponent cancels, and the relative error is at most the machine epsilon <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5Cvarepsilon%20=%20%5Cvarepsilon%5B%5Cbeta,%5C,p%5D%20:=%20%5Cfrac%7B%5Cbeta%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%7D%20%5Ctag%7B2.4a%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20%5Cle%20%5Cvarepsilon%7D%20%5Ctag%7B2.4b%7D%0A"></p>
<p><strong>Guard digits</strong>: computing <img src="https://latex.codecogs.com/png.latex?x%20-%20y"> with only <img src="https://latex.codecogs.com/png.latex?p"> digits can give a relative error as large as <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20-%201"> (Theorem 1); a single extra digit brings it below <img src="https://latex.codecogs.com/png.latex?2%5Cvarepsilon"> (Theorem 2):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctext%7Bno%20guard%20digit:%7D%20&amp;%5Cquad%20%5Cmathrm%7Brerr%7D(x%20-%20y,%5C;%20x%20%5Cominus%20y)%20%5Cle%20%5Cbeta%20-%201%20%5C%5C%5B6pt%5D%0A%5Ctext%7Bone%20guard%20digit:%7D%20&amp;%5Cquad%20%5Cmathrm%7Brerr%7D(x%20-%20y,%5C;%20x%20%5Cominus%20y)%20%3C%202%5Cvarepsilon%0A%5Cend%7Balign*%7D%0A"></p>
<p><strong>Cancellation</strong>: subtracting two nearby numbers that already carry rounding errors is <em>catastrophic</em>; subtracting exactly known numbers is <em>benign</em>. Many formulas can be rewritten so that only benign cancellations occur:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 23%">
<col style="width: 55%">
</colgroup>
<thead>
<tr class="header">
<th>quantity</th>
<th>naive form</th>
<th>rewritten form</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>root of <img src="https://latex.codecogs.com/png.latex?ax%5E2%20+%20bx%20+%20c">, <img src="https://latex.codecogs.com/png.latex?b%20%3E%200"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B-b%20+%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%7B2a%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B2c%7D%7B-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D">, Eq.(4.2)</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?x%5E2%20-%20y%5E2"></td>
<td><img src="https://latex.codecogs.com/png.latex?(x%20%5Cotimes%20x)%20%5Cominus%20(y%20%5Cotimes%20y)"></td>
<td><img src="https://latex.codecogs.com/png.latex?(x%20%5Cominus%20y)%20%5Cotimes%20(x%20%5Coplus%20y)"></td>
</tr>
<tr class="odd">
<td>area of a triangle</td>
<td>Heron’s formula, Eq.(4.4)</td>
<td>Kahan’s formula, Eq.(4.5)</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)">, with <img src="https://latex.codecogs.com/png.latex?x%20%5Cll%201"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)"></td>
<td><img src="https://latex.codecogs.com/png.latex?x%20%5Cotimes%20%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)%20%5Coslash%20%5Cbig((1%20%5Coplus%20x)%20%5Cominus%201%5Cbig)">, Eq.(4.8)</td>
</tr>
</tbody>
</table>
<p><strong>Exact rounding</strong> (compute exactly, then round to the nearest) with <strong>round half to even</strong> keeps <img src="https://latex.codecogs.com/png.latex?(x%20%5Cominus%20y)%20%5Coplus%20y"> from drifting (Theorem 5), allows to split a number into two half-precision parts (Theorem 6), and guarantees <img src="https://latex.codecogs.com/png.latex?(m%20%5Coslash%20n)%20%5Cotimes%20n%20=%20m"> for <img src="https://latex.codecogs.com/png.latex?n%20=%202%5Ei%20+%202%5Ej"> (Theorem 7).</p>
</section>
<section id="floating-point-formats" class="level2">
<h2 class="anchored" data-anchor-id="floating-point-formats">1. Floating-point formats</h2>
<section id="representation" class="level3">
<h3 class="anchored" data-anchor-id="representation">1.1 Representation</h3>
<p>Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation: the result of most computations with real numbers cannot be represented exactly, and must be rounded to fit back into the finite representation. The resulting rounding error is the characteristic feature of floating-point computation.</p>
<p>A floating-point representation has a base <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> (always assumed to be even) and a precision <img src="https://latex.codecogs.com/png.latex?p">. A number <img src="https://latex.codecogs.com/png.latex?x"> is written as <img src="https://latex.codecogs.com/png.latex?%5Cpm%5C,%20d.dd%20%5Ccdots%20d%20%5Ctimes%20%5Cbeta%5Ee">, where the <img src="https://latex.codecogs.com/png.latex?p">-digit number <img src="https://latex.codecogs.com/png.latex?d.dd%20%5Ccdots%20d"> is the <em>significand</em> (formerly called <em>mantissa</em>) and <img src="https://latex.codecogs.com/png.latex?e"> is the exponent. More precisely, <img src="https://latex.codecogs.com/png.latex?%5Cpm%5C,%20d_0.d_1%20d_2%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee"> represents the number</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%20%5Cpm%5C,%20d_0.d_1%20d_2%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee%20%5C%5C%5B5pt%5D%0A&amp;=%20%5Cpm%5Cleft(d_0%20+%20d_1%5C,%5Cbeta%5E%7B-1%7D%20+%20%5Cdots%20+%20d_%7Bp-1%7D%5C,%5Cbeta%5E%7B-(p-1)%7D%5Cright)%5Cbeta%5Ee%20%5C,%20,%20%5Cquad%200%20%5Cle%20d_i%20%3C%20%5Cbeta%0A%5Cend%7Balign*%7D%0A%7D%0A%5Ctag%7B1.1%7D%0A"></p>
<p>The term <em>floating-point number</em> means a real number that can be represented exactly in the format under discussion. The exponent is bounded, <img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmin%7D%20%5Cle%20e%20%5Cle%20e_%7B%5Cmax%7D">.</p>
<p><strong>Example</strong>: the number <img src="https://latex.codecogs.com/png.latex?x%20=%200.1">.</p>
<ul>
<li>With <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010"> and <img src="https://latex.codecogs.com/png.latex?p%20=%203">, <img src="https://latex.codecogs.com/png.latex?0.1"> is represented exactly as <img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%2010%5E%7B-1%7D">.</li>
<li>With <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202"> and <img src="https://latex.codecogs.com/png.latex?p%20=%2024">, <img src="https://latex.codecogs.com/png.latex?0.1"> cannot be represented exactly: it has a finite decimal representation, but an infinite binary one. Doubling the fractional part over and over gives its binary digits one by one:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A0.1%20%5Ctimes%202%20&amp;=%200.2%20%5C;%20%5Crightarrow%20%5C;%200%20%5C%5C%5B2pt%5D%0A0.2%20%5Ctimes%202%20&amp;=%200.4%20%5C;%20%5Crightarrow%20%5C;%200%20%5C%5C%5B2pt%5D%0A0.4%20%5Ctimes%202%20&amp;=%200.8%20%5C;%20%5Crightarrow%20%5C;%200%20%5C%5C%5B2pt%5D%0A0.8%20%5Ctimes%202%20&amp;=%201.6%20%5C;%20%5Crightarrow%20%5C;%201%20%5C%5C%5B2pt%5D%0A0.6%20%5Ctimes%202%20&amp;=%201.2%20%5C;%20%5Crightarrow%20%5C;%201%20%5C%5C%5B2pt%5D%0A0.2%20%5Ctimes%202%20&amp;=%200.4%20%5C;%20%5Crightarrow%20%5C;%200%20%5Cquad%20%5Ctext%7B(back%20to%20%7D%200.2%20%5Ctext%7B:%20from%20here%20on%20the%20digits%20%7D%200011%20%5Ctext%7B%20repeat)%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A0.1%20=%20(0.0%5C,0011%5C,0011%5C,0011%5C,0011%20%5Cldots)_2%20=%20(1.1001%5C,1001%5C,1001%20%5Cldots)_2%20%5Ctimes%202%5E%7B-4%7D%20%5Ctag%7B1.2%7D%0A"></p>
<p>Keeping <img src="https://latex.codecogs.com/png.latex?p%20=%2024"> digits (one before the binary point and 23 after it, which is what a single-precision number in IEEE 754 standard does), the first discarded digits are <img src="https://latex.codecogs.com/png.latex?1100%20%5Cldots">, more than half a unit of the last kept digit, so we round up:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A0.1%20&amp;=%201.10011001100110011001100%5C,%5Cbig%7C%5C,1100%20%5Cldots%20%5Ctimes%202%5E%7B-4%7D%20%5C%5C%5B6pt%5D%0A&amp;%5Capprox%201.10011001100110011001101%20%5Ctimes%202%5E%7B-4%7D%20%5C%5C%5B6pt%5D%0A&amp;=%2013421773%20%5Ctimes%202%5E%7B-27%7D%20%20%5C%5C%5B6pt%5D%0A&amp;=%200.100000001490116119384765625%0A%5Cend%7Balign*%7D%20%5Ctag%7B1.3%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?13421773"> is the 24-digit significand read as an integer, and <img src="https://latex.codecogs.com/png.latex?2%5E%7B-27%7D%20=%202%5E%7B-23%7D%20%5Ctimes%202%5E%7B-4%7D"> is the weight of its last digit. In this single-precision representation, <img src="https://latex.codecogs.com/png.latex?0.1"> lies strictly between the two consecutive floating-point numbers <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?b"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A&amp;%5Cquad%5Cquad%20a%20%3C%200.1%20%3C%20b%20%5C%5C%5B6pt%5D%0A&amp;a%20=%2013421772%20%5Ctimes%202%5E%7B-27%7D%20=%200.0999999940%5Cldots%20%5C%5C%5B6pt%5D%0A&amp;b%20=%2013421773%20%5Ctimes%202%5E%7B-27%7D%20=%200.1000000015%5Cldots%0A%5Cend%7Balign*%7D%20%5Ctag%7B1.4%7D%0A"></p>
<p>so it is exactly representable by neither of them: the closest one, <img src="https://latex.codecogs.com/png.latex?b">, is in error by <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(0.1,%20b)%20=%201.49%20%5Ctimes%2010%5E%7B-9%7D">, i.e.&nbsp;by <img src="https://latex.codecogs.com/png.latex?0.2"> units of the last digit (<img src="https://latex.codecogs.com/png.latex?2%5E%7B-27%7D">).</p>
</section>
<section id="encoding" class="level3">
<h3 class="anchored" data-anchor-id="encoding">1.2 Encoding</h3>
<p>Given a base <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, a precision <img src="https://latex.codecogs.com/png.latex?p"> and max/min integer exponents <img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmax%7D/e_%7B%5Cmin%7D">, there are <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ep"> possible significands and <img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmax%7D%20-%20e_%7B%5Cmin%7D%20+%201"> possible exponents. So, a floating-point number <img src="https://latex.codecogs.com/png.latex?x"> can be encoded in a number of bits equal to</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN_%7B%5Cmathrm%7Bbits%7D%7D%5B%5Cbeta,%5C,%20p,%5C,%20e_%7B%5Cmax%7D,%5C,%20e_%7B%5Cmin%7D%5D%20=%0A%5Cbig%5Clceil%20%5Clog_2(e_%7B%5Cmax%7D%20-%20e_%7B%5Cmin%7D%20+%201)%20%5Cbig%5Crceil%20+%20%5Cbig%5Clceil%20%5Clog_2(%5Cbeta%5Ep)%20%5Cbig%5Crceil%20+%201%0A%5Ctag%7B1.5%7D%0A"></p>
<p>and where the final <img src="https://latex.codecogs.com/png.latex?+1"> is the sign bit. The precise encoding is not important for now. Two examples:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 6%">
<col style="width: 3%">
<col style="width: 9%">
<col style="width: 9%">
<col style="width: 28%">
<col style="width: 32%">
<col style="width: 3%">
<col style="width: 4%">
</colgroup>
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?%5Cbeta"></th>
<th><img src="https://latex.codecogs.com/png.latex?p"></th>
<th><img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmin%7D"></th>
<th><img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmax%7D"></th>
<th>exponent</th>
<th>significand</th>
<th>sign</th>
<th>total</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?3"></td>
<td><img src="https://latex.codecogs.com/png.latex?-1"></td>
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2%204%20%5Crceil%20=%202"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2%202%5E3%20%5Crceil%20=%203"></td>
<td>1</td>
<td>6</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?24"></td>
<td><img src="https://latex.codecogs.com/png.latex?-126"></td>
<td><img src="https://latex.codecogs.com/png.latex?127"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2%20254%20%5Crceil%20=%208"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2%202%5E%7B24%7D%20%5Crceil%20=%2024"></td>
<td>1</td>
<td>33</td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?53"></td>
<td><img src="https://latex.codecogs.com/png.latex?-1022"></td>
<td><img src="https://latex.codecogs.com/png.latex?1023"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2%202046%20%5Crceil%20=%2011"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2%202%5E%7B53%7D%20%5Crceil%20=%2053"></td>
<td>1</td>
<td>65</td>
</tr>
</tbody>
</table>
<p>The second and the third ones have the exponent range and the precision of IEEE single and double precision respectively, which nevertheless fit in 32 and 64 bits: the leading digit of a normalized binary significand (Section 1.4) is always 1, so it does not need to be stored. This will be discussed in the next article.</p>
</section>
<section id="why-a-real-number-may-not-be-representable" class="level3">
<h3 class="anchored" data-anchor-id="why-a-real-number-may-not-be-representable">1.3 Why a real number may not be representable</h3>
<p>There are two reasons:</p>
<ol type="1">
<li>the most common one: a real number with a finite representation in one base can have an infinite representation in another, like <img src="https://latex.codecogs.com/png.latex?0.1"> in Eq.(1.2). Then it lies strictly between two floating-point numbers;</li>
<li>the number is out of range: its absolute value is larger than <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Ctimes%20%5Cbeta%5E%7Be_%7B%5Cmax%7D%7D"> or smaller than <img src="https://latex.codecogs.com/png.latex?1.0%20%5Ctimes%20%5Cbeta%5E%7Be_%7B%5Cmin%7D%7D">.</li>
</ol>
<p>This article deals with the first reason only; out-of-range numbers are discussed in the next article.</p>
</section>
<section id="normalized-numbers" class="level3">
<h3 class="anchored" data-anchor-id="normalized-numbers">1.4 Normalized numbers</h3>
<p>Floating-point representations are not unique: both <img src="https://latex.codecogs.com/png.latex?0.01%20%5Ctimes%2010%5E1"> and <img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%2010%5E%7B-1%7D"> represent <img src="https://latex.codecogs.com/png.latex?0.1">.</p>
<p><strong>Normalized representation</strong>: if the leading digit of a floating-point representation of a number is nonzero, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?d_0%20%5Cneq%200"> in Eq.(1.1), the representation is said to be <em>normalized</em></p>
<p>E.g.: <img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%2010%5E%7B-1%7D"> is normalized, <img src="https://latex.codecogs.com/png.latex?0.01%20%5Ctimes%2010%5E1"> is not. <strong>Requiring normalization makes the representation unique</strong>.</p>
<p>In a normalized significand <img src="https://latex.codecogs.com/png.latex?d_0"> can take <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20-%201"> values and each of <img src="https://latex.codecogs.com/png.latex?d_1,%20%5Cldots,%20d_%7Bp-1%7D"> can take <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> values, so the number of positive normalized floating-point numbers is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN_%7B%5Cmathrm%7Bnorm%7D%7D%5B%5Cbeta,%5C,%20p,%5C,%20e_%7B%5Cmax%7D,%5C,%20e_%7B%5Cmin%7D%5D%20=%0A(%5Cbeta%20-%201)%5C,%5Cbeta%5E%7Bp-1%7D%5C,(e_%7B%5Cmax%7D%20-%20e_%7B%5Cmin%7D%20+%201)%0A%5Ctag%7B1.6%7D%0A"></p>
<p>For <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, <img src="https://latex.codecogs.com/png.latex?p%20=%203">, <img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmin%7D%20=%20-1">, <img src="https://latex.codecogs.com/png.latex?e_%7B%5Cmax%7D%20=%202">, this gives <img src="https://latex.codecogs.com/png.latex?1%20%5Ctimes%202%5E2%20%5Ctimes%204%20=%2016"> numbers. The significand <img src="https://latex.codecogs.com/png.latex?1.d_1%20d_2"> is <img src="https://latex.codecogs.com/png.latex?1%20+%20d_1/2%20+%20d_2/4">, and the spacing between consecutive numbers with exponent <img src="https://latex.codecogs.com/png.latex?e"> is one unit of the last digit, <img src="https://latex.codecogs.com/png.latex?2%5E%7B-2%7D%20%5Ctimes%202%5Ee">:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 4%">
<col style="width: 19%">
<col style="width: 19%">
<col style="width: 19%">
<col style="width: 19%">
<col style="width: 19%">
</colgroup>
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?e"></th>
<th><img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%202%5Ee"></th>
<th><img src="https://latex.codecogs.com/png.latex?1.01%20%5Ctimes%202%5Ee"></th>
<th><img src="https://latex.codecogs.com/png.latex?1.10%20%5Ctimes%202%5Ee"></th>
<th><img src="https://latex.codecogs.com/png.latex?1.11%20%5Ctimes%202%5Ee"></th>
<th>spacing <img src="https://latex.codecogs.com/png.latex?2%5E%7Be-2%7D"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?-1"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.625"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.75"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.875"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.125"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?0"></td>
<td><img src="https://latex.codecogs.com/png.latex?1"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.25"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.75"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.25"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?1"></td>
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?2.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?3"></td>
<td><img src="https://latex.codecogs.com/png.latex?3.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.5"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?4"></td>
<td><img src="https://latex.codecogs.com/png.latex?5"></td>
<td><img src="https://latex.codecogs.com/png.latex?6"></td>
<td><img src="https://latex.codecogs.com/png.latex?7"></td>
<td><img src="https://latex.codecogs.com/png.latex?1"></td>
</tr>
</tbody>
</table>
<p><img src="https://foglienimatteo.com/blog/floating-point-rounding-error/figure-1-normalized-numbers.svg" class="img-fluid" alt="Normalized floating-point numbers for beta = 2, p = 3, e_min = -1, e_max = 2"></p>
<p>Each time the exponent grows by one, the spacing doubles: floating-point numbers are not uniformly distributed on the real line, it is their <em>relative</em> spacing that stays (roughly) constant. The bold marks are the numbers with significand <img src="https://latex.codecogs.com/png.latex?1.00">.</p>
<p>Normalization has a drawback: zero cannot be represented, since <img src="https://latex.codecogs.com/png.latex?d_0%20%5Cneq%200">. A natural way to represent it is <img src="https://latex.codecogs.com/png.latex?1.0%20%5Ctimes%20%5Cbeta%5E%7Be_%7B%5Cmin%7D%20-%201%7D">: this preserves the fact that the numerical ordering of nonnegative real numbers corresponds to the lexicographic ordering of their floating-point representations (assuming the exponent is stored to the left of the significand). It also means that, when the exponent is stored in a <img src="https://latex.codecogs.com/png.latex?k">-bit field, only <img src="https://latex.codecogs.com/png.latex?2%5Ek%20-%201"> values are available for use as exponents, since one must be reserved to represent 0.</p>
<p><strong>Notation</strong>: the <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> in a floating-point number is part of the notation, and different from a floating-point multiplication. For example, <img src="https://latex.codecogs.com/png.latex?(2.5%20%5Ctimes%2010%5E%7B-3%7D)%20%5Ctimes%20(4.0%20%5Ctimes%2010%5E%7B2%7D)"> involves only a single floating-point multiplication:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(2.5%20%5Ctimes%2010%5E%7B-3%7D)%20%5Ctimes%20(4.0%20%5Ctimes%2010%5E%7B2%7D)%20=%20(2.5%20%5Ctimes%204.0)%20%5Ctimes%2010%5E%7B-3%20+%202%7D%20=%2010.0%20%5Ctimes%2010%5E%7B-1%7D%20=%201.0%20%5Ctimes%2010%5E%7B0%7D%0A"></p>
<p>where the last step renormalizes the significand.</p>
</section>
</section>
<section id="relative-error-and-ulps" class="level2">
<h2 class="anchored" data-anchor-id="relative-error-and-ulps">2. Relative error and ulps</h2>
<p><strong>Assumption</strong>: from now on in this section, unless stated otherwise, <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010"> and <img src="https://latex.codecogs.com/png.latex?p%20=%203">.</p>
<section id="units-in-the-last-place-absolute-and-relative-error" class="level3">
<h3 class="anchored" data-anchor-id="units-in-the-last-place-absolute-and-relative-error">2.1 Units in the last place, absolute and relative error</h3>
<p><strong>Ulps</strong>: if the result of a floating-point computation is e.g.&nbsp;<img src="https://latex.codecogs.com/png.latex?3.12%20%5Ctimes%2010%5E%7B-2%7D"> and the answer computed with infinite precision is <img src="https://latex.codecogs.com/png.latex?0.0314">, the result is in error by 2 <strong>units in the last place (ulps)</strong>, i.e.&nbsp;by 2 units of its last digit.</p>
<p>Note that what an ulp is exactly depends on the base <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, the precision <img src="https://latex.codecogs.com/png.latex?p"> and <strong>the exponent <img src="https://latex.codecogs.com/png.latex?e"></strong> of the floating-point number: it is the weight of its last digit <img src="https://latex.codecogs.com/png.latex?d_%7Bp-1%7D">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D%20:=%20%5Cbeta%5E%7B-(p-1)%7D%5C,%5Cbeta%5E%7Be%7D%20=%20%5Cbeta%5E%7Be%20-%20p%20+%201%7D%0A%5Ctag%7B2.1a%7D%0A"></p>
<p>For example, with <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010"> and <img src="https://latex.codecogs.com/png.latex?p%20=%203">, one ulp of <img src="https://latex.codecogs.com/png.latex?3.12%20%5Ctimes%2010%5E%7B-2%7D"> is <img src="https://latex.codecogs.com/png.latex?10%5E%7B-4%7D">, while one ulp of <img src="https://latex.codecogs.com/png.latex?9.92%20%5Ctimes%2010%5E%7B1%7D"> is <img src="https://latex.codecogs.com/png.latex?10%5E%7B-1%7D">.</p>
<p><strong>Absolute error</strong>: if the floating-point number <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20=%20d_0.d_1%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee"> is used to represent the real number <img src="https://latex.codecogs.com/png.latex?x">, it is in error by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20:=%20%7C%5Ctilde%7Bx%7D%20-%20x%7C%0A%5Ctag%7B2.1b%7D%0A"></p>
<p>It is often convenient to measure the absolute error in ulps, i.e.&nbsp;to use <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D"> as the unit of measure: <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20n"> ulps means <img src="https://latex.codecogs.com/png.latex?%5Clvert%20%5Ctilde%7Bx%7D%20-%20x%20%5Crvert%20=%20n%20%5Ctimes%20%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D">. Dividing Eq.(2.1b) by Eq.(2.1a), and using <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D/%5Cbeta%5Ee%20=%20d_0.d_1%20%5Ccdots%20d_%7Bp-1%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%0A=%20%5Cfrac%7B%7C%5Ctilde%7Bx%7D%20-%20x%7C%7D%7B%5Cbeta%5E%7Be%20-%20p%20+%201%7D%7D%20%5C;%20%5Cmathrm%7Bulps%7D%0A=%20%5Cleft%7C%20d_0.d_1%20%5Ccdots%20d_%7Bp-1%7D%20-%20%5Cfrac%7Bx%7D%7B%5Cbeta%5Ee%7D%20%5Cright%7C%5C,%5Cbeta%5E%7Bp-1%7D%20%5C;%20%5Cmathrm%7Bulps%7D%0A%5Ctag%7B2.1c%7D%0A"></p>
<p>as long as <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5E%7Be_%7B%5Cmin%7D%7D%20%5Cle%20x%20%5Cle%20%5Cbeta%5E%7Be_%7B%5Cmax%7D%20+%201%7D">. Note that the exponent <img src="https://latex.codecogs.com/png.latex?e"> is the one of the floating-point number <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D">, not of <img src="https://latex.codecogs.com/png.latex?x">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A&amp;%5Ctilde%7Bx%7D%20=%203.12%20%5Ctimes%2010%5E%7B-2%7D%20%5C;%20,%20%5C;%20x%20=%200.0314%20%5C,%20:%0A&amp;%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20&amp;=%20%7C3.12%20-%203.14%7C%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%0A&amp;&amp;&amp;=%202%20%5C;%20%5Cmathrm%7Bulps%7D%20=%202%20%5Ctimes%2010%5E%7B-4%7D%20%5C%5C%5B6pt%5D%0A&amp;%5Ctilde%7Bx%7D%20=%203.14%20%5Ctimes%2010%5E%7B-2%7D%20%5C;%20,%20%5C;%20x%20=%200.0314159%20%5C,%20:%0A&amp;%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20&amp;=%20%7C3.14%20-%203.14159%7C%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%0A&amp;&amp;&amp;=%200.159%20%5C;%20%5Cmathrm%7Bulps%7D%20=%201.59%20%5Ctimes%2010%5E%7B-5%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>If <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"> is the floating-point number nearest to <img src="https://latex.codecogs.com/png.latex?x">, it can still be in error by as much as half an ulp.</p>
<p><strong>Relative error</strong>: another way to measure the difference between a floating-point number <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"> and the real number <img src="https://latex.codecogs.com/png.latex?x"> it approximates is the <em>relative error</em>:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20:=%20%5Cfrac%7B%7C%5Ctilde%7Bx%7D%20-%20x%7C%7D%7B%7Cx%7C%7D%20=%20%5Cfrac%7B%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%7D%7B%7Cx%7C%7D%0A%5Ctag%7B2.2%7D%0A"></p>
<p>where, in the last fraction, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D"> is expressed in the same units as <img src="https://latex.codecogs.com/png.latex?x">, not in ulps. For example:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ax%20=%203.14159%5C;%20&amp;,%20%5Cquad%20%5Ctilde%7Bx%7D%20=%203.14%20%5Ctimes%2010%5E0%20%5C%5C%5B6pt%5D%0A%5C;%20%5CRightarrow%20%5C;%0A%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20&amp;=%200.00159%20=%200.159%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%5B6pt%5D%0A%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20&amp;=%20%5Cfrac%7B0.00159%7D%7B3.14159%7D%20=%205.06%20%5Ctimes%2010%5E%7B-4%7D%20%5Capprox%200.0005%0A%5Cend%7Balign*%7D%0A"></p>
<p><strong>Notation</strong>: square brackets list the parameters of the floating-point format, as in <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D"> or <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%5B%5Cbeta,%5C,p%5D"> below; round brackets list the exact value and its approximation, as in <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)">. The errors depend on <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and <img src="https://latex.codecogs.com/png.latex?p"> only through <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D">.</p>
</section>
<section id="half-an-ulp-and-the-machine-epsilon" class="level3">
<h3 class="anchored" data-anchor-id="half-an-ulp-and-the-machine-epsilon">2.2 Half an ulp and the machine epsilon</h3>
<p>When a real number <img src="https://latex.codecogs.com/png.latex?x"> is rounded to the closest floating-point number <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20=%20d_0.d_1d_2%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee">, the absolute error can be as large as half an ulp:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20%5Cleq%20%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D%0A&amp;=%20%5Cfrac%7B1%7D%7B2%7D%5C,%5Cbeta%5E%7B-(p-1)%7D%5C,%5Cbeta%5E%7Be%7D%20%5C%5C%5B6pt%5D%0A&amp;=%20%5Cfrac%7B%5Cbeta%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%5C,%5Cbeta%5E%7Be%7D%20%5C%5C%5B6pt%5D%0A&amp;=%200.%5Cunderbrace%7B0%20%5Ccdots%200%7D_%7Bp-1%7D%5CBig(%5Cfrac%7B%5Cbeta%7D%7B2%7D%5CBig)%20%5Ctimes%20%5Cbeta%5E%7Be%7D%0A%5Ctag%7B2.3a%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>That is, the number whose only nonzero digit is <img src="https://latex.codecogs.com/png.latex?%5Cbeta/2">, in the <img src="https://latex.codecogs.com/png.latex?p">-th position after the point; for <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010">, <img src="https://latex.codecogs.com/png.latex?p%20=%203"> this is <img src="https://latex.codecogs.com/png.latex?0.005%20%5Ctimes%2010%5Ee">. <strong>This bound on the absolute error depends on the exponent <img src="https://latex.codecogs.com/png.latex?e"></strong>: the larger the number, the larger the absolute error committed when rounding it.</p>
<p>The relative error, however, divides the absolute error by <img src="https://latex.codecogs.com/png.latex?%5Clvert%20x%20%5Crvert">, which grows with <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ee"> too. All the numbers whose closest floating-point number has the form <img src="https://latex.codecogs.com/png.latex?d_0.d_1d_2%20%5Ccdots%20d_%7Bp-1%7D%20%5Ctimes%20%5Cbeta%5Ee"> lie between <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ee"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Ctimes%20%5Cbeta%5Ee">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%5E%7Be%7D%20%5Cle%20%7Cx%7C%20%5Cle%20%5Cbeta%20%5Ctimes%20%5Cbeta%5E%7Be%7D%0A%5Ctag%7B2.3b%7D%0A"></p>
<p>so, by Eq.(2.2), an absolute error of half an ulp corresponds to the relative error</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%0A=%20%5Cfrac%7B%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%7D%7B%7Cx%7C%7D%0A=%20%5Cfrac%7B%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D%5B%5Cbeta,%5C,p,%5C,e%5D%7D%7B%7Cx%7C%7D%0A=%20%5Cfrac%7B(%5Cbeta/2)%5C,%5Cbeta%5E%7B-p%7D%5C,%5Cbeta%5E%7Be%7D%7D%7B%7Cx%7C%7D%0A%5Ctag%7B2.3c%7D%0A"></p>
<p>which is smallest for the largest <img src="https://latex.codecogs.com/png.latex?%5Clvert%20x%20%5Crvert"> of Eq.(2.3b), and largest for the smallest one:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%7Cx%7C%20%5Cto%20%5Cbeta%20%5Ctimes%20%5Cbeta%5E%7Be%7D%20%5C,%20:%20&amp;%5Cquad%0A%5Cmathrm%7Brerr%7D_%7B%5Cmin%7D%20=%20%5Cfrac%7B(%5Cbeta/2)%5C,%5Cbeta%5E%7B-p%7D%5C,%5Cbeta%5E%7Be%7D%7D%7B%5Cbeta%20%5Ctimes%20%5Cbeta%5E%7Be%7D%7D%20=%20%5Cfrac%7B1%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%20%5C%5C%5B8pt%5D%0A%7Cx%7C%20%5Cto%20%5Cbeta%5E%7Be%7D%20%5C,%20:%20&amp;%5Cquad%0A%5Cmathrm%7Brerr%7D_%7B%5Cmax%7D%20=%20%5Cfrac%7B(%5Cbeta/2)%5C,%5Cbeta%5E%7B-p%7D%5C,%5Cbeta%5E%7Be%7D%7D%7B%5Cbeta%5E%7Be%7D%7D%20=%20%5Cfrac%7B%5Cbeta%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%0A%5Cend%7Balign*%7D%0A%5Ctag%7B2.3d%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cfrac%7B1%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%20%5C;%20%5Cle%20%5C;%20%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%5CBig%7C_%7B%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20%5C,%20=%20%5C,%20%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D%7D%20%5C;%20%5Cle%20%5C;%20%5Cfrac%7B%5Cbeta%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%0A%5Ctag%7B2.3e%7D%0A"></p>
<p><strong>The exponent cancels</strong>: in Eq.(2.3c), <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ee"> appears both in the numerator (the half ulp) and in the denominator (<img src="https://latex.codecogs.com/png.latex?%5Clvert%20x%20%5Crvert">), so the bounds of Eq.(2.3e) depend only on <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and <img src="https://latex.codecogs.com/png.latex?p">. What matters is only where <img src="https://latex.codecogs.com/png.latex?x"> lies between <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ee"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Ctimes%20%5Cbeta%5Ee"> (for <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010">: whether its significand is close to <img src="https://latex.codecogs.com/png.latex?1.00"> or to <img src="https://latex.codecogs.com/png.latex?9.99">), not how large <img src="https://latex.codecogs.com/png.latex?x"> is. [1] writes Eq.(2.3e) as <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%20%5Cle%20%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D%20%5Cle%20%5Cfrac%7B%5Cbeta%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D">, where the middle term is a shorthand for “the relative error corresponding to half an ulp”: it is not the half ulp of Eq.(2.3a), which does contain <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ee">.</p>
<p>With <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010"> and <img src="https://latex.codecogs.com/png.latex?p%20=%203">, the relative size of half an ulp for numbers of very different magnitude:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 19%">
<col style="width: 3%">
<col style="width: 35%">
<col style="width: 41%">
</colgroup>
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"></th>
<th><img src="https://latex.codecogs.com/png.latex?e"></th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D%5B10,%5C,3,%5C,e%5D"></th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D%20%5C,%20/%20%5C,%20%5Ctilde%7Bx%7D"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%2010%5E%7B-2%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?-2"></td>
<td><img src="https://latex.codecogs.com/png.latex?5%20%5Ctimes%2010%5E%7B-5%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.005"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%2010%5E%7B0%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0"></td>
<td><img src="https://latex.codecogs.com/png.latex?5%20%5Ctimes%2010%5E%7B-3%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.005"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?1.00%20%5Ctimes%2010%5E%7B5%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?5"></td>
<td><img src="https://latex.codecogs.com/png.latex?500"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.005"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?9.99%20%5Ctimes%2010%5E%7B-2%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?-2"></td>
<td><img src="https://latex.codecogs.com/png.latex?5%20%5Ctimes%2010%5E%7B-5%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.0005005"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?9.99%20%5Ctimes%2010%5E%7B0%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0"></td>
<td><img src="https://latex.codecogs.com/png.latex?5%20%5Ctimes%2010%5E%7B-3%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.0005005"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?9.99%20%5Ctimes%2010%5E%7B5%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?5"></td>
<td><img src="https://latex.codecogs.com/png.latex?500"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.0005005"></td>
</tr>
</tbody>
</table>
<p>Half an ulp grows by a factor <img src="https://latex.codecogs.com/png.latex?10%5E7"> from <img src="https://latex.codecogs.com/png.latex?e%20=%20-2"> to <img src="https://latex.codecogs.com/png.latex?e%20=%205">, while its relative size does not change at all; for a fixed <img src="https://latex.codecogs.com/png.latex?e">, it varies by a factor <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010"> between the smallest and the largest significand.</p>
<p><strong>Machine epsilon</strong>: the relative error corresponding to half an ulp can thus vary by a factor of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, which is called the <em>wobble</em>. Setting the <em>machine epsilon</em> to the largest of the bounds of Eq.(2.3e),</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5Cvarepsilon%20=%20%5Cvarepsilon%5B%5Cbeta,%5C,p%5D%20:=%20%5Cfrac%7B%5Cbeta%7D%7B2%7D%5C,%5Cbeta%5E%7B-p%7D%7D%20%5Ctag%7B2.4a%7D%0A"></p>
<p>and since rounding to the closest floating-point number always gives <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20%5Cle%20%5Cfrac%7B1%7D%7B2%7D%5C,%5Cmathrm%7Bulp%7D">, Eq.(2.3c)–(2.3e) give, for any real number <img src="https://latex.codecogs.com/png.latex?x"> in range and whatever its exponent,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20%5Cle%20%5Cvarepsilon%7D%20%5Ctag%7B2.4b%7D%0A"></p>
<p>i.e.&nbsp;when a real number is rounded to the closest floating-point number, the relative error is always bounded by <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">. Some values:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?%5Cbeta"></th>
<th><img src="https://latex.codecogs.com/png.latex?p"></th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?10"></td>
<td><img src="https://latex.codecogs.com/png.latex?3"></td>
<td><img src="https://latex.codecogs.com/png.latex?5%20%5Ctimes%2010%5E%7B-3%7D%20=%200.005"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?24"></td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E%7B-24%7D%20%5Capprox%205.96%20%5Ctimes%2010%5E%7B-8%7D"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?2"></td>
<td><img src="https://latex.codecogs.com/png.latex?53"></td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E%7B-53%7D%20%5Capprox%201.11%20%5Ctimes%2010%5E%7B-16%7D"></td>
</tr>
</tbody>
</table>
<p>To avoid such small numbers, relative errors are normally written as a factor times <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">. For the approximation of <img src="https://latex.codecogs.com/png.latex?3.14159"> of Section 2.1:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cbeta%20=%2010%5C;%20&amp;,%20%5Cquad%20p%20=%203%20%5C%5C%5B6pt%5D%0Ax%20=%203.14159%5C;%20&amp;,%20%5Cquad%20%5Ctilde%7Bx%7D%20=%203.14%20%5Ctimes%2010%5E0%20%5C%5C%5B6pt%5D%0A%5C;%20%5CRightarrow%20%5C;%0A%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20&amp;=%20%5Cfrac%7B0.00159%7D%7B3.14159%7D%20=%205.06%20%5Ctimes%2010%5E%7B-4%7D%20%5Capprox%200.0005%20%5C%5C%5B8pt%5D%0A%5Cvarepsilon%5B10,%5C,3%5D%20&amp;=%20%5Cfrac%7B10%7D%7B2%7D%5C,10%5E%7B-3%7D%20=%200.005%20%5C%5C%5B8pt%5D%0A%5C;%20%5CRightarrow%20%5C;%0A%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20&amp;=%0A%5Cfrac%7B5.06%20%5Ctimes%2010%5E%7B-4%7D%7D%7B0.005%7D%5C,%5Cvarepsilon%20%5Capprox%20%200.1%5C,%5Cvarepsilon%0A%5Cend%7Balign*%7D%0A"></p>
</section>
<section id="ulps-versus-relative-error" class="level3">
<h3 class="anchored" data-anchor-id="ulps-versus-relative-error">2.3 Ulps versus relative error</h3>
<p>Consider the real number <img src="https://latex.codecogs.com/png.latex?x%20=%2012.35">. It is approximated by <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20=%201.24%20%5Ctimes%2010%5E1"> (<img src="https://latex.codecogs.com/png.latex?12.35"> is halfway between <img src="https://latex.codecogs.com/png.latex?12.3"> and <img src="https://latex.codecogs.com/png.latex?12.4">; both rounding rules of Section 5.2 give <img src="https://latex.codecogs.com/png.latex?12.4">). Next consider the computation <img src="https://latex.codecogs.com/png.latex?8x">: the exact value is <img src="https://latex.codecogs.com/png.latex?8x%20=%2098.8">, while the computed value is <img src="https://latex.codecogs.com/png.latex?8%5Ctilde%7Bx%7D%20=%208%20%5Ctimes%2012.4%20=%2099.2%20=%209.92%20%5Ctimes%2010%5E1">, which is exactly representable. Using Eq.(2.1c) and Eq.(2.2):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctilde%7Bx%7D%20=%201.24%20%5Ctimes%2010%5E%7B1%7D%20%5C,%20:%20&amp;%5Cquad%20%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20%7C1.24%20-%201.235%7C%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Cmathrm%7Bulps%7D%20=%200.5%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%5B4pt%5D%0A&amp;%5Cquad%20%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20%5Cfrac%7B0.05%7D%7B12.35%7D%20=%204.05%20%5Ctimes%2010%5E%7B-3%7D%20=%200.81%5C,%5Cvarepsilon%20%5C%5C%5B10pt%5D%0A8%5Ctilde%7Bx%7D%20=%209.92%20%5Ctimes%2010%5E%7B1%7D%20%5C,%20:%20&amp;%5Cquad%20%5Cmathrm%7Baerr%7D(8x,%208%5Ctilde%7Bx%7D)%20=%20%7C9.92%20-%209.88%7C%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Cmathrm%7Bulps%7D%20=%204.0%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%5B4pt%5D%0A&amp;%5Cquad%20%5Cmathrm%7Brerr%7D(8x,%208%5Ctilde%7Bx%7D)%20=%20%5Cfrac%7B0.4%7D%7B98.8%7D%20=%204.05%20%5Ctimes%2010%5E%7B-3%7D%20=%200.81%5C,%5Cvarepsilon%0A%5Cend%7Balign*%7D%0A"></p>
<p>The error measured in ulps is eight times larger, even though the relative error is the same: the absolute error has been multiplied by 8, but the ulp has not, since <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"> and <img src="https://latex.codecogs.com/png.latex?8%5Ctilde%7Bx%7D"> have the same exponent. In general, when the base is <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, a fixed relative error expressed in ulps can wobble by a factor of up to <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. Conversely, as Eq.(2.3e) shows, a fixed error of half an ulp results in a relative error that can wobble by <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p>
</section>
<section id="contaminated-digits" class="level3">
<h3 class="anchored" data-anchor-id="contaminated-digits">2.4 Contaminated digits</h3>
<p>When only the order of magnitude of the rounding error matters, ulps and <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"> can be used interchangeably, since they differ by at most a factor of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. If <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20n"> ulps, or if <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x,%20%5Ctilde%7Bx%7D)%20=%20n%5Cvarepsilon">, then</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Bcontaminated%20%5C;%20digits%7D%20%5Capprox%20%5Clog_%5Cbeta%20n%20%5Ctag%7B2.5%7D%0A"></p>
<p>For example, an error of <img src="https://latex.codecogs.com/png.latex?70.8"> ulps (Section 4.1) contaminates <img src="https://latex.codecogs.com/png.latex?%5Clog_%7B10%7D%2070.8%20%5Capprox%201.85"> digits, i.e.&nbsp;about 2 of the 3 digits.</p>
<p>The most natural way to measure the rounding error of a single operation is in ulps: rounding to the nearest floating-point number corresponds to an error of at most half an ulp. When analysing the rounding error caused by a formula, relative error is a better measure. Since <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"> can overestimate the effect of rounding to the nearest floating-point number by the wobble factor <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, error estimates of formulas are tighter on machines with a small <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p>
</section>
<section id="try-it-yourself" class="level3">
<h3 class="anchored" data-anchor-id="try-it-yourself">2.5 Try it yourself</h3>
<p>Everything above is computed right here in your browser. Type a real number <img src="https://latex.codecogs.com/png.latex?x">, pick a base <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and a precision <img src="https://latex.codecogs.com/png.latex?p">, and the box rounds <img src="https://latex.codecogs.com/png.latex?x"> to the nearest number the format can hold and reports the error four ways. The defaults reproduce the <img src="https://latex.codecogs.com/png.latex?x%20=%2012.35"> example of Section 2.3; switch to <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, <img src="https://latex.codecogs.com/png.latex?p%20=%2024"> and type <code>0.1</code> to watch a number that looks exact in decimal fail to be representable in binary. Turn off <em>nearest</em> to compare <img src="https://latex.codecogs.com/png.latex?x"> against an <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"> you choose yourself (e.g.&nbsp;<img src="https://latex.codecogs.com/png.latex?x%20=%200.0314">, <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20="> <code>0.0312</code>, the two-ulp example of Section 2.1).</p>
<div class="controls">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="407" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 406;"><span id="cb1-407">viewof xStr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Real number x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12.35"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">submit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">placeholder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12.35, 0.1, 1/3, pi"</span>})</span>
<span id="cb1-408">viewof beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radio</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span>([[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 (binary)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10 (decimal)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"16 (hex)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>]])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Base β"</span>})</span>
<span id="cb1-409">viewof p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">53</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Precision p"</span>})</span>
<span id="cb1-410">viewof snap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toggle</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x̃ = nearest representable value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>})</span>
<span id="cb1-411">viewof approxStr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"…or set x̃ yourself"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.0312"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">submit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">disabled</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> snap})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-5" data-nodetype="declaration">

</div>
</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="416" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 415;"><span id="cb2-416">parseReal <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-417">  s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toLowerCase</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-418">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> named <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pi</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">PI</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"π"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">PI</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">e</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">E</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">sqrt2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">SQRT2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"√2"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">SQRT2</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-419">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> named) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> named[s]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-420">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^[-+]?\d*\.?\d+(</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">e</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[-+]?\d+)?$</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test</span>(s)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseFloat</span>(s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-421">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^([-+]?\d*\.?\d+)\s*\/\s*([-+]?\d*\.?\d+)$</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-422">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (m) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseFloat</span>(m[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseFloat</span>(m[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-423">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NaN</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-424">}</span>
<span id="cb2-425">exponentOf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-426">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(v)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-427">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(a) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(b))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-428">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> a) e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--;</span></span>
<span id="cb2-429">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> a) e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++;</span></span>
<span id="cb2-430">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-431">}</span>
<span id="cb2-432">toFloat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pr) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-433">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">xt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">e</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ulp</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pr)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">digits</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Array</span>(pr)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">sign</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-434">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sign</span>(v)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(v)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-435">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponentOf</span>(a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ulp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-436">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ulp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> fl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(q)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> fr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> fl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-437">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> fl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> fr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> fl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (fl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> fl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> fl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-438">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ulp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-439">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(xt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) { e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++;</span> ulp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(xt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ulp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> }</span>
<span id="cb2-440">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> digits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-441">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> pr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>) { digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unshift</span>(m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> b)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> b)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> }</span>
<span id="cb2-442">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { xt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ulp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">sign</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> s }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-443">}</span>
<span id="cb2-444">hexd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">String</span>(d) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ABCDEF"</span>[d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]</span>
<span id="cb2-445">fmt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hexd</span>(digits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}${</span>digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(hexd)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> &amp;times; </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>beta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;sup&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/sup&gt;`</span></span>
<span id="cb2-446"></span>
<span id="cb2-447">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parseReal</span>(xStr)</span>
<span id="cb2-448">isDec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^[-+]?\d*\.?\d+$</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test</span>((s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim</span>())</span>
<span id="cb2-449"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Exact base-10 rounding straight from the typed digits, so a halfway case</span></span>
<span id="cb2-450"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// like 12.35 rounds to even (12.4) instead of being nudged off by the fact</span></span>
<span id="cb2-451"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// that 12.35 is itself not representable as a double.</span></span>
<span id="cb2-452">round10 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (str<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pr) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-453">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> str<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-454">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+"</span>) s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>) { sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> }</span>
<span id="cb2-455">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> dot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">indexOf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dot)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> fp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(dot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-456">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (ip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> fp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> allStr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-457">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (allStr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">xt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">e</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ulp</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pr)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">digits</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Array</span>(pr)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">sign</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-458">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (raw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> allStr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-459">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> da <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> allStr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Number</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-460">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-461">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (da<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> pr) {</span>
<span id="cb2-462">    digits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> da<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">concat</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Array</span>(pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> da<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-463">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-464">    digits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> da<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pr)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-465">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> dp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> da[pr]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> restNonzero <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> da<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">some</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-466">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (dp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> (dp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> (restNonzero <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> digits[pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))) {</span>
<span id="cb2-467">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-468">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) { <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>digits[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> digits[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--;</span> }</span>
<span id="cb2-469">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) { digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unshift</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pop</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++;</span> }</span>
<span id="cb2-470">    }</span>
<span id="cb2-471">  }</span>
<span id="cb2-472">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> eOut <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ulp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> eOut <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-473">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>((acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> k) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> eOut <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-474">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { xt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">e</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> eOut<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ulp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> digits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> sign }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-475">}</span>
<span id="cb2-476">floatOf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> str<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pr) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> (b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isDec</span>(str)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round10</span>(str<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFloat</span>(val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pr)</span>
<span id="cb2-477"></span>
<span id="cb2-478">repX <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floatOf</span>(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> xStr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> beta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span></span>
<span id="cb2-479">xApprox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> snap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parseReal</span>(approxStr)</span>
<span id="cb2-480">repT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> snap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> repX <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(xApprox) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floatOf</span>(xApprox<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> approxStr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> beta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span>)</span>
<span id="cb2-481">xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> repT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> repT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xt</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NaN</span></span>
<span id="cb2-482">eT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> repT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> repT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">e</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-483">ulp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> repT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> repT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ulp</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NaN</span></span>
<span id="cb2-484">aerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x)</span>
<span id="cb2-485">aerrUlps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> aerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ulp</span>
<span id="cb2-486">rerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NaN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> aerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x)</span>
<span id="cb2-487">eps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(beta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p)</span>
<span id="cb2-488">rerrEps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> eps</span>
<span id="cb2-489">sci <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(v) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"—"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toExponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-490">tipF <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-bg)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-muted)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> })</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-5" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-6" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-7" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-8" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-9" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-10" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-11" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-12" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-13" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-14" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-15" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-16" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-17" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-18" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-19" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-20" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-21" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-22" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb3" data-startfrom="493" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 492;"><span id="cb3-493"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(x)</span>
<span id="cb3-494">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;p class="input-error"&gt;Couldn't read “</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>xStr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">” — try a decimal like &lt;code&gt;12.35&lt;/code&gt;, a fraction like &lt;code&gt;1/3&lt;/code&gt;, or &lt;code&gt;pi&lt;/code&gt;.&lt;/p&gt;`</span></span>
<span id="cb3-495">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(xt)</span>
<span id="cb3-496">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;p class="input-error"&gt;Couldn't read the approximation “</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>approxStr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">”.&lt;/p&gt;`</span></span>
<span id="cb3-497">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div&gt;</span></span>
<span id="cb3-498"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div class="repr"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>snap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nearest representable value:"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparing against:"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-499"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &amp;nbsp; &lt;b&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"−"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt</span>(repT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> eT)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/b&gt;</span></span>
<span id="cb3-500"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &amp;nbsp;=&amp;nbsp; </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>xt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toPrecision</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-501"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>aerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">` &amp;nbsp;&lt;span style="color:var(--chart-6)"&gt;(exact)&lt;/span&gt;`</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-502"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div class="stats"&gt;</span></span>
<span id="cb3-503"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;ulp[β, p, e]&lt;/div&gt;</span></span>
<span id="cb3-504"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sci</span>(ulp)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-505"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;β&lt;sup&gt;e−p+1&lt;/sup&gt;, e = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>eT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-506"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Absolute error&lt;/div&gt;</span></span>
<span id="cb3-507"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>aerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sci</span>(aerr)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-508"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>aerrUlps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> ulps&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-509"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Relative error&lt;/div&gt;</span></span>
<span id="cb3-510"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"—"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sci</span>(rerr)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-511"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(rerrEps) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> rerrEps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ε"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-512"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Machine epsilon ε&lt;/div&gt;</span></span>
<span id="cb3-513"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sci</span>(eps)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-514"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;(β/2) β&lt;sup&gt;−p&lt;/sup&gt;&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-515"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Contaminated digits&lt;/div&gt;</span></span>
<span id="cb3-516"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>aerrUlps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt; 1"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(aerrUlps) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(beta))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-517"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;≈ log&lt;sub&gt;β&lt;/sub&gt;(ulps)&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-518"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;/div&gt;</span></span>
<span id="cb3-519"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;/div&gt;`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-3" data-nodetype="expression">

</div>
</div>
</div>
<div class="chart">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb4" data-startfrom="524" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 523;"><span id="cb4-524"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(xt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(ulp) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> (() <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb4-525">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> span <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ulp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> aerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> ulp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-526">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> dmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> span<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> span<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-527">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> nt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>((dmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> dmin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ulp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-528">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span></span>
<span id="cb4-529">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceil</span>((dmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> xt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ulp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>((dmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> xt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ulp) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ulp }))</span>
<span id="cb4-530">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-531">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb4-532">    width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginTop</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginBottom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginRight</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-533">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [dmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dmax]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ticks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tickFormat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toPrecision</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-534">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">axis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-535">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb4-536">      Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleY</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-rule)"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-537">      grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleX</span>(grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-1)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeOpacity</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span> }) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-538">      grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>([{ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ulp }]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.22</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"← 1 ulp →"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">dy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-muted)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span> }) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-539">      Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot</span>([{ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> xt }]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-1)"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-540">      Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot</span>([{ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x }]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-highlight)"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-541">      Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>([{ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> xt }]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x̃ (stored)"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">dy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-1)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-542">      Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>([{ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x }]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x (real)"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">dy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-highlight)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> })</span>
<span id="cb4-543">    ]</span>
<span id="cb4-544">  })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-545">})() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">``</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-4" data-nodetype="expression">

</div>
</div>
</div>
</div>
</section>
</section>
<section id="guard-digits" class="level2">
<h2 class="anchored" data-anchor-id="guard-digits">3. Guard digits</h2>
<p><strong>Notation</strong>: to avoid confusion between exact and computed values, <img src="https://latex.codecogs.com/png.latex?x%20-%20y"> denotes the exact difference of <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">, while <img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y"> denotes the computed difference (i.e.&nbsp;with rounding error). Similarly, <img src="https://latex.codecogs.com/png.latex?%5Coplus">, <img src="https://latex.codecogs.com/png.latex?%5Cotimes"> and <img src="https://latex.codecogs.com/png.latex?%5Coslash"> denote computed addition, multiplication and division. All caps indicate the computed value of a function, as in <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(x)"> or <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BSQRT%7D(x)">, while lowercase functions and the traditional notation, as in <img src="https://latex.codecogs.com/png.latex?%5Cln(x)"> and <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bx%7D">, denote their exact values. The error of a computed difference is then, e.g., <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)">.</p>
<section id="exact-difference-then-rounding" class="level3">
<h3 class="anchored" data-anchor-id="exact-difference-then-rounding">3.1 Exact difference, then rounding</h3>
<p>One method of computing the difference between two floating-point numbers is to compute it exactly, then round it to the nearest floating-point number. With <img src="https://latex.codecogs.com/png.latex?p%20=%203">, <img src="https://latex.codecogs.com/png.latex?2.15%20%5Ctimes%2010%5E%7B12%7D%20-%201.25%20%5Ctimes%2010%5E%7B-5%7D"> is computed as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%202.15%20%5Ctimes%2010%5E%7B12%7D%20%5C%5C%5B2pt%5D%0Ay%20&amp;=%200.0000000000000000125%20%5Ctimes%2010%5E%7B12%7D%20%5C%5C%5B2pt%5D%0Ax%20-%20y%20&amp;=%202.1499999999999999875%20%5Ctimes%2010%5E%7B12%7D%20%5C;%20%5Crightarrow%20%5C;%202.15%20%5Ctimes%2010%5E%7B12%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>which is correct, but expensive when the operands differ greatly in size: here we needed 20 digits to produce a 3-digit result.</p>
</section>
<section id="no-guard-digit" class="level3">
<h3 class="anchored" data-anchor-id="no-guard-digit">3.2 No guard digit</h3>
<p>Floating-point hardware normally operates on a fixed number of digits. Suppose that only <img src="https://latex.codecogs.com/png.latex?p"> digits are kept, and that when the smaller operand is shifted right, the digits that fall off are simply discarded (truncation, not rounding). Then the example above becomes</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%202.15%20%5Ctimes%2010%5E%7B12%7D%20%5C%5C%5B2pt%5D%0Ay%20&amp;=%200.00%20%5Ctimes%2010%5E%7B12%7D%20%5C%5C%5B2pt%5D%0Ax%20-%20y%20&amp;=%202.15%20%5Ctimes%2010%5E%7B12%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>and the answer is the same as if the difference had been computed exactly, then rounded. But take <img src="https://latex.codecogs.com/png.latex?10.1%20-%209.93">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%201.01%20%5Ctimes%2010%5E%7B1%7D%20%5C%5C%5B2pt%5D%0Ay%20&amp;=%200.99%20%5Ctimes%2010%5E%7B1%7D%20%5Cqquad%20(9.93%20=%200.993%20%5Ctimes%2010%5E%7B1%7D,%20%5Ctext%7B%20the%20last%20digit%20is%20shifted%20off%7D)%20%5C%5C%5B2pt%5D%0Ax%20-%20y%20&amp;=%200.02%20%5Ctimes%2010%5E%7B1%7D%20=%202.00%20%5Ctimes%2010%5E%7B-1%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>The correct answer is <img src="https://latex.codecogs.com/png.latex?x%20-%20y%20=%200.17%20=%201.70%20%5Ctimes%2010%5E%7B-1%7D">, so by Eq.(2.1c) and Eq.(2.2) the computed difference <img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y%20=%202.00%20%5Ctimes%2010%5E%7B-1%7D"> is off by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cmathrm%7Baerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20&amp;=%20%7C2.00%20-%201.70%7C%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Cmathrm%7Bulps%7D%20=%2030%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%5B6pt%5D%0A%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20&amp;=%20%5Cfrac%7B0.03%7D%7B0.17%7D%20=%200.18%20=%2035%5C,%5Cvarepsilon%0A%5Cend%7Balign*%7D%0A"></p>
<p>and it is wrong in every digit!</p>
</section>
<section id="theorem-1" class="level3">
<h3 class="anchored" data-anchor-id="theorem-1">3.3 Theorem 1</h3>
<p>How bad can the error be?</p>
<blockquote class="blockquote">
<p><strong>Theorem 1</strong>: Using a floating-point format with parameters <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and <img src="https://latex.codecogs.com/png.latex?p"> and computing differences using <img src="https://latex.codecogs.com/png.latex?p"> digits, the relative error of the result, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)">, can be as large as <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20-%201">.</p>
</blockquote>
<p><strong>Proof</strong>: take <img src="https://latex.codecogs.com/png.latex?x%20=%201.00%20%5Ccdots%200"> and <img src="https://latex.codecogs.com/png.latex?y%20=%200.%5Crho%5Crho%20%5Ccdots%20%5Crho">, where <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%20%5Cbeta%20-%201"> is the largest digit and <img src="https://latex.codecogs.com/png.latex?y"> has <img src="https://latex.codecogs.com/png.latex?p"> digits, all equal to <img src="https://latex.codecogs.com/png.latex?%5Crho"> (i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?y%20=%20%5Crho.%5Crho%20%5Ccdots%20%5Crho%20%5Ctimes%20%5Cbeta%5E%7B-1%7D">). Summing the geometric series:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay%20=%20%5Csum_%7Bk=1%7D%5E%7Bp%7D%20%5Crho%5C,%5Cbeta%5E%7B-k%7D%20=%20(%5Cbeta%20-%201)%5C,%5Cfrac%7B%5Cbeta%5E%7B-1%7D%5Cleft(1%20-%20%5Cbeta%5E%7B-p%7D%5Cright)%7D%7B1%20-%20%5Cbeta%5E%7B-1%7D%7D%20=%201%20-%20%5Cbeta%5E%7B-p%7D%0A%5Cquad%20%5CRightarrow%20%5Cquad%0Ax%20-%20y%20=%20%5Cbeta%5E%7B-p%7D%0A"></p>
<p>To subtract, <img src="https://latex.codecogs.com/png.latex?y"> is aligned to the exponent of <img src="https://latex.codecogs.com/png.latex?x">: <img src="https://latex.codecogs.com/png.latex?0.%5Crho%5Crho%20%5Ccdots%20%5Crho%20%5Ctimes%20%5Cbeta%5E%7B0%7D"> has <img src="https://latex.codecogs.com/png.latex?p%20+%201"> digits (the leading 0 and the <img src="https://latex.codecogs.com/png.latex?p"> digits <img src="https://latex.codecogs.com/png.latex?%5Crho">), but only <img src="https://latex.codecogs.com/png.latex?p"> are kept, so the rightmost digit of <img src="https://latex.codecogs.com/png.latex?y"> gets shifted off:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctilde%7By%7D%20=%200.%5Cunderbrace%7B%5Crho%20%5Ccdots%20%5Crho%7D_%7Bp%20-%201%7D%20=%201%20-%20%5Cbeta%5E%7B-(p-1)%7D%0A%5Cquad%20%5CRightarrow%20%5Cquad%0Ax%20%5Cominus%20y%20=%20x%20-%20%5Ctilde%7By%7D%20=%20%5Cbeta%5E%7B-p+1%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20%5Cquad%0A%5Cmathrm%7Baerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20&amp;=%20%5Cbeta%5E%7B-p+1%7D%20-%20%5Cbeta%5E%7B-p%7D%20=%20%5Cbeta%5E%7B-p%7D(%5Cbeta%20-%201)%20%5C%5C%5B6pt%5D%0A%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20&amp;=%20%5Cfrac%7B%5Cbeta%5E%7B-p%7D(%5Cbeta%20-%201)%7D%7B%5Cbeta%5E%7B-p%7D%7D%20=%20%5Cbeta%20-%201%20%5Cqquad%20%5Cblacksquare%0A%5Cend%7Balign*%7D%0A"></p>
<p>This example is also the worst case, so <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20-%201"> is an upper bound too: without guard digit, only the digits of <img src="https://latex.codecogs.com/png.latex?y"> shifted beyond the last digit of <img src="https://latex.codecogs.com/png.latex?x"> are lost; they are at most <img src="https://latex.codecogs.com/png.latex?(%5Cbeta%20-%201)%5C,%5Cbeta%5E%7B-p%7D"> (one digit, relative to <img src="https://latex.codecogs.com/png.latex?x">’s exponent), while a nonzero exact difference <img src="https://latex.codecogs.com/png.latex?x%20-%20y"> is at least <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5E%7B-p%7D">.</p>
<p>Two instances of the proof:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 33%">
<col style="width: 41%">
</colgroup>
<thead>
<tr class="header">
<th></th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010">, <img src="https://latex.codecogs.com/png.latex?p%20=%203"></th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, <img src="https://latex.codecogs.com/png.latex?p%20=%203"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?x"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.00"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.00_2%20=%201"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?y"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.999"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.111_2%20=%207/8"></td>
</tr>
<tr class="odd">
<td>exact <img src="https://latex.codecogs.com/png.latex?x%20-%20y"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.001"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.001_2%20=%201/8"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?%5Ctilde%7By%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.99"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.11_2%20=%203/4"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.01"></td>
<td><img src="https://latex.codecogs.com/png.latex?0.01_2%20=%201/4"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)"></td>
<td><img src="https://latex.codecogs.com/png.latex?9"></td>
<td><img src="https://latex.codecogs.com/png.latex?1"></td>
</tr>
</tbody>
</table>
<p>When <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, the absolute error <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)"> can be as large as the exact result <img src="https://latex.codecogs.com/png.latex?x%20-%20y"> itself, and when <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010"> it can be nine times larger. To put it another way, for <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202"> we have <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20=%202%5E%7B-p%7D">, so <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20=%201%20=%20(1/%5Cvarepsilon)%5C,%5Cvarepsilon"> means by Eq.(2.5) <img src="https://latex.codecogs.com/png.latex?%5Clog_2(1/%5Cvarepsilon)%20=%20%5Clog_2(2%5Ep)%20=%20p"> contaminated digits: all of the <img src="https://latex.codecogs.com/png.latex?p"> digits of the result are wrong!</p>
</section>
<section id="one-guard-digit" class="level3">
<h3 class="anchored" data-anchor-id="one-guard-digit">3.4 One guard digit</h3>
<p>Suppose one extra digit is added to guard against this situation (a <em>guard digit</em>): the smaller number is truncated to <img src="https://latex.codecogs.com/png.latex?p%20+%201"> digits, then the result of the subtraction is rounded to <img src="https://latex.codecogs.com/png.latex?p"> digits. With a guard digit, the previous example becomes</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%201.010%20%5Ctimes%2010%5E%7B1%7D%20%5C%5C%5B2pt%5D%0Ay%20&amp;=%200.993%20%5Ctimes%2010%5E%7B1%7D%20%5C%5C%5B2pt%5D%0Ax%20-%20y%20&amp;=%200.017%20%5Ctimes%2010%5E%7B1%7D%20=%201.70%20%5Ctimes%2010%5E%7B-1%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>and the answer is exact. With a single guard digit, however, the relative error of the result may still be greater than <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">, as in <img src="https://latex.codecogs.com/png.latex?110%20-%208.59">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ax%20&amp;=%201.10%20%5Ctimes%2010%5E%7B2%7D%20%5C%5C%5B2pt%5D%0Ay%20&amp;=%200.085%20%5Ctimes%2010%5E%7B2%7D%20%5Cqquad%20(8.59%20=%200.0859%20%5Ctimes%2010%5E%7B2%7D,%20%5Ctext%7B%20truncated%20to%20%7D%20p%20+%201%20=%204%20%5Ctext%7B%20digits%7D)%20%5C%5C%5B2pt%5D%0Ax%20-%20y%20&amp;=%201.015%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Crightarrow%20%5C;%201.02%20%5Ctimes%2010%5E%7B2%7D%20=%20102%0A%5Cend%7Balign*%7D%0A"></p>
<p>(<img src="https://latex.codecogs.com/png.latex?1.015"> is a halfway case; both rounding rules of Section 5.2 give <img src="https://latex.codecogs.com/png.latex?1.02">). Compared with the correct answer <img src="https://latex.codecogs.com/png.latex?x%20-%20y%20=%20110%20-%208.59%20=%20101.41">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20=%20%5Cfrac%7B102%20-%20101.41%7D%7B101.41%7D%20=%20%5Cfrac%7B0.59%7D%7B101.41%7D%20=%205.8%20%5Ctimes%2010%5E%7B-3%7D%20%5Capprox%200.006%20=%201.16%5C,%5Cvarepsilon%20%5C;%20%3E%20%5C;%20%5Cvarepsilon%20=%200.005%0A"></p>
<p>The exactly rounded result would have been <img src="https://latex.codecogs.com/png.latex?101">, with <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(101.41,%20101)%20=%200.41"> ulps only: the truncated digit of <img src="https://latex.codecogs.com/png.latex?8.59"> is the one that would have decided the rounding.</p>
</section>
<section id="theorem-2" class="level3">
<h3 class="anchored" data-anchor-id="theorem-2">3.5 Theorem 2</h3>
<p>In general, the relative error of the result can be only slightly larger than <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">:</p>
<blockquote class="blockquote">
<p><strong>Theorem 2</strong>: If <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> are floating-point numbers in a format with parameters <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and <img src="https://latex.codecogs.com/png.latex?p">, and if subtraction is done with <img src="https://latex.codecogs.com/png.latex?p%20+%201"> digits (i.e.&nbsp;one guard digit), then the relative rounding error in the result is less than <img src="https://latex.codecogs.com/png.latex?2%5Cvarepsilon">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20%3C%202%5Cvarepsilon">.</p>
</blockquote>
<p>The theorem is proven in Section 4.1 of [1]. Addition is included in it, since <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> can be positive or negative. In the example above, <img src="https://latex.codecogs.com/png.latex?1.16%5C,%5Cvarepsilon%20%3C%202%5Cvarepsilon">.</p>
</section>
</section>
<section id="cancellation" class="level2">
<h2 class="anchored" data-anchor-id="cancellation">4. Cancellation</h2>
<p>Section 3 can be summarized by saying that without a guard digit, the relative error committed when subtracting two nearby quantities can be very large. In other words, the evaluation of any expression containing a subtraction (or an addition of quantities with opposite signs) could result in a relative error so large that <em>all</em> the digits are meaningless (Theorem 1). When subtracting nearby quantities, the most significant digits of the operands match and cancel each other.</p>
<section id="catastrophic-and-benign-cancellation" class="level3">
<h3 class="anchored" data-anchor-id="catastrophic-and-benign-cancellation">4.1 Catastrophic and benign cancellation</h3>
<p>There are two kinds of cancellation.</p>
<p><strong>Catastrophic cancellation</strong> occurs when the operands are subject to rounding errors. For example, the quadratic formula contains the expression <img src="https://latex.codecogs.com/png.latex?b%5E2%20-%204ac">. The quantities <img src="https://latex.codecogs.com/png.latex?b%5E2"> and <img src="https://latex.codecogs.com/png.latex?4ac"> are subject to rounding errors, since they are the results of floating-point multiplications: even if they are rounded to the nearest floating-point number, and so accurate to within half an ulp, their subtraction can make many of the accurate digits disappear, leaving behind mainly digits contaminated by rounding error. Example with <img src="https://latex.codecogs.com/png.latex?b%20=%203.34">, <img src="https://latex.codecogs.com/png.latex?a%20=%201.22">, <img src="https://latex.codecogs.com/png.latex?c%20=%202.28">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctext%7Bexact:%7D%20&amp;%5Cquad%20b%5E2%20=%2011.1556%20%5C,%20,%20%5Cquad%204ac%20=%204%20%5Ctimes%202.7816%20=%2011.1264%20%5C,%20,%20%5Cquad%20b%5E2%20-%204ac%20=%200.0292%20%5C%5C%5B8pt%5D%0A%5Ctext%7Bfloating%20point:%7D%20&amp;%5Cquad%20b%20%5Cotimes%20b%20=%2011.1556%20%5C;%20%5Crightarrow%20%5C;%2011.2%20%5C%5C%5B4pt%5D%0A&amp;%5Cquad%20a%20%5Cotimes%20c%20=%202.7816%20%5C;%20%5Crightarrow%20%5C;%202.78%20%5C,%20,%20%5Cquad%204%20%5Cotimes%202.78%20=%2011.12%20%5C;%20%5Crightarrow%20%5C;%2011.1%20%5C%5C%5B4pt%5D%0A&amp;%5Cquad%20(b%20%5Cotimes%20b)%20%5Cominus%20(4%20%5Cotimes%20a%20%5Cotimes%20c)%20=%2011.2%20-%2011.1%20=%200.100%0A%5Cend%7Balign*%7D%0A"></p>
<p>(<img src="https://latex.codecogs.com/png.latex?(4%20%5Cotimes%20a)%20%5Cotimes%20c%20=%204.88%20%5Cotimes%202.28%20=%2011.1264%20%5Crightarrow%2011.1"> gives the same). Calling <img src="https://latex.codecogs.com/png.latex?%5CDelta%20:=%20b%5E2%20-%204ac"> the exact value and <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5CDelta%7D%20:=%20(b%20%5Cotimes%20b)%20%5Cominus%20(4%20%5Cotimes%20a%20%5Cotimes%20c)%20=%201.00%20%5Ctimes%2010%5E%7B-1%7D"> the computed one, by Eq.(2.1c) and Eq.(2.2):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cmathrm%7Baerr%7D(%5CDelta,%20%5Ctilde%7B%5CDelta%7D)%20&amp;=%20%7C1.00%20-%200.292%7C%20%5Ctimes%2010%5E%7B2%7D%20%5C;%20%5Cmathrm%7Bulps%7D%20=%2070.8%20%5Capprox%2070%20%5C;%20%5Cmathrm%7Bulps%7D%20%5C%5C%5B6pt%5D%0A%5Cmathrm%7Brerr%7D(%5CDelta,%20%5Ctilde%7B%5CDelta%7D)%20&amp;=%20%5Cfrac%7B0.0708%7D%7B0.0292%7D%20=%202.4%20%5Capprox%20485%5C,%5Cvarepsilon%0A%5Cend%7Balign*%7D%0A"></p>
<p>The subtraction itself did not introduce any error, <img src="https://latex.codecogs.com/png.latex?11.2%20-%2011.1"> is exactly <img src="https://latex.codecogs.com/png.latex?0.1">: it rather exposed the errors introduced in the earlier multiplications,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cunderbrace%7B%5Ctilde%7B%5CDelta%7D%20-%20%5CDelta%7D_%7B0.1%20%5C,%20-%20%5C,%200.0292%7D%20=%200.0708%20=%20%5Cunderbrace%7B(11.2%20-%2011.1556)%7D_%7Bb%20%5Cotimes%20b%20%5C,%20-%20%5C,%20b%5E2%7D%20-%20%5Cunderbrace%7B(11.1%20-%2011.1264)%7D_%7B4%20%5Cotimes%20a%20%5Cotimes%20c%20%5C,%20-%20%5C,%204ac%7D%20=%200.0444%20+%200.0264%0A"></p>
<p>Both products are within half an ulp (<img src="https://latex.codecogs.com/png.latex?0.05">) of the exact values, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(b%5E2,%20b%20%5Cotimes%20b)%20=%200.0444"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(4ac,%204%20%5Cotimes%20a%20%5Cotimes%20c)%20=%200.0264">, i.e.&nbsp;their relative errors are below <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">; but these absolute errors are not small compared with the small difference <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%200.0292">.</p>
<p><strong>Benign cancellation</strong> occurs when subtracting exactly known quantities. If <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> have no rounding error, then by Theorem 2, if the subtraction is done with a guard digit, the difference <img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y"> has a very small relative error, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)%20%3C%202%5Cvarepsilon">.</p>
<p>A formula that exhibits catastrophic cancellation can sometimes be rearranged to eliminate the problem. The next sections show four examples.</p>
</section>
<section id="the-quadratic-formula" class="level3">
<h3 class="anchored" data-anchor-id="the-quadratic-formula">4.2 The quadratic formula</h3>
<p>The roots of <img src="https://latex.codecogs.com/png.latex?ax%5E2%20+%20bx%20+%20c%20=%200"> are</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_1%20=%20%5Cfrac%7B-b%20+%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%7B2a%7D%20%5C,%20,%20%5Cqquad%20r_2%20=%20%5Cfrac%7B-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%7B2a%7D%20%5Ctag%7B4.1%7D%0A"></p>
<p>When <img src="https://latex.codecogs.com/png.latex?b%5E2%20%5Cgg%20ac">, <img src="https://latex.codecogs.com/png.latex?b%5E2%20-%204ac"> does not involve a cancellation and <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bb%5E2%20-%204ac%7D%20%5Capprox%20%5Clvert%20b%20%5Crvert">. But then the other addition (subtraction) in one of the two formulas, <img src="https://latex.codecogs.com/png.latex?-b%20%5Cpm%20%5Csqrt%7Bb%5E2%20-%204ac%7D">, subtracts two nearly equal numbers, and the square root carries a rounding error: a catastrophic cancellation. To avoid it, we multiply the numerator and denominator of <img src="https://latex.codecogs.com/png.latex?r_1"> by <img src="https://latex.codecogs.com/png.latex?-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ar_1%20&amp;=%20%5Cfrac%7B-b%20+%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%7B2a%7D%20%5Ccdot%20%5Cfrac%7B-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%7B-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%0A%20%20%20%20%20=%20%5Cfrac%7Bb%5E2%20-%20%5Cleft(b%5E2%20-%204ac%5Cright)%7D%7B2a%5Cleft(-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%5Cright)%7D%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20%5Cfrac%7B4ac%7D%7B2a%5Cleft(-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%5Cright)%7D%20=%20%5Cfrac%7B2c%7D%7B-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>and similarly for <img src="https://latex.codecogs.com/png.latex?r_2">, multiplying by <img src="https://latex.codecogs.com/png.latex?-b%20+%20%5Csqrt%7Bb%5E2%20-%204ac%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_1%20=%20%5Cfrac%7B2c%7D%7B-b%20-%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%20%5C,%20,%20%5Cqquad%20r_2%20=%20%5Cfrac%7B2c%7D%7B-b%20+%20%5Csqrt%7Bb%5E2%20-%204ac%7D%7D%20%5Ctag%7B4.2%7D%0A"></p>
<p>If <img src="https://latex.codecogs.com/png.latex?b%5E2%20%5Cgg%20ac"> and <img src="https://latex.codecogs.com/png.latex?b%20%3E%200">, computing <img src="https://latex.codecogs.com/png.latex?r_1"> with Eq.(4.1) involves a cancellation, so we use Eq.(4.2) for <img src="https://latex.codecogs.com/png.latex?r_1"> and Eq.(4.1) for <img src="https://latex.codecogs.com/png.latex?r_2">. If <img src="https://latex.codecogs.com/png.latex?b%20%3C%200">, we use Eq.(4.1) for <img src="https://latex.codecogs.com/png.latex?r_1"> and Eq.(4.2) for <img src="https://latex.codecogs.com/png.latex?r_2">. In both cases the sum that is actually computed is <img src="https://latex.codecogs.com/png.latex?-b%20-%20%5Cmathrm%7Bsign%7D(b)%5Csqrt%7Bb%5E2%20-%204ac%7D">, whose two terms have the same sign.</p>
<p>Example, not in [1]: <img src="https://latex.codecogs.com/png.latex?a%20=%201.00">, <img src="https://latex.codecogs.com/png.latex?b%20=%20100">, <img src="https://latex.codecogs.com/png.latex?c%20=%201.00">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ab%20%5Cotimes%20b%20&amp;=%201.00%20%5Ctimes%2010%5E%7B4%7D%20%5C,%20,%20%5Cquad%204%20%5Cotimes%20a%20%5Cotimes%20c%20=%204.00%20%5C%5C%5B4pt%5D%0A(b%20%5Cotimes%20b)%20%5Cominus%204.00%20&amp;=%209996%20%5C;%20%5Crightarrow%20%5C;%201.00%20%5Ctimes%2010%5E%7B4%7D%20%5C,%20,%20%5Cquad%20%5Cmathrm%7BSQRT%7D(1.00%20%5Ctimes%2010%5E%7B4%7D)%20=%20100%20%5C%5C%5B8pt%5D%0A%5Cmathrm%7BEq.(4.1)%7D%20%5C,%20:%20%5Cquad%20r_1%20&amp;=%20(-100%20%5Coplus%20100)%20%5Coslash%202.00%20=%200%20%5C,%20,%20%5Cqquad%20r_2%20=%20(-100%20%5Cominus%20100)%20%5Coslash%202.00%20=%20-100%20%5C%5C%5B6pt%5D%0A%5Cmathrm%7BEq.(4.2)%7D%20%5C,%20:%20%5Cquad%20r_1%20&amp;=%202.00%20%5Coslash%20(-100%20%5Cominus%20100)%20=%202.00%20%5Coslash%20(-200)%20=%20-0.0100%0A%5Cend%7Balign*%7D%0A"></p>
<p>The exact roots are <img src="https://latex.codecogs.com/png.latex?r_1%20=%20-0.0100010002%5Cldots"> and <img src="https://latex.codecogs.com/png.latex?r_2%20=%20-99.9899989998%5Cldots">. Eq.(4.1) gives a meaningless <img src="https://latex.codecogs.com/png.latex?r_1%20=%200">, (<img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(r_1,%200)%20=%201">), while <img src="https://latex.codecogs.com/png.latex?r_1"> from Eq.(4.2) and <img src="https://latex.codecogs.com/png.latex?r_2"> from Eq.(4.1) are both correct to all 3 digits: <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(r_1,%20-0.0100)%20=%20%5Cmathrm%7Brerr%7D(r_2,%20-100)%20=%2010%5E%7B-4%7D%20=%200.02%5C,%5Cvarepsilon">.</p>
</section>
<section id="difference-of-squares" class="level3">
<h3 class="anchored" data-anchor-id="difference-of-squares">4.3 Difference of squares</h3>
<p>The expression <img src="https://latex.codecogs.com/png.latex?x%5E2%20-%20y%5E2"> is another formula that exhibits catastrophic cancellation. It is more accurate to evaluate it as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax%5E2%20-%20y%5E2%20=%20(x%20-%20y)(x%20+%20y)%20%5Ctag%7B4.3%7D%0A"></p>
<p>This form still has a subtraction, but it is a benign cancellation of quantities without rounding error. Example, not in [1], with <img src="https://latex.codecogs.com/png.latex?x%20=%203.34"> and <img src="https://latex.codecogs.com/png.latex?y%20=%203.33"> (exact <img src="https://latex.codecogs.com/png.latex?x%5E2%20-%20y%5E2%20=%200.0667">):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(x%20%5Cotimes%20x)%20%5Cominus%20(y%20%5Cotimes%20y)%20&amp;=%20(11.1556%20%5Crightarrow%2011.2)%20%5Cominus%20(11.0889%20%5Crightarrow%2011.1)%20=%200.100%20%5C%5C%5B6pt%5D%0A(x%20%5Cominus%20y)%20%5Cotimes%20(x%20%5Coplus%20y)%20&amp;=%200.01%20%5Cotimes%206.67%20=%200.0667%0A%5Cend%7Balign*%7D%0A"></p>
<p>The first result has <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D%20=%20%5Clvert%201.00%20-%200.667%20%5Crvert%20%5Ctimes%2010%5E%7B2%7D%20=%2033.3"> ulps and <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D%20=%200.0333/0.0667%20=%200.50">; the second one is exact.</p>
<p>By Theorem 2, the relative error of both <img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y"> and <img src="https://latex.codecogs.com/png.latex?x%20%5Coplus%20y"> is at most <img src="https://latex.codecogs.com/png.latex?2%5Cvarepsilon">, and multiplying two quantities with a small relative error results in a product with a small relative error (Section 4.1 of [1]): with <img src="https://latex.codecogs.com/png.latex?%5Cdelta_1,%20%5Cdelta_2"> the (signed) relative errors of the subtraction and of the addition, and <img src="https://latex.codecogs.com/png.latex?%5Cdelta_3"> the one of the multiplication, so that <img src="https://latex.codecogs.com/png.latex?%5Clvert%20%5Cdelta_1%20%5Crvert%20=%20%5Cmathrm%7Brerr%7D(x%20-%20y,%5C,%20x%20%5Cominus%20y)">, <img src="https://latex.codecogs.com/png.latex?%5Clvert%20%5Cdelta_2%20%5Crvert%20=%20%5Cmathrm%7Brerr%7D(x%20+%20y,%5C,%20x%20%5Coplus%20y)">, and <img src="https://latex.codecogs.com/png.latex?%5Clvert%20%5Cdelta_3%20%5Crvert%20%5Cle%20%5Cvarepsilon"> by Eq.(2.4b),</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(x%20%5Cominus%20y)%20%5Cotimes%20(x%20%5Coplus%20y)%20&amp;=%20(x%20-%20y)(1%20+%20%5Cdelta_1)%5C,(x%20+%20y)(1%20+%20%5Cdelta_2)%5C,(1%20+%20%5Cdelta_3)%20%5C,%20,%20%5Cqquad%20%7C%5Cdelta_1%7C,%20%7C%5Cdelta_2%7C%20%5Cle%202%5Cvarepsilon%20%5C,%20,%20%5C;%20%7C%5Cdelta_3%7C%20%5Cle%20%5Cvarepsilon%20%5C%5C%5B6pt%5D%0A&amp;=%20(x%5E2%20-%20y%5E2)%5Cleft(1%20+%20%5Cdelta_1%20+%20%5Cdelta_2%20+%20%5Cdelta_3%20+%20O(%5Cvarepsilon%5E2)%5Cright)%20%5C,%20,%20%5Cqquad%20%7C%5Cdelta_1%20+%20%5Cdelta_2%20+%20%5Cdelta_3%7C%20%5Cle%205%5Cvarepsilon%0A%5Cend%7Balign*%7D%0A"></p>
<p>Although <img src="https://latex.codecogs.com/png.latex?(x%20-%20y)(x%20+%20y)"> does not cause a catastrophic cancellation, it is slightly less accurate than <img src="https://latex.codecogs.com/png.latex?x%5E2%20-%20y%5E2"> if <img src="https://latex.codecogs.com/png.latex?x%20%5Cgg%20y"> or <img src="https://latex.codecogs.com/png.latex?x%20%5Cll%20y">: in this case <img src="https://latex.codecogs.com/png.latex?(x%20-%20y)(x%20+%20y)"> has three rounding errors, but <img src="https://latex.codecogs.com/png.latex?x%5E2%20-%20y%5E2"> has only two, since the rounding error committed when computing the smaller of <img src="https://latex.codecogs.com/png.latex?x%5E2"> and <img src="https://latex.codecogs.com/png.latex?y%5E2"> does not affect the final subtraction (footnote 5 of [1]).</p>
<p><strong>Inexact data</strong>: the floating-point numbers <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> might themselves be approximations of some true quantities <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D">, e.g.&nbsp;exactly known decimal numbers that cannot be expressed exactly in binary. Then, even though <img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y"> is a good approximation of <img src="https://latex.codecogs.com/png.latex?x%20-%20y">, it can have a huge relative error compared with the true <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D%20-%20%5Chat%7By%7D">. For example, if <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D%20=%203.344"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D%20=%203.326"> are stored as <img src="https://latex.codecogs.com/png.latex?x%20=%203.34"> and <img src="https://latex.codecogs.com/png.latex?y%20=%203.33">, then <img src="https://latex.codecogs.com/png.latex?x%20%5Cominus%20y%20=%200.01"> is exact, but <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D%20-%20%5Chat%7By%7D%20=%200.018">, so <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(%5Chat%7Bx%7D%20-%20%5Chat%7By%7D,%5C,%20x%20%5Cominus%20y)%20=%200.008/0.018%20=%200.44">. So the advantage of <img src="https://latex.codecogs.com/png.latex?(x%20+%20y)(x%20-%20y)"> over <img src="https://latex.codecogs.com/png.latex?x%5E2%20-%20y%5E2"> is not as dramatic with inexact data; since computing it is about the same amount of work, it is still clearly the preferred form. In general, replacing a catastrophic cancellation by a benign one is not worthwhile if the expense is large, because the input is often (not always) an approximation. But eliminating a cancellation entirely, as in the quadratic formula, is worthwhile even if the data are not exact.</p>
<p><strong>Assumption</strong>: from now on, as in [1], the floating-point inputs to an algorithm are exact, and the results are computed as accurately as possible.</p>
</section>
<section id="area-of-a-triangle" class="level3">
<h3 class="anchored" data-anchor-id="area-of-a-triangle">4.4 Area of a triangle</h3>
<p>The area of a triangle can be expressed directly in terms of the lengths of its sides <img src="https://latex.codecogs.com/png.latex?a">, <img src="https://latex.codecogs.com/png.latex?b"> and <img src="https://latex.codecogs.com/png.latex?c"> with Heron’s formula:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AA%20=%20%5Csqrt%7Bs%5C,(s%20-%20a)(s%20-%20b)(s%20-%20c)%7D%20%5C,%20,%20%5Cqquad%20s%20=%20%5Cfrac%7Ba%20+%20b%20+%20c%7D%7B2%7D%20%5Ctag%7B4.4%7D%0A"></p>
<p>Suppose the triangle is very flat, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?a%20%5Capprox%20b%20+%20c">. Then <img src="https://latex.codecogs.com/png.latex?s%20%5Capprox%20a">, and the term <img src="https://latex.codecogs.com/png.latex?s%20-%20a"> subtracts two nearby numbers, one of which (<img src="https://latex.codecogs.com/png.latex?s">) may have a rounding error. Example with <img src="https://latex.codecogs.com/png.latex?a%20=%209.0">, <img src="https://latex.codecogs.com/png.latex?b%20=%20c%20=%204.53">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctext%7Bexact:%7D%20&amp;%5Cquad%20s%20=%20%5Cfrac%7B9.0%20+%204.53%20+%204.53%7D%7B2%7D%20=%20%5Cfrac%7B18.06%7D%7B2%7D%20=%209.03%20%5C,%20,%20%5Cquad%20s%20-%20a%20=%200.03%20%5C,%20,%20%5Cquad%20s%20-%20b%20=%20s%20-%20c%20=%204.50%20%5C%5C%5B6pt%5D%0A&amp;%5Cquad%20A%20=%20%5Csqrt%7B9.03%20%5Ctimes%200.03%20%5Ctimes%204.50%20%5Ctimes%204.50%7D%20=%20%5Csqrt%7B5.485725%7D%20=%202.34216%20%5Cldots%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctext%7Bfloating%20point:%7D%20%5Cquad%20b%20%5Coplus%20c%20&amp;=%209.06%20%5C%5C%5B2pt%5D%0Aa%20%5Coplus%20(b%20%5Coplus%20c)%20&amp;=%2018.06%20%5C;%20%5Crightarrow%20%5C;%2018.1%20%5C%5C%5B2pt%5D%0As%20=%2018.1%20%5Coslash%202%20&amp;=%209.05%20%5Cqquad%20(2%20%5C;%20%5Cmathrm%7Bulps%20%5C;%20off%7D)%20%5C%5C%5B2pt%5D%0As%20%5Cominus%20a%20&amp;=%200.05%20%5Cqquad%20(%5Ctext%7Bexact,%20but%20%7D%200.03%20%5Ctext%7B%20was%20the%20correct%20value%7D)%20%5C%5C%5B2pt%5D%0As%20%5Cominus%20b%20=%20s%20%5Cominus%20c%20&amp;=%204.52%20%5C%5C%5B2pt%5D%0As%20%5Cotimes%20(s%20%5Cominus%20a)%20&amp;=%200.4525%20%5C;%20%5Crightarrow%20%5C;%200.452%20%5C%5C%5B2pt%5D%0A0.452%20%5Cotimes%20(s%20%5Cominus%20b)%20&amp;=%202.04304%20%5C;%20%5Crightarrow%20%5C;%202.04%20%5C%5C%5B2pt%5D%0A2.04%20%5Cotimes%20(s%20%5Cominus%20c)%20&amp;=%209.2208%20%5C;%20%5Crightarrow%20%5C;%209.22%20%5C%5C%5B2pt%5D%0A%5Cmathrm%7BSQRT%7D(9.22)%20&amp;=%203.0364%20%5Cldots%20%5C;%20%5Crightarrow%20%5C;%203.04%0A%5Cend%7Balign*%7D%0A"></p>
<p>Even though the computed value <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bs%7D%20=%209.05"> is in error by only <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(s,%20%5Ctilde%7Bs%7D)%20=%202"> ulps, the computed area <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BA%7D%20=%203.04"> is in error by <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(A,%20%5Ctilde%7BA%7D)%20=%20%5Clvert%203.04%20-%202.34216%20%5Crvert%20%5Ctimes%2010%5E2%20=%2069.8%20%5Capprox%2070"> ulps, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(A,%20%5Ctilde%7BA%7D)%20=%200.30%20=%2060%5C,%5Cvarepsilon">. (Rounding the halfway case <img src="https://latex.codecogs.com/png.latex?0.4525"> up to <img src="https://latex.codecogs.com/png.latex?0.453"> instead gives <img src="https://latex.codecogs.com/png.latex?2.05">, <img src="https://latex.codecogs.com/png.latex?9.27">, and again <img src="https://latex.codecogs.com/png.latex?3.04">.)</p>
<p><strong>Misprint in [1]</strong>: the ACM version states “an error of 60 ulps”; the error is <img src="https://latex.codecogs.com/png.latex?70"> ulps.</p>
<p>The order of the additions in <img src="https://latex.codecogs.com/png.latex?s"> matters too: <img src="https://latex.codecogs.com/png.latex?(a%20%5Coplus%20b)%20%5Coplus%20c%20=%20(13.53%20%5Crightarrow%2013.5)%20%5Coplus%204.53%20=%2018.03%20%5Crightarrow%2018.0"> gives <img src="https://latex.codecogs.com/png.latex?s%20=%209.00">, <img src="https://latex.codecogs.com/png.latex?s%20%5Cominus%20a%20=%200">, and <img src="https://latex.codecogs.com/png.latex?A%20=%200">.</p>
<p>There is a way to rewrite formula (4.4) so that it returns accurate results even for flat triangles (Kahan, 1986):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7BA%20=%20%5Cfrac%7B1%7D%7B4%7D%5Csqrt%7B%5Cbig(a%20+%20(b%20+%20c)%5Cbig)%5Cbig(c%20-%20(a%20-%20b)%5Cbig)%5Cbig(c%20+%20(a%20-%20b)%5Cbig)%5Cbig(a%20+%20(b%20-%20c)%5Cbig)%7D%20%5C,%20,%20%5Cqquad%20a%20%5Cge%20b%20%5Cge%20c%7D%20%5Ctag%7B4.5%7D%0A"></p>
<p>If <img src="https://latex.codecogs.com/png.latex?a">, <img src="https://latex.codecogs.com/png.latex?b"> and <img src="https://latex.codecogs.com/png.latex?c"> do not satisfy <img src="https://latex.codecogs.com/png.latex?a%20%5Cge%20b%20%5Cge%20c">, simply rename them before applying Eq.(4.5). Eq.(4.4) and Eq.(4.5) are algebraically identical:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A2s%20&amp;=%20a%20+%20b%20+%20c%20=%20a%20+%20(b%20+%20c)%20%5C%5C%5B2pt%5D%0A2(s%20-%20a)%20&amp;=%20-a%20+%20b%20+%20c%20=%20c%20-%20(a%20-%20b)%20%5C%5C%5B2pt%5D%0A2(s%20-%20b)%20&amp;=%20a%20-%20b%20+%20c%20=%20c%20+%20(a%20-%20b)%20%5C%5C%5B2pt%5D%0A2(s%20-%20c)%20&amp;=%20a%20+%20b%20-%20c%20=%20a%20+%20(b%20-%20c)%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cbig(a%20+%20(b%20+%20c)%5Cbig)%5Cbig(c%20-%20(a%20-%20b)%5Cbig)%5Cbig(c%20+%20(a%20-%20b)%5Cbig)%5Cbig(a%20+%20(b%20-%20c)%5Cbig)%20=%2016%5C,s%5C,(s%20-%20a)(s%20-%20b)(s%20-%20c)%0A"></p>
<p>and the square root of <img src="https://latex.codecogs.com/png.latex?16%20A%5E2">, divided by 4, is <img src="https://latex.codecogs.com/png.latex?A">. The parentheses do not matter in exact arithmetic, but they decide the order of the floating-point operations. With the same values as above:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Aa%20%5Coplus%20(b%20%5Coplus%20c)%20&amp;=%209.0%20%5Coplus%209.06%20=%2018.06%20%5C;%20%5Crightarrow%20%5C;%2018.1%20%5C%5C%5B2pt%5D%0Aa%20%5Cominus%20b%20&amp;=%209.0%20-%204.53%20=%204.47%20%5Cqquad%20(%5Ctext%7Bexact%7D)%20%5C%5C%5B2pt%5D%0Ac%20%5Cominus%20(a%20%5Cominus%20b)%20&amp;=%204.53%20-%204.47%20=%200.06%20%5Cqquad%20(%5Ctext%7Bexact%7D)%20%5C%5C%5B2pt%5D%0Ac%20%5Coplus%20(a%20%5Cominus%20b)%20&amp;=%204.53%20+%204.47%20=%209.00%20%5Cqquad%20(%5Ctext%7Bexact%7D)%20%5C%5C%5B2pt%5D%0Aa%20%5Coplus%20(b%20%5Cominus%20c)%20&amp;=%209.0%20+%200%20=%209.0%20%5C%5C%5B6pt%5D%0A18.1%20%5Cotimes%200.06%20&amp;=%201.086%20%5C;%20%5Crightarrow%20%5C;%201.09%20%5C%5C%5B2pt%5D%0A1.09%20%5Cotimes%209.00%20&amp;=%209.81%20%5C%5C%5B2pt%5D%0A9.81%20%5Cotimes%209.0%20&amp;=%2088.29%20%5C;%20%5Crightarrow%20%5C;%2088.3%20%5C%5C%5B2pt%5D%0A%5Cmathrm%7BSQRT%7D(88.3)%20&amp;=%209.3968%20%5Cldots%20%5C;%20%5Crightarrow%20%5C;%209.40%20%5C%5C%5B2pt%5D%0A9.40%20%5Coslash%204%20&amp;=%202.35%0A%5Cend%7Balign*%7D%0A"></p>
<p>The computed area <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BA%7D%20=%202.35"> is in error by <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(A,%20%5Ctilde%7BA%7D)%20=%20%5Clvert%202.35%20-%202.34216%20%5Crvert%20%5Ctimes%2010%5E2%20=%200.78"> ulps, about 1 ulp, much more accurate than Heron’s formula: <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(A,%20%5Ctilde%7BA%7D)%20=%20(2.35%20-%202.34216)/2.34216%20=%203.35%20%5Ctimes%2010%5E%7B-3%7D%20=%200.67%5C,%5Cvarepsilon%20%5Capprox%200.7%5C,%5Cvarepsilon">. The difference is in the small factor: in Heron’s formula, <img src="https://latex.codecogs.com/png.latex?s%20-%20a"> cancels <img src="https://latex.codecogs.com/png.latex?s">, which already carries a rounding error, while in Kahan’s formula the corresponding factor <img src="https://latex.codecogs.com/png.latex?c%20%5Cominus%20(a%20%5Cominus%20b)"> is computed exactly from exactly known data: both cancellations are benign.</p>
<p>Although formula (4.5) is much more accurate than (4.4) for this example, it would be nice to know how well (4.5) performs in general:</p>
<blockquote class="blockquote">
<p><strong>Theorem 3</strong>: The rounding error incurred when using Eq.(4.5) to compute the area of a triangle is at most <img src="https://latex.codecogs.com/png.latex?11%5Cvarepsilon">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(A,%20%5Ctilde%7BA%7D)%20%5Cle%2011%5Cvarepsilon">, provided subtraction is performed with a guard digit, <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Cle%200.005">, and square roots are computed to within half an ulp.</p>
</blockquote>
<p>The condition <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Cle%200.005"> is met in virtually every actual floating-point system:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cbeta%20=%202%20%5C,%20:%20&amp;%5Cquad%20%5Cvarepsilon%20=%202%5E%7B-p%7D%20%5C,%20,%20%5Cqquad%202%5E%7B-7%7D%20=%200.0078%20%5C,%20,%20%5Cquad%202%5E%7B-8%7D%20=%200.0039%20%5Cqquad%20%5CRightarrow%20%5Cquad%20p%20%5Cge%208%20%5C%5C%5B6pt%5D%0A%5Cbeta%20=%2010%20%5C,%20:%20&amp;%5Cquad%20%5Cvarepsilon%20=%205%20%5Ctimes%2010%5E%7B-p%7D%20%5C,%20,%20%5Cquad%205%20%5Ctimes%2010%5E%7B-2%7D%20=%200.05%20%5C,%20,%20%5Cquad%205%20%5Ctimes%2010%5E%7B-3%7D%20=%200.005%20%5Cquad%20%5CRightarrow%20%5Cquad%20p%20%5Cge%203%0A%5Cend%7Balign*%7D%0A"></p>
<p>In statements like Theorem 3, the relative error is that of the expression computed with floating-point arithmetic, i.e.&nbsp;of</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BSQRT%7D%5CBig(%5Cbig(a%20%5Coplus%20(b%20%5Coplus%20c)%5Cbig)%20%5Cotimes%20%5Cbig(c%20%5Cominus%20(a%20%5Cominus%20b)%5Cbig)%20%5Cotimes%20%5Cbig(c%20%5Coplus%20(a%20%5Cominus%20b)%5Cbig)%20%5Cotimes%20%5Cbig(a%20%5Coplus%20(b%20%5Cominus%20c)%5Cbig)%5CBig)%20%5Coslash%204%20%5Ctag%7B4.6%7D%0A"></p>
<p>Because of the cumbersome nature of Eq.(4.6), [1] usually says “the computed value of <img src="https://latex.codecogs.com/png.latex?E">” rather than writing out <img src="https://latex.codecogs.com/png.latex?E"> with circle notation.</p>
<p>Error bounds are usually too pessimistic: in the example above <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(A,%20%5Ctilde%7BA%7D)%20=%200.67%5C,%5Cvarepsilon">, much less than <img src="https://latex.codecogs.com/png.latex?11%5Cvarepsilon">. The main reason for computing error bounds is not to get precise bounds, but rather to verify that the formula does not contain numerical problems.</p>
</section>
<section id="compound-interest-and-ln1-x" class="level3">
<h3 class="anchored" data-anchor-id="compound-interest-and-ln1-x">4.5 Compound interest and <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)"></h3>
<p>A final example of an expression that can be rewritten to use benign cancellation is <img src="https://latex.codecogs.com/png.latex?(1%20+%20x)%5En">, where <img src="https://latex.codecogs.com/png.latex?x%20%5Cll%201">. This expression arises in financial calculations. Consider depositing 100 USD every day into a bank account that earns an annual interest rate of <img src="https://latex.codecogs.com/png.latex?i%20=%206%5C%25">, compounded daily, so that <img src="https://latex.codecogs.com/png.latex?n%20=%20365"> and the daily rate is <img src="https://latex.codecogs.com/png.latex?j%20=%20i/n">. A deposit made <img src="https://latex.codecogs.com/png.latex?k"> days before the end of the year has grown to <img src="https://latex.codecogs.com/png.latex?100%5C,(1%20+%20j)%5Ek">, so the amount of money accumulated at the end of one year (deposits at the end of each day, <img src="https://latex.codecogs.com/png.latex?k%20=%200,%20%5Cldots,%20n%20-%201">) is the geometric sum</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A100%20%5Csum_%7Bk=0%7D%5E%7Bn-1%7D%20(1%20+%20j)%5Ek%20=%20100%5C,%5Cfrac%7B(1%20+%20j)%5En%20-%201%7D%7B(1%20+%20j)%20-%201%7D%20=%20100%5C,%5Cfrac%7B(1%20+%20i/n)%5En%20-%201%7D%7Bi/n%7D%20%5Ctag%7B4.7%7D%0A"></p>
<p>Computed exactly:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Aj%20=%20i/n%20&amp;=%200.06/365%20=%201.643835616%20%5Ctimes%2010%5E%7B-4%7D%20%5C%5C%5B4pt%5D%0A(1%20+%20j)%5En%20&amp;=%201.0618313107%20%5C%5C%5B4pt%5D%0A100%5C,%5Cfrac%7B(1%20+%20j)%5En%20-%201%7D%7Bj%7D%20&amp;=%20100%20%5Ctimes%20%5Cfrac%7B0.0618313107%7D%7B1.643835616%20%5Ctimes%2010%5E%7B-4%7D%7D%20=%20%5C$%5C,37614.05%0A%5Cend%7Balign*%7D%0A"></p>
<p>Computed with <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202"> and <img src="https://latex.codecogs.com/png.latex?p%20=%2024"> (single precision), the result is 37615.45 USD, a discrepancy of 1.40 USD. The reason is easy to see: <img src="https://latex.codecogs.com/png.latex?1%20%5Coplus%20j"> involves adding 1 to <img src="https://latex.codecogs.com/png.latex?0.0001643836">, so the low-order bits of <img src="https://latex.codecogs.com/png.latex?j"> are lost, and this rounding error is amplified when <img src="https://latex.codecogs.com/png.latex?1%20%5Coplus%20j"> is raised to the <img src="https://latex.codecogs.com/png.latex?n">-th power. In detail:</p>
<p><strong>Step 1</strong>: near 1, the floating-point numbers are spaced by <img src="https://latex.codecogs.com/png.latex?2%5E%7B-23%7D%20=%201.19%20%5Ctimes%2010%5E%7B-7%7D">, so</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Bj%7D%7B2%5E%7B-23%7D%7D%20=%201378.95%20%5Cquad%20%5CRightarrow%20%5Cquad%201%20%5Coplus%20j%20=%201%20+%201379%20%5Ctimes%202%5E%7B-23%7D%20=%201.000164389610%0A"></p>
<p>The computation actually uses the daily rate <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bj%7D%20=%20(1%20%5Coplus%20j)%20%5Cominus%201%20=%201379%20%5Ctimes%202%5E%7B-23%7D%20=%201.64389610%20%5Ctimes%2010%5E%7B-4%7D">: since <img src="https://latex.codecogs.com/png.latex?1379%20%3C%202%5E%7B11%7D">, only 11 of the 24 bits of <img src="https://latex.codecogs.com/png.latex?j"> survive, and</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7Brerr%7D(j,%20%5Ctilde%7Bj%7D)%20=%20%5Cfrac%7B%5Ctilde%7Bj%7D%20-%20j%7D%7Bj%7D%20=%20%5Cfrac%7B6.05%20%5Ctimes%2010%5E%7B-9%7D%7D%7B1.64%20%5Ctimes%2010%5E%7B-4%7D%7D%20=%203.7%20%5Ctimes%2010%5E%7B-5%7D%20%5Capprox%20617%5C,%5Cvarepsilon%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20=%20%5Cvarepsilon%5B2,%5C,24%5D%20=%202%5E%7B-24%7D">.</p>
<p><strong>Step 2</strong>: raising to the <img src="https://latex.codecogs.com/png.latex?n">-th power multiplies the relative error by <img src="https://latex.codecogs.com/png.latex?n">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cmathrm%7Brerr%7D%5Cbig((1%20+%20j)%5En,%20(1%20+%20%5Ctilde%7Bj%7D)%5En%5Cbig)%20&amp;=%20%5Cfrac%7B(1%20+%20%5Ctilde%7Bj%7D)%5En%7D%7B(1%20+%20j)%5En%7D%20-%201%20=%20%5Cleft(1%20+%20%5Cfrac%7B%5Ctilde%7Bj%7D%20-%20j%7D%7B1%20+%20j%7D%5Cright)%5En%20-%201%20%5C%5C%5B6pt%5D%0A&amp;%5Capprox%20n%5C,%5Cfrac%7B%5Ctilde%7Bj%7D%20-%20j%7D%7B1%20+%20j%7D%20=%20365%20%5Ctimes%206.05%20%5Ctimes%2010%5E%7B-9%7D%20=%202.21%20%5Ctimes%2010%5E%7B-6%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p><strong>Step 3</strong>: subtracting 1 amplifies the relative error again, by a factor <img src="https://latex.codecogs.com/png.latex?(1%20+%20j)%5En%20/%20%5Cbig((1%20+%20j)%5En%20-%201%5Cbig)%20=%201.0618/0.0618%20=%2017.2">, giving <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D%5Cbig((1%20+%20j)%5En%20-%201,%20(1%20+%20%5Ctilde%7Bj%7D)%5En%20-%201%5Cbig)%20=%202.21%20%5Ctimes%2010%5E%7B-6%7D%20%5Ctimes%2017.2%20=%203.79%20%5Ctimes%2010%5E%7B-5%7D">, i.e.&nbsp;an absolute error on the amount of <img src="https://latex.codecogs.com/png.latex?37614.05%20%5Ctimes%203.79%20%5Ctimes%2010%5E%7B-5%7D%20=%201.43"> USD. Evaluating Eq.(4.7) exactly, but with <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bj%7D"> instead of <img src="https://latex.codecogs.com/png.latex?j"> in the power, gives 37615.47 USD; the remaining single-precision roundings give the 37615.45 USD of [1].</p>
<p>The troublesome expression <img src="https://latex.codecogs.com/png.latex?(1%20+%20i/n)%5En"> can be rewritten as <img src="https://latex.codecogs.com/png.latex?%5Cexp%5Cleft%5Bn%20%5Cln(1%20+%20i/n)%5Cright%5D">, where now the problem is to compute <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)"> for small <img src="https://latex.codecogs.com/png.latex?x">.</p>
<ul>
<li>One approach is to use the approximation <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)%20%5Capprox%20x">, in which case the payment becomes 37617.26 USD, with an absolute error of 3.21 USD: even less accurate than the obvious formula. The approximation drops the <img src="https://latex.codecogs.com/png.latex?-x%5E2/2"> term of <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)%20=%20x%20-%20x%5E2/2%20+%20%5Cdots">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?n%20j%5E2/2%20=%204.9%20%5Ctimes%2010%5E%7B-6%7D"> in the exponent, again amplified by the factor 17.2 of Step 3. (In exact arithmetic, <img src="https://latex.codecogs.com/png.latex?100%5C,(e%5E%7B0.06%7D%20-%201)/j%20=%2037617.23">; single precision gives <img src="https://latex.codecogs.com/png.latex?37617.27">, which [1] reports as 37617.26.)</li>
<li>There is a way to compute <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)"> accurately, as Theorem 4 shows (Hewlett-Packard, 1982). This formula yields 37614.07 USD, accurate to within 2 cents!</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0A%5Cln(1%20+%20x)%20=%20%5Cbegin%7Bcases%7D%0Ax%20&amp;%20%5Ctext%7Bfor%7D%20%5C;%201%20%5Coplus%20x%20=%201%20%5C%5C%5B10pt%5D%0A%5Cdfrac%7Bx%20%5Cln(1%20+%20x)%7D%7B(1%20+%20x)%20-%201%7D%20&amp;%20%5Ctext%7Bfor%7D%20%5C;%201%20%5Coplus%20x%20%5Cneq%201%0A%5Cend%7Bcases%7D%7D%20%5Ctag%7B4.8%7D%0A"></p>
<blockquote class="blockquote">
<p><strong>Theorem 4</strong>: If <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)"> is computed using formula (4.8), the relative error is at most <img src="https://latex.codecogs.com/png.latex?5%5Cvarepsilon">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D%5Cbig(%5Cln(1%20+%20x),%5C,%20x%20%5Cotimes%20%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)%20%5Coslash%20((1%20%5Coplus%20x)%20%5Cominus%201)%5Cbig)%20%5Cle%205%5Cvarepsilon">, when <img src="https://latex.codecogs.com/png.latex?0%20%5Cle%20x%20%3C%203/4">, provided subtraction is performed with a guard digit, <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%3C%200.1">, and <img src="https://latex.codecogs.com/png.latex?%5Cln"> is computed to within half an ulp.</p>
</blockquote>
<p><strong>Misprint in [1]</strong>: the ACM version states “If <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20-%20x)"> is computed using the formula”; it is <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)">.</p>
<p>In exact arithmetic the second line of Eq.(4.8) is just <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)">; what matters is that it is computed as <img src="https://latex.codecogs.com/png.latex?x%20%5Cotimes%20%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)%20%5Coslash%20%5Cbig((1%20%5Coplus%20x)%20%5Cominus%201%5Cbig)">. Theorem 4 assumes that <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(x)"> approximates <img src="https://latex.codecogs.com/png.latex?%5Cln(x)"> to within half an ulp. The problem it solves is that when <img src="https://latex.codecogs.com/png.latex?x"> is small, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)"> is not close to <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)">, because <img src="https://latex.codecogs.com/png.latex?1%20%5Coplus%20x"> has lost the information in the low-order bits of <img src="https://latex.codecogs.com/png.latex?x">. That is, the computed value of <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)"> is not close to its actual value when <img src="https://latex.codecogs.com/png.latex?x%20%5Cll%201">.</p>
<p>The formula works for any value of <img src="https://latex.codecogs.com/png.latex?x">, but it is only interesting for <img src="https://latex.codecogs.com/png.latex?x%20%5Cll%201">, which is where catastrophic cancellation occurs in the naive formula <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)">. Although it may seem mysterious, there is a simple explanation for why it works. Write</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cln(1%20+%20x)%20=%20x%5C,%5Cfrac%7B%5Cln(1%20+%20x)%7D%7Bx%7D%20=%20x%5C,%5Cmu(x)%20%5C,%20,%20%5Cqquad%20%5Cmu(x)%20:=%20%5Cfrac%7B%5Cln(1%20+%20x)%7D%7Bx%7D%20=%201%20-%20%5Cfrac%7Bx%7D%7B2%7D%20+%20%5Cfrac%7Bx%5E2%7D%7B3%7D%20-%20%5Cdots%20%5Ctag%7B4.9%7D%0A"></p>
<ul>
<li>The left factor <img src="https://latex.codecogs.com/png.latex?x"> can be computed exactly, but the right factor <img src="https://latex.codecogs.com/png.latex?%5Cmu(x)"> will suffer a large rounding error when adding 1 to <img src="https://latex.codecogs.com/png.latex?x">.</li>
<li>However, <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is almost constant, since <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)%20%5Capprox%20x">: by Eq.(4.9), <img src="https://latex.codecogs.com/png.latex?%5Cmu(%5Ctilde%7Bx%7D)%20-%20%5Cmu(x)%20%5Capprox%20-(%5Ctilde%7Bx%7D%20-%20x)/2">. So changing <img src="https://latex.codecogs.com/png.latex?x"> slightly will not introduce much error: if <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20%5Capprox%20x">, computing <img src="https://latex.codecogs.com/png.latex?x%5C,%5Cmu(%5Ctilde%7Bx%7D)"> is a good approximation of <img src="https://latex.codecogs.com/png.latex?x%5C,%5Cmu(x)%20=%20%5Cln(1%20+%20x)">.</li>
<li>Is there a value <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"> for which <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20+%201"> can be computed accurately? There is: <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20=%20(1%20%5Coplus%20x)%20%5Cominus%201">, because then <img src="https://latex.codecogs.com/png.latex?1%20+%20%5Ctilde%7Bx%7D"> is exactly equal to <img src="https://latex.codecogs.com/png.latex?1%20%5Coplus%20x"> (and the subtraction is a benign cancellation, exact with a guard digit). So <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)%20%5Coslash%20%5Ctilde%7Bx%7D"> is an accurate value of <img src="https://latex.codecogs.com/png.latex?%5Cmu(%5Ctilde%7Bx%7D)">.</li>
</ul>
<p>In the compound interest example (single precision):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 58%">
<col style="width: 21%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="header">
<th>quantity</th>
<th>value</th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D"> vs exact</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20j)">, exact</td>
<td><img src="https://latex.codecogs.com/png.latex?1.643700521%20%5Ctimes%2010%5E%7B-4%7D"></td>
<td></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(1%20%5Coplus%20j)"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.643761060%20%5Ctimes%2010%5E%7B-4%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?3.7%20%5Ctimes%2010%5E%7B-5%7D"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?%5Cmu(j)">, exact</td>
<td><img src="https://latex.codecogs.com/png.latex?0.9999178172"></td>
<td></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?%5Cmu(%5Ctilde%7Bj%7D)">, exact</td>
<td><img src="https://latex.codecogs.com/png.latex?0.9999178142"></td>
<td><img src="https://latex.codecogs.com/png.latex?3.0%20%5Ctimes%2010%5E%7B-9%7D"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?j%20%5Cotimes%20%5Cmathrm%7BLN%7D(1%20%5Coplus%20j)%20%5Coslash%20%5Cbig((1%20%5Coplus%20j)%20%5Cominus%201%5Cbig)"></td>
<td><img src="https://latex.codecogs.com/png.latex?1.643700525%20%5Ctimes%2010%5E%7B-4%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?2.2%20%5Ctimes%2010%5E%7B-9%7D%20=%200.04%5C,%5Cvarepsilon"></td>
</tr>
</tbody>
</table>
<p>The <img src="https://latex.codecogs.com/png.latex?3.7%20%5Ctimes%2010%5E%7B-5%7D"> error of <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bj%7D"> that ruins <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BLN%7D(1%20%5Coplus%20j)"> changes <img src="https://latex.codecogs.com/png.latex?%5Cmu"> by only <img src="https://latex.codecogs.com/png.latex?3.0%20%5Ctimes%2010%5E%7B-9%7D">.</p>
<p>The same mechanism with <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010">, <img src="https://latex.codecogs.com/png.latex?p%20=%203">, and <img src="https://latex.codecogs.com/png.latex?x%20=%200.0123"> (not in [1]):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A1%20%5Coplus%20x%20&amp;=%201.0123%20%5C;%20%5Crightarrow%20%5C;%201.01%20%5C%5C%5B6pt%5D%0A%5Ctext%7Bexact:%7D%20%5Cquad%20%5Cln(1%20+%20x)%20&amp;=%20%5Cln(1.0123)%20=%200.0122250%20%5Cldots%20%5C%5C%5B6pt%5D%0A%5Ctext%7Bnaive:%7D%20%5Cquad%20%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)%20&amp;=%20%5Cmathrm%7BLN%7D(1.01)%20=%200.00995033%20%5Cldots%20%5C;%20%5Crightarrow%20%5C;%200.00995%20%5C%5C%5B6pt%5D%0A%5Ctext%7BTheorem%204:%7D%20%5Cquad%20%5Ctilde%7Bx%7D%20&amp;=%20(1%20%5Coplus%20x)%20%5Cominus%201%20=%200.01%20%5C%5C%5B2pt%5D%0Ax%20%5Cotimes%20%5Cmathrm%7BLN%7D(1%20%5Coplus%20x)%20&amp;=%200.0123%20%5Ctimes%200.00995%20=%200.000122385%20%5C;%20%5Crightarrow%20%5C;%200.000122%20%5C%5C%5B2pt%5D%0A0.000122%20%5Coslash%20%5Ctilde%7Bx%7D%20&amp;=%200.0122%0A%5Cend%7Balign*%7D%0A"></p>
<p>The naive result has <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(%5Cln(1%20+%20x),%5C,%200.00995)%20=%200.19%20=%2037%5C,%5Cvarepsilon">, the one of Theorem 4 <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Brerr%7D(%5Cln(1%20+%20x),%5C,%200.0122)%20=%200.002%20=%200.41%5C,%5Cvarepsilon">: <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D%20=%200.01"> differs from <img src="https://latex.codecogs.com/png.latex?x%20=%200.0123"> by 19%, but <img src="https://latex.codecogs.com/png.latex?%5Cmu(0.01)%20=%200.99503"> differs from <img src="https://latex.codecogs.com/png.latex?%5Cmu(0.0123)%20=%200.99390"> by only 0.11%.</p>
</section>
<section id="the-price-of-a-guard-digit" class="level3">
<h3 class="anchored" data-anchor-id="the-price-of-a-guard-digit">4.6 The price of a guard digit</h3>
<p>The results of this section can be summarized by saying that a guard digit guarantees accuracy when nearby precisely known quantities are subtracted (benign cancellation). Sometimes a formula that gives inaccurate results can be rewritten to have much higher numerical accuracy by using benign cancellation; however, the procedure only works if subtraction is performed using a guard digit.</p>
<p>The price of a guard digit is not high, because it merely requires making the adder 1 bit wider: for a 54-bit double precision adder, the additional cost is <img src="https://latex.codecogs.com/png.latex?1/54%20=%201.9%5C%25%20%3C%202%5C%25">. For this price, you gain the ability to run many algorithms, such as Kahan’s formula (4.5) for computing the area of a triangle ([1] says formula (6), but it is the one that requires the guard digit, i.e.&nbsp;their (7)) and the expression of Theorem 4 for computing <img src="https://latex.codecogs.com/png.latex?%5Cln(1%20+%20x)">. Although most modern computers (in 1991) have a guard digit, there are a few, such as Crays, that do not.</p>
</section>
</section>
<section id="exactly-rounded-operations" class="level2">
<h2 class="anchored" data-anchor-id="exactly-rounded-operations">5. Exactly rounded operations</h2>
<section id="definition" class="level3">
<h3 class="anchored" data-anchor-id="definition">5.1 Definition</h3>
<p>When floating-point operations are done with a guard digit, they are not as accurate as if they were computed exactly, then rounded to the nearest floating-point number. Operations performed in this manner are called <em>exactly rounded</em>. The example <img src="https://latex.codecogs.com/png.latex?110%20-%208.59"> of Section 3.4 shows that a single guard digit does not always give exactly rounded results (<img src="https://latex.codecogs.com/png.latex?102"> instead of <img src="https://latex.codecogs.com/png.latex?101">). Section 4 gave several examples of algorithms that require a guard digit in order to work properly; this section gives examples of algorithms that require exact rounding.</p>
</section>
<section id="halfway-cases-round-half-up-or-round-half-to-even" class="level3">
<h3 class="anchored" data-anchor-id="halfway-cases-round-half-up-or-round-half-to-even">5.2 Halfway cases: round half up or round half to even</h3>
<p>Rounding is straightforward, with the exception of how to round halfway cases: should <img src="https://latex.codecogs.com/png.latex?12.5"> round to <img src="https://latex.codecogs.com/png.latex?12"> or <img src="https://latex.codecogs.com/png.latex?13">?</p>
<ul>
<li><em>Round half up</em>: one school of thought divides the 10 digits in half, letting <img src="https://latex.codecogs.com/png.latex?%5Clbrace%200,%201,%202,%203,%204%20%5Crbrace"> round down and <img src="https://latex.codecogs.com/png.latex?%5Clbrace%205,%206,%207,%208,%209%20%5Crbrace"> round up; thus <img src="https://latex.codecogs.com/png.latex?12.5"> rounds to <img src="https://latex.codecogs.com/png.latex?13">. This is how rounding works on Digital Equipment Corporation’s VAX computers.</li>
<li><em>Round half to even</em>: since numbers ending in 5 are halfway between two possible roundings, they should round down half of the time and round up the other half. One way of obtaining this behaviour is to require the rounded result to have its least significant digit even: <img src="https://latex.codecogs.com/png.latex?12.5"> rounds to <img src="https://latex.codecogs.com/png.latex?12">, because 2 is even.</li>
</ul>
<p>Reiser and Knuth (1975) offer the following reason for preferring round to even:</p>
<blockquote class="blockquote">
<p><strong>Theorem 5</strong>: Let <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> be floating-point numbers, and define <img src="https://latex.codecogs.com/png.latex?x_0%20=%20x">, <img src="https://latex.codecogs.com/png.latex?x_1%20=%20(x_0%20%5Cominus%20y)%20%5Coplus%20y">, …, <img src="https://latex.codecogs.com/png.latex?x_n%20=%20(x_%7Bn-1%7D%20%5Cominus%20y)%20%5Coplus%20y">. If <img src="https://latex.codecogs.com/png.latex?%5Coplus"> and <img src="https://latex.codecogs.com/png.latex?%5Cominus"> are exactly rounded using round to even, then either <img src="https://latex.codecogs.com/png.latex?x_n%20=%20x"> for all <img src="https://latex.codecogs.com/png.latex?n">, or <img src="https://latex.codecogs.com/png.latex?x_n%20=%20x_1"> for all <img src="https://latex.codecogs.com/png.latex?n%20%5Cge%201">.</p>
</blockquote>
<p>[1] does not include a proof. To clarify the result, take <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010">, <img src="https://latex.codecogs.com/png.latex?p%20=%203">, <img src="https://latex.codecogs.com/png.latex?x%20=%201.00">, <img src="https://latex.codecogs.com/png.latex?y%20=%20-0.555">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctext%7Bround%20half%20up:%7D%20%5Cquad%20x_0%20%5Cominus%20y%20&amp;=%201.00%20+%200.555%20=%201.555%20%5C;%20%5Crightarrow%20%5C;%201.56%20%5C%5C%5B2pt%5D%0Ax_1%20=%201.56%20%5Coplus%20y%20&amp;=%201.56%20-%200.555%20=%201.005%20%5C;%20%5Crightarrow%20%5C;%201.01%20%5C%5C%5B2pt%5D%0Ax_1%20%5Cominus%20y%20&amp;=%201.01%20+%200.555%20=%201.565%20%5C;%20%5Crightarrow%20%5C;%201.57%20%5C%5C%5B2pt%5D%0Ax_2%20=%201.57%20%5Coplus%20y%20&amp;=%201.57%20-%200.555%20=%201.015%20%5C;%20%5Crightarrow%20%5C;%201.02%20%5C%5C%5B2pt%5D%0A&amp;%5C;%5C;%5Cvdots%20%5C%5C%5B2pt%5D%0Ax_n%20&amp;=%201.00%20+%200.01%5C,n%20%5C%5C%5B10pt%5D%0A%5Ctext%7Bround%20half%20to%20even:%7D%20%5Cquad%20x_0%20%5Cominus%20y%20&amp;=%201.555%20%5C;%20%5Crightarrow%20%5C;%201.56%20%5Cqquad%20(6%20%5Ctext%7B%20is%20even%7D)%20%5C%5C%5B2pt%5D%0Ax_1%20=%201.56%20%5Coplus%20y%20&amp;=%201.005%20%5C;%20%5Crightarrow%20%5C;%201.00%20%5Cqquad%20(0%20%5Ctext%7B%20is%20even%7D)%20%5C%5C%5B2pt%5D%0Ax_n%20&amp;=%201.00%20%5Cquad%20%5Cforall%5C,%20n%0A%5Cend%7Balign*%7D%0A"></p>
<p>All the exact intermediate results have 4 digits ending in 5, i.e.&nbsp;they are halfway cases: round half up rounds all of them up, so each successive value of <img src="https://latex.codecogs.com/png.latex?x_n"> increases by <img src="https://latex.codecogs.com/png.latex?0.01">, while round half to even rounds up once and down once. How far does the drift go? (not in [1]): <img src="https://latex.codecogs.com/png.latex?845"> steps, up to <img src="https://latex.codecogs.com/png.latex?x_%7B845%7D%20=%209.45">. There, <img src="https://latex.codecogs.com/png.latex?9.45%20+%200.555%20=%2010.005%20%5Crightarrow%2010.0"> is no longer a halfway case (the result has exponent 1, and its last digit has weight <img src="https://latex.codecogs.com/png.latex?0.1">), and <img src="https://latex.codecogs.com/png.latex?10.0%20-%200.555%20=%209.445%20%5Crightarrow%209.45%20=%20x_%7B845%7D">.</p>
<p>This example suggests that when using the round up rule, computations can gradually drift upward, whereas when using round to even the theorem says this cannot happen. Throughout the rest of [1], round to even is used.</p>
</section>
<section id="multiple-precision-splitting-a-number" class="level3">
<h3 class="anchored" data-anchor-id="multiple-precision-splitting-a-number">5.3 Multiple precision: splitting a number</h3>
<p>One application of exact rounding occurs in multiple precision arithmetic. There are two basic approaches to higher precision:</p>
<ol type="1">
<li>represent floating-point numbers using a very large significand, stored in an array of words, and code the routines for manipulating these numbers in assembly language;</li>
<li>represent higher precision floating-point numbers as an array of ordinary floating-point numbers, where adding the elements of the array in infinite precision recovers the high precision floating-point number.</li>
</ol>
<p>[1] discusses the second approach: it can be coded portably in a high-level language, but it requires exactly rounded arithmetic.</p>
<p>The key to multiplication in this system is representing a product <img src="https://latex.codecogs.com/png.latex?xy"> as a sum, where each summand has the same precision as <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">. This can be done by splitting <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">: writing <img src="https://latex.codecogs.com/png.latex?x%20=%20x_h%20+%20x_l"> and <img src="https://latex.codecogs.com/png.latex?y%20=%20y_h%20+%20y_l">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Axy%20=%20x_h%5C,y_h%20+%20x_h%5C,y_l%20+%20x_l%5C,y_h%20+%20x_l%5C,y_l%20%5Ctag%7B5.1%7D%0A"></p>
<p>If <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> have <img src="https://latex.codecogs.com/png.latex?p">-bit significands, the summands will also have <img src="https://latex.codecogs.com/png.latex?p">-bit significands, provided <img src="https://latex.codecogs.com/png.latex?x_l">, <img src="https://latex.codecogs.com/png.latex?x_h">, <img src="https://latex.codecogs.com/png.latex?y_h">, <img src="https://latex.codecogs.com/png.latex?y_l"> can be represented using <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20p/2%20%5Crfloor"> bits (the product of two numbers with <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20p/2%20%5Crfloor"> bits has at most <img src="https://latex.codecogs.com/png.latex?2%20%5Clfloor%20p/2%20%5Crfloor%20%5Cle%20p"> bits). When <img src="https://latex.codecogs.com/png.latex?p"> is even, it is easy to find a splitting: the number <img src="https://latex.codecogs.com/png.latex?x_0.x_1%20%5Ccdots%20x_%7Bp-1%7D"> can be written as the sum</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_0.x_1%20%5Ccdots%20x_%7Bp-1%7D%20=%20x_0.x_1%20%5Ccdots%20x_%7Bp/2%20-%201%7D%20+%200.0%20%5Ccdots%200%5C,x_%7Bp/2%7D%20%5Ccdots%20x_%7Bp-1%7D%0A"></p>
<p>When <img src="https://latex.codecogs.com/png.latex?p"> is odd, this simple splitting method will not work, since one of the two halves has <img src="https://latex.codecogs.com/png.latex?%5Clceil%20p/2%20%5Crceil"> digits. An extra bit can, however, be gained by using negative numbers. For example, with <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, <img src="https://latex.codecogs.com/png.latex?p%20=%205"> and <img src="https://latex.codecogs.com/png.latex?x%20=%200.10111_2">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax%20=%200.10111_2%20=%20%5Cfrac%7B23%7D%7B32%7D%20=%20%5Cunderbrace%7B0.11_2%7D_%7Bx_h%20%5C,%20=%20%5C,%2024/32%7D%20%5C;%20%5Cunderbrace%7B-%20%5C;%200.00001_2%7D_%7Bx_l%20%5C,%20=%20%5C,%20-1/32%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?x_h"> has 2 significant bits and <img src="https://latex.codecogs.com/png.latex?x_l"> has 1, both at most <img src="https://latex.codecogs.com/png.latex?%5Clfloor%205/2%20%5Crfloor%20=%202">, while the digit splitting <img src="https://latex.codecogs.com/png.latex?0.10_2%20+%200.00111_2"> would need 3 significant bits for the second part.</p>
<p>There is more than one way to split a number. A splitting method that is easy to compute is due to Dekker (1971), but it requires more than a single guard digit:</p>
<blockquote class="blockquote">
<p><strong>Theorem 6</strong>: Let <img src="https://latex.codecogs.com/png.latex?p"> be the floating-point precision, with the restriction that <img src="https://latex.codecogs.com/png.latex?p"> is even when <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%3E%202">, and assume that floating-point operations are exactly rounded. Then if <img src="https://latex.codecogs.com/png.latex?k%20=%20%5Clceil%20p/2%20%5Crceil"> is half the precision (rounded up) and <img src="https://latex.codecogs.com/png.latex?m%20=%20%5Cbeta%5Ek%20+%201">, <img src="https://latex.codecogs.com/png.latex?x"> can be split as <img src="https://latex.codecogs.com/png.latex?x%20=%20x_h%20+%20x_l">, where <img src="https://latex.codecogs.com/png.latex?x_h"> and <img src="https://latex.codecogs.com/png.latex?x_l"> are given by Eq.(5.2), and each of them is representable using <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20p/2%20%5Crfloor"> bits of precision.</p>
</blockquote>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7Bx_h%20=%20(m%20%5Cotimes%20x)%20%5Cominus%20(m%20%5Cotimes%20x%20%5Cominus%20x)%20%5C,%20,%20%5Cqquad%20x_l%20=%20x%20%5Cominus%20x_h%7D%20%5Ctag%7B5.2%7D%0A"></p>
<p>To see how this theorem works, take <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%2010">, <img src="https://latex.codecogs.com/png.latex?p%20=%204">, <img src="https://latex.codecogs.com/png.latex?b%20=%203.476">, <img src="https://latex.codecogs.com/png.latex?a%20=%203.463">, <img src="https://latex.codecogs.com/png.latex?c%20=%203.479">, and compute <img src="https://latex.codecogs.com/png.latex?b%5E2%20-%20ac">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Ctext%7Bexact:%7D%20&amp;%5Cquad%20b%5E2%20=%2012.082576%20%5C,%20,%20%5Cquad%20ac%20=%2012.047777%20%5C,%20,%20%5Cquad%20b%5E2%20-%20ac%20=%200.034799%20%5C;%20%5Crightarrow%20%5C;%200.03480%20%5C%5C%5B6pt%5D%0A%5Ctext%7Bnaive:%7D%20&amp;%5Cquad%20b%20%5Cotimes%20b%20=%2012.08%20%5C,%20,%20%5Cquad%20a%20%5Cotimes%20c%20=%2012.05%20%5C,%20,%20%5Cquad%20(b%20%5Cotimes%20b)%20%5Cominus%20(a%20%5Cotimes%20c)%20=%200.03%0A%5Cend%7Balign*%7D%0A"></p>
<p>By Eq.(2.1c) with <img src="https://latex.codecogs.com/png.latex?p%20=%204">, the naive result <img src="https://latex.codecogs.com/png.latex?3.000%20%5Ctimes%2010%5E%7B-2%7D"> is in error by <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Baerr%7D(b%5E2%20-%20ac,%5C,%200.03)%20=%20%5Clvert%203.000%20-%203.4799%20%5Crvert%20%5Ctimes%2010%5E3%20=%20479.9%20%5Capprox%20480"> ulps. Now split <img src="https://latex.codecogs.com/png.latex?a">, <img src="https://latex.codecogs.com/png.latex?b"> and <img src="https://latex.codecogs.com/png.latex?c"> with Eq.(5.2): <img src="https://latex.codecogs.com/png.latex?k%20=%20%5Clceil%204/2%20%5Crceil%20=%202"> and <img src="https://latex.codecogs.com/png.latex?m%20=%2010%5E2%20+%201%20=%20101">.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 10%">
<col style="width: 20%">
<col style="width: 21%">
<col style="width: 26%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?x"></th>
<th><img src="https://latex.codecogs.com/png.latex?m%20%5Cotimes%20x"></th>
<th><img src="https://latex.codecogs.com/png.latex?m%20%5Cotimes%20x%20%5Cominus%20x"></th>
<th><img src="https://latex.codecogs.com/png.latex?x_h"></th>
<th><img src="https://latex.codecogs.com/png.latex?x_l%20=%20x%20%5Cominus%20x_h"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?b%20=%203.476"></td>
<td><img src="https://latex.codecogs.com/png.latex?351.076%20%5Cto%20351.1"></td>
<td><img src="https://latex.codecogs.com/png.latex?347.624%20%5Cto%20347.6"></td>
<td><img src="https://latex.codecogs.com/png.latex?351.1%20-%20347.6%20=%203.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?-0.024"></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?a%20=%203.463"></td>
<td><img src="https://latex.codecogs.com/png.latex?349.763%20%5Cto%20349.8"></td>
<td><img src="https://latex.codecogs.com/png.latex?346.337%20%5Cto%20346.3"></td>
<td><img src="https://latex.codecogs.com/png.latex?349.8%20-%20346.3%20=%203.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?-0.037"></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?c%20=%203.479"></td>
<td><img src="https://latex.codecogs.com/png.latex?351.379%20%5Cto%20351.4"></td>
<td><img src="https://latex.codecogs.com/png.latex?347.921%20%5Cto%20347.9"></td>
<td><img src="https://latex.codecogs.com/png.latex?351.4%20-%20347.9%20=%203.5"></td>
<td><img src="https://latex.codecogs.com/png.latex?-0.021"></td>
</tr>
</tbody>
</table>
<p>So <img src="https://latex.codecogs.com/png.latex?b%20=%203.5%20-%200.024">, <img src="https://latex.codecogs.com/png.latex?a%20=%203.5%20-%200.037">, <img src="https://latex.codecogs.com/png.latex?c%20=%203.5%20-%200.021">, and with Eq.(5.1) each product is exact with <img src="https://latex.codecogs.com/png.latex?p%20=%204"> digits:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ab%5E2%20&amp;=%203.5%5E2%20-%202%20%5Ctimes%203.5%20%5Ctimes%200.024%20+%200.024%5E2%20=%2012.25%20-%200.168%20+%200.000576%20%5C%5C%5B6pt%5D%0Aac%20&amp;=%203.5%5E2%20-%20(3.5%20%5Ctimes%200.037%20+%203.5%20%5Ctimes%200.021)%20+%200.037%20%5Ctimes%200.021%20%5C%5C%5B2pt%5D%0A%20%20%20&amp;=%2012.25%20-%20(0.1295%20+%200.0735)%20+%200.000777%20=%2012.25%20-%200.2030%20+%200.000777%0A%5Cend%7Balign*%7D%0A"></p>
<p>where the sums are left unevaluated. Finally, subtracting these two series term by term:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ab%5E2%20-%20ac%20&amp;=%20(12.25%20-%2012.25)%20%5Coplus%20(0.2030%20-%200.168)%20%5Cominus%20(0.000777%20-%200.000576)%20%5C%5C%5B6pt%5D%0A&amp;=%200%20%5Coplus%200.0350%20%5Cominus%200.000201%20=%200.034799%20%5C;%20%5Crightarrow%20%5C;%200.03480%0A%5Cend%7Balign*%7D%0A"></p>
<p>which is identical to the exactly rounded result.</p>
<p><strong>Misprint in [1]</strong>: the ACM version prints “<img src="https://latex.codecogs.com/png.latex?0%20%5Coplus%20.0350%20%5Cominus%20.04685%20=%20.03480">”; the last term is <img src="https://latex.codecogs.com/png.latex?0.000777%20-%200.000576%20=%200.000201">.</p>
<p>To show that Theorem 6 really requires exact rounding, take <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, <img src="https://latex.codecogs.com/png.latex?p%20=%203"> and <img src="https://latex.codecogs.com/png.latex?x%20=%207%20=%20111_2">. Then <img src="https://latex.codecogs.com/png.latex?k%20=%20%5Clceil%203/2%20%5Crceil%20=%202">, <img src="https://latex.codecogs.com/png.latex?m%20=%202%5E2%20+%201%20=%205">, and</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Am%5C,x%20=%2035%20=%20100011_2%20%5C;%20%5Crightarrow%20%5C;%20m%20%5Cotimes%20x%20=%20100000_2%20=%2032%20%5Cqquad%20(35%20-%2032%20=%203%20%5C;%20%3C%20%5C;%2040%20-%2035%20=%205)%0A"></p>
<ul>
<li>with exact rounding: <img src="https://latex.codecogs.com/png.latex?m%20%5Cotimes%20x%20%5Cominus%20x%20=%2032%20-%207%20=%2025%20=%2011001_2%20%5Crightarrow%2011000_2%20=%2024">, so <img src="https://latex.codecogs.com/png.latex?x_h%20=%2032%20%5Cominus%2024%20=%208%20=%201_2%20%5Ctimes%202%5E3"> and <img src="https://latex.codecogs.com/png.latex?x_l%20=%207%20%5Cominus%208%20=%20-1">: both representable with <img src="https://latex.codecogs.com/png.latex?%5Clfloor%203/2%20%5Crfloor%20=%201"> bit;</li>
<li>with a single guard digit: <img src="https://latex.codecogs.com/png.latex?7%20=%201.11_2%20%5Ctimes%202%5E2%20=%200.00111_2%20%5Ctimes%202%5E5"> is truncated to <img src="https://latex.codecogs.com/png.latex?p%20+%201%20=%204"> digits, <img src="https://latex.codecogs.com/png.latex?0.001_2%20%5Ctimes%202%5E5%20=%204">, and <img src="https://latex.codecogs.com/png.latex?32%20-%204%20=%2028%20=%2011100_2">, exactly representable. So <img src="https://latex.codecogs.com/png.latex?m%20%5Cotimes%20x%20%5Cominus%20x%20=%2028">, <img src="https://latex.codecogs.com/png.latex?x_h%20=%2032%20-%2028%20=%204"> and <img src="https://latex.codecogs.com/png.latex?x_l%20=%207%20-%204%20=%203%20=%2011_2">, which is not representable with <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20p/2%20%5Crfloor%20=%201"> bit.</li>
</ul>
</section>
<section id="theorem-7" class="level3">
<h3 class="anchored" data-anchor-id="theorem-7">5.4 Theorem 7</h3>
<p>As a final example of exact rounding, consider dividing <img src="https://latex.codecogs.com/png.latex?m"> by 10. The result is a floating-point number that will in general not be equal to <img src="https://latex.codecogs.com/png.latex?m/10">. When <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, however, multiplying <img src="https://latex.codecogs.com/png.latex?m%20%5Coslash%2010"> by 10 will miraculously restore <img src="https://latex.codecogs.com/png.latex?m">, provided exact rounding is being used. Actually, a more general fact (due to Kahan) is true:</p>
<blockquote class="blockquote">
<p><strong>Theorem 7</strong>: When <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, if <img src="https://latex.codecogs.com/png.latex?m"> and <img src="https://latex.codecogs.com/png.latex?n"> are integers with <img src="https://latex.codecogs.com/png.latex?%5Clvert%20m%20%5Crvert%20%3C%202%5E%7Bp-1%7D"> and <img src="https://latex.codecogs.com/png.latex?n"> has the special form <img src="https://latex.codecogs.com/png.latex?n%20=%202%5Ei%20+%202%5Ej">, then <img src="https://latex.codecogs.com/png.latex?(m%20%5Coslash%20n)%20%5Cotimes%20n%20=%20m">, provided floating-point operations are exactly rounded.</p>
</blockquote>
<p>The proof is ingenious (see [1], pp.&nbsp;14–15), and skipped here. For the example above, <img src="https://latex.codecogs.com/png.latex?10%20=%202%5E3%20+%202%5E1">. The theorem holds true for any base <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, as long as <img src="https://latex.codecogs.com/png.latex?2%5Ei%20+%202%5Ej"> is replaced by <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5Ei%20+%20%5Cbeta%5Ej">. As <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> gets larger, however, there are fewer and fewer denominators of this form. Up to 100:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 5%">
<col style="width: 90%">
<col style="width: 4%">
</colgroup>
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?%5Cbeta"></th>
<th><img src="https://latex.codecogs.com/png.latex?n%20=%20%5Cbeta%5Ei%20+%20%5Cbeta%5Ej%20%5Cle%20100"></th>
<th>count</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>2</td>
<td>2, 3, 4, 5, 6, 8, 9, 10, 12, 16, 17, 18, 20, 24, 32, 33, 34, 36, 40, 48, 64, 65, 66, 68, 72, 80, 96</td>
<td>27</td>
</tr>
<tr class="even">
<td>10</td>
<td>2, 11, 20</td>
<td>3</td>
</tr>
</tbody>
</table>
<p>Numerical check in IEEE double precision (<img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%202">, <img src="https://latex.codecogs.com/png.latex?p%20=%2053">), where <code>m/n*n</code> in most programming languages computes exactly <img src="https://latex.codecogs.com/png.latex?(m%20%5Coslash%20n)%20%5Cotimes%20n"> (not in [1]):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A3%20%5Coslash%2010%20&amp;=%200.299999999999999988897769753748%5Cldots%20%5Cneq%200.3%20%5C%5C%5B4pt%5D%0A(3%20%5Coslash%2010)%20%5Ctimes%2010%20&amp;=%202.99999999999999988897769753748%5Cldots%20%5C;%20%5Crightarrow%20%5C;%20(3%20%5Coslash%2010)%20%5Cotimes%2010%20=%203%0A%5Cend%7Balign*%7D%0A"></p>
<p>since the double precision numbers between 2 and 4 are spaced <img src="https://latex.codecogs.com/png.latex?2%5E%7B-51%7D%20=%204.4%20%5Ctimes%2010%5E%7B-16%7D">, and the exact product is only <img src="https://latex.codecogs.com/png.latex?1.1%20%5Ctimes%2010%5E%7B-16%7D"> below 3. Counting the failures of <img src="https://latex.codecogs.com/png.latex?(m%20%5Coslash%20n)%20%5Cotimes%20n%20=%20m"> for <img src="https://latex.codecogs.com/png.latex?m%20=%201,%20%5Cldots,%202%5E%7B20%7D%20-%201">:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th><img src="https://latex.codecogs.com/png.latex?n"></th>
<th>binary form</th>
<th>failures</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>3</td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E1%20+%202%5E0"></td>
<td>0</td>
</tr>
<tr class="even">
<td>10</td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E3%20+%202%5E1"></td>
<td>0</td>
</tr>
<tr class="odd">
<td>12</td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E3%20+%202%5E2"></td>
<td>0</td>
</tr>
<tr class="even">
<td>7</td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E2%20+%202%5E1%20+%202%5E0"></td>
<td>74892</td>
</tr>
<tr class="odd">
<td>49</td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E5%20+%202%5E4%20+%202%5E0"></td>
<td>160502</td>
</tr>
<tr class="even">
<td>100</td>
<td><img src="https://latex.codecogs.com/png.latex?2%5E6%20+%202%5E5%20+%202%5E2"></td>
<td>146800</td>
</tr>
</tbody>
</table>
<p>For example, <img src="https://latex.codecogs.com/png.latex?(1%20%5Coslash%2049)%20%5Cotimes%2049%20=%200.9999999999999999%20%5Cneq%201">.</p>
</section>
<section id="does-it-matter" class="level3">
<h3 class="anchored" data-anchor-id="does-it-matter">5.5 Does it matter?</h3>
<p>We are now in a position to answer the question: does it matter if the basic arithmetic operations introduce a little more rounding error than necessary? The answer is that it does matter, because accurate basic operations enable us to prove that formulas are “correct”, in the sense that they have a small relative error. Section 4 discussed several algorithms that require guard digits to produce correct results in this sense. If the inputs to those formulas are numbers representing imprecise measurements, however, the bounds of Theorems 3 and 4 become less interesting. The reason is that the benign cancellation <img src="https://latex.codecogs.com/png.latex?x%20-%20y"> can become catastrophic if <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> are only approximations of some measured quantity. But accurate operations are useful even in the face of inexact data, because they enable us to establish exact relationships like those of Theorems 6 and 7. These are useful even if every floating-point variable is only an approximation of some actual value.</p>
</section>
</section>
<section id="reproducing-the-computations" class="level2">
<h2 class="anchored" data-anchor-id="reproducing-the-computations">6. Reproducing the computations</h2>
<p>All the base-10 examples above can be reproduced with Python’s <code>decimal</code> module: a <code>Context(prec=p)</code> performs exactly rounded <img src="https://latex.codecogs.com/png.latex?+">, <img src="https://latex.codecogs.com/png.latex?-">, <img src="https://latex.codecogs.com/png.latex?%5Ctimes">, <img src="https://latex.codecogs.com/png.latex?/">, <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B%5Ccdot%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cln"> with <img src="https://latex.codecogs.com/png.latex?p"> significant digits, and the rounding of halfway cases can be chosen (<code>ROUND_HALF_EVEN</code> or <code>ROUND_HALF_UP</code>). The truncating subtraction of Section 3 (with or without guard digit) has to be written by hand. The single precision examples can be reproduced with <code>numpy.float32</code>, computing <img src="https://latex.codecogs.com/png.latex?%5Cln"> and <img src="https://latex.codecogs.com/png.latex?%5Cexp"> in double precision and rounding them to single, so that they are accurate to within half an ulp as assumed by Theorem 4 (numpy’s own <code>float32</code> exponential is off by 2 ulps in this example, which changes the result by about 7 cents).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> decimal <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Decimal <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> D, Context, ROUND_HALF_EVEN</span>
<span id="cb5-2">c3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Context(prec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, rounding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ROUND_HALF_EVEN)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># beta = 10, p = 3, exactly rounded</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># catastrophic cancellation, Section 4.1</span></span>
<span id="cb5-5">b, a, c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> D(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3.34"</span>), D(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.22"</span>), D(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2.28"</span>)</span>
<span id="cb5-6">bb  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c3.multiply(b, b)                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 11.1556 -&gt; 11.2</span></span>
<span id="cb5-7">ac4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c3.multiply(D(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), c3.multiply(a, c))          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4 * 2.78 = 11.12 -&gt; 11.1</span></span>
<span id="cb5-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(c3.subtract(bb, ac4), b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>c)            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.1   0.0292</span></span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kahan's formula (4.5), Section 4.4</span></span>
<span id="cb5-11">a, b, c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> D(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9.0"</span>), D(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.53"</span>), D(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.53"</span>)</span>
<span id="cb5-12">amb  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c3.subtract(a, b)                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4.47, exact</span></span>
<span id="cb5-13">prod <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c3.multiply(c3.multiply(c3.multiply(</span>
<span id="cb5-14">           c3.add(a, c3.add(b, c)), c3.subtract(c, amb)),</span>
<span id="cb5-15">           c3.add(c, amb)), c3.add(a, c3.subtract(b, c)))</span>
<span id="cb5-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(c3.divide(c3.sqrt(prod), D(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)))               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2.35</span></span>
<span id="cb5-17"></span>
<span id="cb5-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compound interest, Section 4.5: beta = 2, p = 24</span></span>
<span id="cb5-19"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb5-20">f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.float32</span>
<span id="cb5-21">j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>)</span>
<span id="cb5-22">w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> j                                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 + 1379 * 2^-23</span></span>
<span id="cb5-23">P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-24"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>):</span>
<span id="cb5-25">    P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> w                                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (1 (+) j)^n</span></span>
<span id="cb5-26"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> j))                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 37615.453</span></span>
<span id="cb5-27"></span>
<span id="cb5-28">LN  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> v: f(np.log(np.float64(v)))            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ln, exp correctly rounded to float32</span></span>
<span id="cb5-29">EXP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> v: f(np.exp(np.float64(v)))</span>
<span id="cb5-30">ln1p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LN(w) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Theorem 4</span></span>
<span id="cb5-31">P4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EXP(f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ln1p)</span>
<span id="cb5-32"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((P4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> j))                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 37614.074</span></span></code></pre></div></div>


</section>

 ]]></description>
  <category>math</category>
  <category>computer science</category>
  <guid>https://foglienimatteo.com/blog/floating-point-rounding-error/</guid>
  <pubDate>Sun, 01 Nov 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The algebra of a loan: where the amortization formulas come from</title>
  <link>https://foglienimatteo.com/blog/bank-loan-interest-derivation/</link>
  <description><![CDATA[ 





<p>Notation:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?D"> : loan amount</li>
<li><img src="https://latex.codecogs.com/png.latex?R"> : monthly installment</li>
<li><img src="https://latex.codecogs.com/png.latex?I"> : nominal annual interest rate</li>
<li><img src="https://latex.codecogs.com/png.latex?i%20=%20%5Cfrac%7BI%7D%7B12%7D"> : monthly interest rate, 30/360 convention</li>
<li><img src="https://latex.codecogs.com/png.latex?N"> : total number of installments</li>
<li><img src="https://latex.codecogs.com/png.latex?T%20=%20D%20+%20Z"> : total paid (loan + interests)</li>
<li><img src="https://latex.codecogs.com/png.latex?Z"> : total interests paid</li>
</ul>
<p>For the <img src="https://latex.codecogs.com/png.latex?n">-th installment:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?d_n"> : debt left after the <img src="https://latex.codecogs.com/png.latex?n">-th installment (<img src="https://latex.codecogs.com/png.latex?d_0=D">)</li>
<li><img src="https://latex.codecogs.com/png.latex?p_n%20=%20R%20-%20i%5C,d_%7Bn-1%7D"> : principal part of the <img src="https://latex.codecogs.com/png.latex?n">-th installment</li>
<li><img src="https://latex.codecogs.com/png.latex?z_n%20=%20i%5C,d_%7Bn-1%7D"> : interest part of the <img src="https://latex.codecogs.com/png.latex?n">-th installment</li>
</ul>
<p>Here a summary of all the equations derived in this article:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7BN%20=%20%5Clceil%20n%5E*%20%5Crceil%7D%0A%5Cquad%20%5Cquad%20%5Cquad%0A%5Cboxed%7B%5C;n%5E*%20=%20%5Cfrac%7B%5Cln%7B(R)%7D%20-%20%5Cln%7B(R-i%5C,D)%7D%7D%7B%5Cln%7B(i+1)%7D%7D%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0AR_n%20=%20%5Cbegin%7Bcases%7D%0AR%20&amp;%20%5Cquad%20%5Cforall%20%5C,%20n=1,...,N-1%20%5C%5C%5B8pt%5D%0AR_%7B%5Ctext%7Blast%7D%7D%20%5Cle%20R%20&amp;%20%5Cquad%20n=%7BN%7D%0A%5Cend%7Bcases%7D%0A%5Cquad%20%5Cquad%20%5Cquad%0A%5Cboxed%7B%5C;R_%5Cmathrm%7Blast%7D%20=%20%5Cfrac%7BR%7D%7Bi%7D(i+1)%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20(i+1)%5EN%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0A%5C;%20R%20=%20%5Cfrac%7Bi%20%5C,%20(1%20+%20i)%5E%7BN%7D%7D%7B(1%20+%20i)%5E%7BN%7D%20-%201%7DD%0A=%20%5Cfrac%7Bi%7D%7B1%20-%20(1%20+%20i)%5E%7B-N%7D%7D%20D%0A%7D%0A%5Cquad%20%5Cquad%20%5Cquad%0A%5Cboxed%7B%5C;%0AD%20=%20%5Cfrac%7B(1%20+%20i)%5E%7BN%7D%20-%201%7D%7Bi%20%5C,%20(1%20+%20i)%5E%7BN%7D%7D%20R%0A=%20%5Cfrac%7B1%20-%20(1%20+%20i)%5E%7B-N%7D%7D%7Bi%7D%5C,%20R%0A%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?d_n%20=%20D%5C,(1+i)%5En%20-%20R%5C,%5Cfrac%7B(1+i)%5En%20-%201%7D%7Bi%7D"> <img src="https://latex.codecogs.com/png.latex?p_n%20=%20(R%20-%20iD)%5C,(i+1)%5E%7Bn-1%7D,%20%5Cqquad%20z_n%20=%20R%20-%20(R%20-%20iD)%5C,(i+1)%5E%7Bn-1%7D%5C,"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0A%5C;T%20=(N%20-%201)%5C,R%20+%20R_%5Cmathrm%7Blast%7D%20%5Capprox%20n%5E*%20R%0A%7D%0A%5Cquad%20%5Cquad%20%5Cquad%0A%5Cboxed%7B%0A%5C;Z%20=%20(N%20-%201)%5C,R%20+%20R_%5Cmathrm%7Blast%7D%20-%20D%20%5Capprox%20n%5E*%20R%20-%20D%0A%7D%0A%5Cquad%20%5Cquad%20%5Cquad%0AP_%5Cmathrm%7Btot%7D%20=%20D%0A"></p>
<section id="check-amortization-condition" class="level3">
<h3 class="anchored" data-anchor-id="check-amortization-condition">Check: amortization condition</h3>
<p>The debt decreases iff:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AR%20%3E%20i%5C,D%20%5Cquad%5CLongleftrightarrow%5Cquad%2012R%20%3E%20I%5C,D%0A"></p>
<p>If <img src="https://latex.codecogs.com/png.latex?R%20%5Cle%20iD">, then <img src="https://latex.codecogs.com/png.latex?d_n%20%5Cge%20D"> for all <img src="https://latex.codecogs.com/png.latex?n"> and the loan is never repaid.</p>
</section>
<section id="debt-left-to-be-payed-after-n-installments" class="level3">
<h3 class="anchored" data-anchor-id="debt-left-to-be-payed-after-n-installments">Debt left to be payed after <img src="https://latex.codecogs.com/png.latex?n"> installments</h3>
<p>Each month the bank adds the interest on the current balance, then subtracts the installment:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctag%7B1%7D%0A%5Cbegin%7Bcases%7D%0Ad_0%20&amp;=%20D%20%5C%5C%5B10pt%5D%0Ad_n%20&amp;=%20d_%7Bn-1%7D%20-%20(R%20-%20i%5C,d_%7Bn-1%7D)%20&amp;%20%5Cquad%5Cquad%20%5Cforall%20n%20%3E%200%5C%5C%5B4pt%5D%0A%5Cend%7Bcases%7D%0A"></p>
<p>With simple algebra:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5CRightarrow%20%5Cquad%20d_n%20&amp;=%20d_%7Bn-1%7D%20-%20(R%20-%20i%5C,d_%7Bn-1%7D)%20%5Cquad%5Cquad%20%5Cforall%20n%20%3E%200%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;%20%5Cquad%5Cquad%20q%20:=%201%20+%20i%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20q%5C,d_%7Bn-1%7D%20-%20R%20%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20q%5C,(q%5C,d_%7Bn-2%7D%20-%20R)%20-R%20=%20q%5E2%20d_%7Bn-2%7D%20-%20R(1%20+%20q)%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20q%5E2%5C,(q%5C,d_%7Bn-3%7D%20-%20R)%20-%20R(1%20+%20q)%20=%20q%5E3%20d_%7Bn-3%7D%20-%20R(1%20+%20q%20+%20q%5E2)%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20%5Cdots%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20q%5En%20d_0%20-%20R%20(1%20+%20q%20+%20q%5E2%20+%20...%20+%20q%5E%7Bn-1%7D)%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;%20%5Cquad%5Cquad%20d_0%20:=%20D%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20q%5En%20D%20-%20R%20%5Csum_%7Bk=0%7D%5E%7Bn-1%7D%20q%5Ek%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20q%5En%20D%20-%20R%5C,%5Cfrac%7Bq%5En%20-%201%7D%7Bq%20-%201%7D%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;%20%5Cquad%5Cquad%20q%20=%201%20+%20i%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20D%5C,(1+i)%5En%20-%20R%5C,%5Cfrac%7B(1+i)%5En%20-%201%7D%7Bi%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5C,d_n%20=%20D%5C,(1+i)%5En%20-%20R%5C,%5Cfrac%7B(1+i)%5En%20-%201%7D%7Bi%7D%7D%0A"></p>
<p>From this equation, we can easily derive the relation between the total number of installments <img src="https://latex.codecogs.com/png.latex?N">, the loan <img src="https://latex.codecogs.com/png.latex?D">, the interest rate <img src="https://latex.codecogs.com/png.latex?i"> and the installment <img src="https://latex.codecogs.com/png.latex?R"> by imposing the condition <img src="https://latex.codecogs.com/png.latex?d_N=0"> (i.e.&nbsp;the debt must be repaid at the end of last installment).</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ad_N%20=%200%0A%5Cquad&amp;%5Ciff%5Cquad%200%20=%20%5Cfrac%7BR%7D%7Bi%7D%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20(i+1)%5EN%20%5C%5C%5B10pt%5D%0A&amp;%5Ciff%5Cquad%20%5Cfrac%7BR-i%5C,D%7D%7Bi%7D(i+1)%5EN%20=%20%5Cfrac%7BR%7D%7Bi%7D%20%5C%5C%5B10pt%5D%0A&amp;%5Ciff%5Cquad%20%5Cboxed%7B(i+1)%5EN%20=%20%5Cfrac%7BR%7D%7BR-i%5C,D%7D%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>We’ll derive all the expressions later.</p>
<p><strong>Equivalent derivation from fixed-point theorem</strong></p>
<p>The fixed point of <img src="https://latex.codecogs.com/png.latex?d%20%5Cmapsto%20q%20d%20-%20R"> is <img src="https://latex.codecogs.com/png.latex?d%5E*%20=%20R/i">, and the deviation from it grows geometrically:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ad_%7Bn-1%7D%20%5Cmapsto%20d_%7Bn%7D%20&amp;=%20q%20d_n%20-%20R%20%5C%5C%5B8pt%5D%0Ad_%7Bn-1%7D%20=%20d_%7Bn%7D%20&amp;%5Ciff%20d_n=d%5E*:=%5Cfrac%7BR%7D%7Bq-1%7D=%5Cfrac%7BR%7D%7Bi%7D%20%5C%5C%5B8pt%5D%0Ad_%7Bn%7D%20-%20d%5E*%20&amp;=%20d_%7Bn%7D%20-%20%5Cfrac%7BR%7D%7Bi%7D%20%5C%5C%5B6pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20q%20%5C,%20d_%7Bn-1%7D%20-%20R%20-%20%5Cfrac%7BR%7D%7Bi%7D%20%20%5C%5C%5B6pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20q%20%5Cleft(%20d_%7Bn-1%7D%20-%20%5Cfrac%7B1%7D%7B%5Ccancel%7Bi+1%7D%7D%5Cfrac%7B%5Ccancel%7B(i+1)%7DR%7D%7Bi%7D%5Cright)%20%5C%5C%5B8pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20q%20%5Cleft(d_%7Bn-1%7D%20-%20d%5E*%5Cright)%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20q%5E2%20%5Cleft(d_%7Bn-2%7D%20-%20d%5E*%5Cright)%20%5C%5C%5B8pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20...%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20q%5En%20%5Cleft(d_%7B0%7D%20-%20d%5E*%5Cright)%20%5C%5C%5B8pt%5D%0A%5Cend%7Baligned%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5C;%5CLongrightarrow%5C;%0A%5Cboxed%7Bd_n%20%20-%20d%5E*%20=%20q%5En%20%5Cleft(d_%7B0%7D%20-%20d%5E*%5Cright)%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5C;%5CLongrightarrow%5C;%0A%5Cboxed%7Bd_n%20=%20%5Cfrac%7BR%7D%7Bi%7D%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20(i+1)%5En%7D%0A"></p>
</section>
<section id="total-number-of-installments" class="level3">
<h3 class="anchored" data-anchor-id="total-number-of-installments">Total number of installments</h3>
<p>To get the number of installments needed to repay a debt, we can set <img src="https://latex.codecogs.com/png.latex?d_n%20=%200"> and get the value <img src="https://latex.codecogs.com/png.latex?n%5E*">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ad_n%20=%200%0A%5Cquad&amp;%5Ciff%5Cquad%200%20=%20%5Cfrac%7BR%7D%7Bi%7D%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20(i+1)%5En%20%5C%5C%5B10pt%5D%0A&amp;%5Ciff%5Cquad%20%5Cfrac%7BR-i%5C,D%7D%7Bi%7D(i+1)%5En%20=%20%5Cfrac%7BR%7D%7Bi%7D%20%5C%5C%5B10pt%5D%0A&amp;%5Ciff%5Cquad%20(i+1)%5En%20=%20%5Cfrac%7BR%7D%7BR-i%5C,D%7D%20%5C%5C%5B10pt%5D%0A&amp;%5Ciff%5Cquad%20n%20%5C,%20%5Cln%7B(i+1)%7D%20=%20%5Cln%7B%5Cleft(%5Cfrac%7BR%7D%7BR-i%5C,D%7D%5Cright)%7D%20%5C%5C%5B10pt%5D%0A&amp;%5Ciff%5Cquad%20n%20=%20n%5E*%20:=%5Cfrac%7B%5Cln%7B(R)%7D%20-%20%5Cln%7B(R-i%5C,D)%7D%7D%7B%5Cln%7B(i+1)%7D%7D%20%5C%5C%5B10pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>In the previous expression, <img src="https://latex.codecogs.com/png.latex?n%5E%7B*%7D"> is real-valued =&gt; the (integer) number of installments is <img src="https://latex.codecogs.com/png.latex?N%20=%20%5Clceil%20n%5E%7B*%7D%20%5Crceil">. However, the last installment, that pays the remaining debt plus one month of interest on it, is smaller than (or equal to) the standard <img src="https://latex.codecogs.com/png.latex?R">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5C,n%5E*%20=%20%5Cfrac%7B%5Cln%7B(R)%7D%20-%20%5Cln%7B(Ri%5C,D)%7D%7D%7B%5Cln%7B(i+1)%7D%7D%7D%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BTotal%20%5C;%20number%20%5C;%20of%20%5C;%20installments:%7D%20%5Cquad%20%5Cboxed%7BN%20=%20%5Clceil%20n%5E*%20%5Crceil%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0AR_n%20=%20%5Cbegin%7Bcases%7D%0AR%20&amp;%20%5Cquad%20%5Cforall%20%5C,%20n=1,...,N-1%20%5C%5C%5B8pt%5D%0AR_%7B%5Ctext%7Blast%7D%7D%20%5Cle%20R%20&amp;%20%5Cquad%20n=%7BN%7D%0A%5Cend%7Bcases%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0AR_%5Cmathrm%7Blast%7D%20:=%20(i+1)%5C,d_%7BN-1%7D%0A=%20%5Cfrac%7BR%7D%7Bi%7D(i+1)%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20(i+1)%5EN%7D%0A"></p>
</section>
<section id="installment-and-loan-from-the-other-terms" class="level3">
<h3 class="anchored" data-anchor-id="installment-and-loan-from-the-other-terms">Installment and Loan from the other terms</h3>
<p>Recalling the equation we derived by setting <img src="https://latex.codecogs.com/png.latex?d_N=0">, we can get all the inverses:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ad_N%20=%200%0A%5Cquad&amp;%5Ciff%5Cquad%200%20=%20%5Cfrac%7BR%7D%7Bi%7D%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20(i+1)%5EN%20%5C%5C%5B10pt%5D%0A%5Cquad&amp;%5Ciff%5Cquad%20R%20+%20(iD%20-%20R)%20(i+1)%5EN%20=%200%20%5C%5C%5B10pt%5D%0A%5Cquad&amp;%5Ciff%5Cquad%20%5Cleft%5B1-(i+1)%5EN%5Cright%5DR%20+%20i(i+1)%5EN%20D%20=%200%20%5C%5C%5B10pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p><strong>Installment from <img src="https://latex.codecogs.com/png.latex?D">, <img src="https://latex.codecogs.com/png.latex?I">, <img src="https://latex.codecogs.com/png.latex?N"></strong></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0AR%20=%20%5Cfrac%7Bi%20%5C,%20(1%20+%20i)%5E%7BN%7D%7D%7B(1%20+%20i)%5E%7BN%7D%20-%201%7DD%0A=%20%5Cfrac%7Bi%7D%7B1%20-%20(1%20+%20i)%5E%7B-N%7D%7D%20D%0A%7D%0A"></p>
<p><strong>Loan from <img src="https://latex.codecogs.com/png.latex?R">, <img src="https://latex.codecogs.com/png.latex?I">, <img src="https://latex.codecogs.com/png.latex?N"></strong></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0AD%20=%20%5Cfrac%7B(1%20+%20i)%5E%7BN%7D%20-%201%7D%7Bi%20%5C,%20(1%20+%20i)%5E%7BN%7D%7D%20R%0A=%20%5Cfrac%7B1%20-%20(1%20+%20i)%5E%7B-N%7D%7D%7Bi%7D%5C,%20R%0A%7D%0A"></p>
<p><strong>Interest rate from <img src="https://latex.codecogs.com/png.latex?D">, <img src="https://latex.codecogs.com/png.latex?R">, <img src="https://latex.codecogs.com/png.latex?N">: no closed form</strong></p>
<p>Substitute <img src="https://latex.codecogs.com/png.latex?x%20=%201/q"> into the present-value condition:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ag(x)%20%5Cequiv%20R%5C,(x%20+%20x%5E2%20+%20%5Cdots%20+%20x%5EN)%20-%20D%20=%200%0A"></p>
<p>This is a polynomial of degree <img src="https://latex.codecogs.com/png.latex?N"> in <img src="https://latex.codecogs.com/png.latex?x">. For <img src="https://latex.codecogs.com/png.latex?N%20%5Cge%205"> there is no general solution in radicals (Abel–Ruffini), and <img src="https://latex.codecogs.com/png.latex?N"> is usually in the hundreds. The same equation defines the APR / internal rate of return.</p>
<p>Existence and uniqueness of a positive rate: <img src="https://latex.codecogs.com/png.latex?g"> is strictly increasing on <img src="https://latex.codecogs.com/png.latex?x%20%3E%200">, with <img src="https://latex.codecogs.com/png.latex?g(0)%20=%20-D%20%3C%200"> and <img src="https://latex.codecogs.com/png.latex?g(x)%20%5Cto%20%5Cinfty">. So there is exactly one positive root <img src="https://latex.codecogs.com/png.latex?x%5E%7B*%7D">. It satisfies <img src="https://latex.codecogs.com/png.latex?x%5E%7B*%7D%20%3C%201">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?i%20%3E%200">, iff</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ag(1)%20=%20R%5C,N%20-%20D%20%3E%200%0A"></p>
<p>Numerically, in terms of <img src="https://latex.codecogs.com/png.latex?i">: the present value</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BPV%7D(i)%20=%20R%5C,%5Cfrac%7B1%20-%20(1%20+%20i)%5E%7B-N%7D%7D%7Bi%7D,%20%5Cqquad%0A%5Clim_%7Bi%20%5Cto%200%7D%20%5Cmathrm%7BPV%7D(i)%20=%20R%5C,N%0A"></p>
<p>is strictly decreasing in <img src="https://latex.codecogs.com/png.latex?i">. Bisection on <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BPV%7D(i)%20=%20D"> therefore converges to the unique root, in <img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Clog_2(%5Ctext%7Bwidth%7D/%5Ctext%7Btol%7D)%20%5Crceil%20%5Capprox%2043"> steps for width <img src="https://latex.codecogs.com/png.latex?1"> and tolerance <img src="https://latex.codecogs.com/png.latex?10%5E%7B-13%7D">.</p>
<p>For small <img src="https://latex.codecogs.com/png.latex?i">, evaluate <img src="https://latex.codecogs.com/png.latex?1%20-%20(1%20+%20i)%5E%7B-N%7D"> as <code>-expm1(-N*log1p(i))</code> to avoid cancellation.</p>
</section>
<section id="principal-part-and-interest-part" class="level3">
<h3 class="anchored" data-anchor-id="principal-part-and-interest-part">Principal part and Interest part</h3>
<p>Recalling the definitions of the principal part <img src="https://latex.codecogs.com/png.latex?p_n"> and interest part <img src="https://latex.codecogs.com/png.latex?z_n"> of each installment</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Az_n%20&amp;:=%20i%5C,d_%7Bn-1%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5C%5C%5B10pt%5D%0Ap_n%20&amp;:=%20R%20-%20i%5C,d_%7Bn-1%7D%20=%20d_%7Bn-1%7D%20-%20d_n%0A%5Cend%7Balign*%7D%0A"></p>
<p>we can get their expressions by inserting the fixed-point form of <img src="https://latex.codecogs.com/png.latex?d_%7Bn-1%7D"> into <img src="https://latex.codecogs.com/png.latex?p_n">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ap_n%20&amp;=%20R%20-%20i%5C,d_%7Bn-1%7D%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%5Ccancel%7BR%7D%20-%20i%5Cleft%5B%5Ccancel%7B%5Cfrac%7BR%7D%7Bi%7D%7D%20+%20%5Cleft(D%20-%20%5Cfrac%7BR%7D%7Bi%7D%5Cright)%20q%5E%7Bn-1%7D%5Cright%5D%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20(R-iD)%5C,q%5E%7Bn-1%7D%20%5C%5C%5B10pt%5D%0Az_n%20&amp;=%20i%5C,d_%7Bn-1%7D%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20R%20-%20(R%20-%20iD)%20q%5E%7Bn-1%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5C,p_n%20=%20(R%20-%20iD)%5C,(i+1)%5E%7Bn-1%7D,%20%5Cqquad%20z_n%20=%20R%20-%20(R%20-%20iD)%5C,(i+1)%5E%7Bn-1%7D%5C,%7D%0A"></p>
<p>The principal part grows geometrically, so the interest part shrinks.</p>
</section>
<section id="total-paid-and-total-paid-interest" class="level3">
<h3 class="anchored" data-anchor-id="total-paid-and-total-paid-interest">Total paid and total paid interest</h3>
<p>We can get the total paied <img src="https://latex.codecogs.com/png.latex?T"> and the total interest paid <img src="https://latex.codecogs.com/png.latex?Z"> easily (the total principal paid <img src="https://latex.codecogs.com/png.latex?P_%5Cmathrm%7Btot%7D"> is trivial):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0AT%20=(N%20-%201)%5C,R%20+%20R_%5Cmathrm%7Blast%7D%20%5Capprox%20n%5E*%20R%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0AZ%20=%20(N%20-%201)%5C,R%20+%20R_%5Cmathrm%7Blast%7D%20-%20D%20%5Capprox%20n%5E*%20R%20-%20D%0A%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%5Cmathrm%7Btot%7D%20=%20D%0A"></p>
<p>Treating <img src="https://latex.codecogs.com/png.latex?N"> as continuous gives <img src="https://latex.codecogs.com/png.latex?Z%20%5Capprox%20R%5C,n%5E*%20-%20D">, with an error below <img src="https://latex.codecogs.com/png.latex?R">.</p>
</section>
<section id="function-definitions" class="level2">
<h2 class="anchored" data-anchor-id="function-definitions">Function definitions</h2>
<section id="analytical-definitions" class="level3">
<h3 class="anchored" data-anchor-id="analytical-definitions">Analytical definitions</h3>
<p>Note: for numerical reasons, it’s better to rewrite some terms as follows and use the numerical functions <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Blog1p%7D(x)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bexpm1%7D(x)">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cln(1+i)%20&amp;=%20%5Cmathrm%7Blog1p%7D(i)%20%5C%5C%5B6pt%5D%0A1-(1+i)%5E%7B-N%7D%20&amp;=%20-%20%5Cleft%5B(1+i)%5E%7B-N%7D%20-1%20%5Cright%5D%20=%20-%20%5Cleft%5Be%5E%7B-N%5C,%5Cln(1+i)%7D%20-1%20%5Cright%5D%20=%20-%5Cmathrm%7Bexpm1%7D(-N%20%5C,%20%5Cmathrm%7Blog1p%7D(i))%5C%5C%5B10pt%5D%0A%5Cmathrm%7Blog1p%7D(x)%20:&amp;=%20%5Cln(1+x)%20%5C%5C%5B6pt%5D%0A%5Cmathrm%7Bexpm1%7D(x)%20:&amp;=%20e%5Ex%20-1%0A%5Cend%7Balign*%7D%0A"></p>


</section>
</section>

 ]]></description>
  <category>math</category>
  <guid>https://foglienimatteo.com/blog/bank-loan-interest-derivation/</guid>
  <pubDate>Sun, 18 Oct 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>What a loan really costs you</title>
  <link>https://foglienimatteo.com/blog/bank-loan-interest-plots/</link>
  <description><![CDATA[ 





<p>Here are some interactive plots of what a loan actually costs you. Most of a loan’s early payments are almost all interest, with only a trickle going to the principal; the balance tips the other way only later on. Pick a loan and a rate below, then either set the monthly installment and read off how long you will be paying, or set the term and get the installment it implies. The curves and the summary follow as you drag, and hovering a curve reads off its values.</p>
<p>Notation:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?D"> : loan amount</li>
<li><img src="https://latex.codecogs.com/png.latex?R"> : monthly installment</li>
<li><img src="https://latex.codecogs.com/png.latex?I"> : nominal annual interest rate</li>
<li><img src="https://latex.codecogs.com/png.latex?i%20=%20%5Cfrac%7BI%7D%7B12%7D"> : monthly interest rate, 30/360 convention</li>
<li><img src="https://latex.codecogs.com/png.latex?N"> : total number of installments</li>
<li><img src="https://latex.codecogs.com/png.latex?T%20=%20D%20+%20Z"> : total paid (loan + interests)</li>
<li><img src="https://latex.codecogs.com/png.latex?Z"> : total interests paid</li>
</ul>
<p>For the <img src="https://latex.codecogs.com/png.latex?n">-th installment:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?d_n"> : debt left after the <img src="https://latex.codecogs.com/png.latex?n">-th installment (<img src="https://latex.codecogs.com/png.latex?d_0=D">)</li>
<li><img src="https://latex.codecogs.com/png.latex?p_n%20=%20R%20-%20i%5C,d_%7Bn-1%7D"> : principal part of the <img src="https://latex.codecogs.com/png.latex?n">-th installment</li>
<li><img src="https://latex.codecogs.com/png.latex?z_n%20=%20i%5C,d_%7Bn-1%7D"> : interest part of the <img src="https://latex.codecogs.com/png.latex?n">-th installment</li>
</ul>
<p>The formulas used here are derived in <a href="../../blog/bank-loan-interest-derivation/index.html">Bank interest functions: analytical derivation</a>.</p>
<section id="your-loan" class="level2">
<h2 class="anchored" data-anchor-id="your-loan">Your loan</h2>
<div class="controls">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="38" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 37;"><span id="cb1-38">viewof D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500000</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Loan D [€]"</span>})</span>
<span id="cb1-39">viewof I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Annual rate I [%]"</span>})</span>
<span id="cb1-40">viewof mode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radio</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span>([[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"set the monthly installment"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"set the term"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>]])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I want to"</span>})</span>
<span id="cb1-41">viewof RN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span></span>
<span id="cb1-42">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Installment R [€]"</span>})</span>
<span id="cb1-43">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Term [years]"</span>})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-4" data-nodetype="declaration">

</div>
</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="52" data-source-offset="-234" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 51;"><span id="cb2-52">installmentForTerm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> years) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-53">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(years <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-54">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-55">}</span>
<span id="cb2-56">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> RN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">installmentForTerm</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> RN)</span>
<span id="cb2-57"></span>
<span id="cb2-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Does the installment cover at least the first month of interest?</span></span>
<span id="cb2-59">amortizes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> (I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D</span>
<span id="cb2-60"></span>
<span id="cb2-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of installments N = ceil(n*), last installment, totals.</span></span>
<span id="cb2-62"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loan</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I) {</span>
<span id="cb2-63">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-64">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">amortizes</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-65">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> nstar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(R) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p</span>(i)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-66">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceil</span>(nstar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-9</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-67">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-68">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> Rlast <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-69">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Rlast<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-70">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Rlast<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> D}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-71">}</span>
<span id="cb2-72"></span>
<span id="cb2-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// One row per installment: interest part z_n, principal part p_n, debt left d_n.</span></span>
<span id="cb2-74"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">schedule</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I) {</span>
<span id="cb2-75">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loan</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-76">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>L) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-77">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-78">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-79">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> L<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-80">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-81">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> paid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> L<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> L<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rlast</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-82">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (paid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> z))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-83">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">years</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">p</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> paid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">share</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> I}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-84">  })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-85">}</span>
<span id="cb2-86"></span>
<span id="cb2-87">rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]</span>
<span id="cb2-88">color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`var(--chart-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>I<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">)`</span></span>
<span id="cb2-89">eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(x)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toLocaleString</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"en-GB"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" €"</span></span>
<span id="cb2-90">pct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".1%"</span>)</span>
<span id="cb2-91"></span>
<span id="cb2-92">mine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loan</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-5" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-6" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-7" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-8" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-9" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-10" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb3" data-startfrom="95" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 94;"><span id="cb3-95">mine</span>
<span id="cb3-96">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div class="stats"&gt;</span></span>
<span id="cb3-97"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>mode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div&gt;&lt;div class="stat__label"&gt;Monthly installment&lt;/div&gt;</span></span>
<span id="cb3-98"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(R)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-99"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;to clear it in </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>RN<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> years&lt;/div&gt;&lt;/div&gt;`</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-100"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Installments&lt;/div&gt;</span></span>
<span id="cb3-101"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-102"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>(mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> years&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-103"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Total interest Z&lt;/div&gt;</span></span>
<span id="cb3-104"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-105"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pct</span>(mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> of the loan&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-106"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;Total paid T&lt;/div&gt;</span></span>
<span id="cb3-107"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-108"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;last installment </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rlast</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-109"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      &lt;div&gt;&lt;div class="stat__label"&gt;First installment&lt;/div&gt;</span></span>
<span id="cb3-110"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pct</span>((I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> R)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-111"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">           &lt;div class="stat__note"&gt;of it is interest&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-112"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;/div&gt;`</span></span>
<span id="cb3-113">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;p&gt;&lt;strong&gt;This loan is never repaid:&lt;/strong&gt; the first month of interest is</span></span>
<span id="cb3-114"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>((I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">, which is not less than the installment of </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(R)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">. Raise &lt;em&gt;R&lt;/em&gt; or lower &lt;em&gt;I&lt;/em&gt;.&lt;/p&gt;`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-3" data-nodetype="expression">

</div>
</div>
</div>
</section>
<section id="interest-part-of-each-installment" class="level2">
<h2 class="anchored" data-anchor-id="interest-part-of-each-installment">Interest part of each installment</h2>
<p>How much of the <img src="https://latex.codecogs.com/png.latex?n">-th installment is interest, <img src="https://latex.codecogs.com/png.latex?z_n%20/%20R">, for a loan of <span><span id="ojs-element-id-1"></span></span> repaid with <span><span id="ojs-element-id-2"></span></span> a month. The blue curves are fixed annual rates from 1% to 6%, the orange one is your rate. A rate whose first month of interest exceeds the installment has no curve: that loan is never repaid.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb4" data-startfrom="125" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 124;"><span id="cb4-125">curvesA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">flatMap</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">schedule</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> r))</span>
<span id="cb4-126">mineA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">schedule</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I)</span>
<span id="cb4-127"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// One label per curve, sitting on the line a bit before its middle.</span></span>
<span id="cb4-128">labelsA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> curvesA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> r))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> c[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.42</span>)])</span>
<span id="cb4-129"></span>
<span id="cb4-130">legend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div class="chart-legend"&gt;</span></span>
<span id="cb4-131"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>rates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;span&gt;&lt;i style="color:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">color</span>(r)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"&gt;&lt;/i&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%&lt;/span&gt;`</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-132"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;span&gt;&lt;i style="color:var(--chart-highlight)"&gt;&lt;/i&gt;your rate, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>I<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%&lt;/span&gt;</span></span>
<span id="cb4-133"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;`</span></span>
<span id="cb4-134"></span>
<span id="cb4-135"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Hover box in the page's own colours, so it is readable in light and dark.</span></span>
<span id="cb4-136">tipStyle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-bg)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-muted)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>})</span>
<span id="cb4-137"></span>
<span id="cb4-138"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Curve labels: text in the page's ink with a halo in the background colour.</span></span>
<span id="cb4-139">label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"currentColor"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--plot-background)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-5" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-6" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="chart">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb5" data-startfrom="143" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 142;"><span id="cb5-143"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-5" data-nodetype="expression">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb6" data-startfrom="147" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 146;"><span id="cb6-147">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb6-148">  width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-149">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-150">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginTop</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-151">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">44</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-152">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginRight</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-153">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Years →"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">nice</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-154">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"↑ Interest part of the installment"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tickFormat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">zero</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-155">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb6-156">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleY</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-157">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>rates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lineY</span>(curvesA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> r)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"years"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">color</span>(r)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-158">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lineY</span>(mineA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"years"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-highlight)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-159">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label</span>(labelsA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"years"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-160">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tip</span>([<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>curvesA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>mineA]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pointer</span>({</span>
<span id="cb6-161">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>tipStyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-162">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"years"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-163">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">title</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`I = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">after </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">years</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> years (installment </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">interest </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pct</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">share</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> of R</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">debt left </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb6-164">    }))</span>
<span id="cb6-165">  ]</span>
<span id="cb6-166">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-6" data-nodetype="expression">

</div>
</div>
</div>
</div>
</section>
<section id="total-interest-paid" class="level2">
<h2 class="anchored" data-anchor-id="total-interest-paid">Total interest paid</h2>
<p>Total interest <img src="https://latex.codecogs.com/png.latex?Z"> as a fraction of the loan, against the monthly installment, for a loan of <span><span id="ojs-element-id-3"></span>.</span> The curves leave the plot at 100%: beyond that you pay back more than twice what you borrowed.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb7" data-startfrom="177" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 176;"><span id="cb7-177">Rmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.002</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D)</span>
<span id="cb7-178">Rmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.012</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.15</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> R)</span>
<span id="cb7-179">curvesB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">flatMap</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span></span>
<span id="cb7-180">  d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(Rmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Rmax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> (Rmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Rmin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span>
<span id="cb7-181">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loan</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> r)}))</span>
<span id="cb7-182">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span>)</span>
<span id="cb7-183">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">frac</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">N</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span>}))</span>
<span id="cb7-184">)</span>
<span id="cb7-185">mineB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(Rmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Rmax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> (Rmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Rmin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span>
<span id="cb7-186">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> I<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loan</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I)}))</span>
<span id="cb7-187">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span>)</span>
<span id="cb7-188">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">frac</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">N</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span>}))</span>
<span id="cb7-189"></span>
<span id="cb7-190"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// One label per curve, where it crosses 50% (or as close to it as the curve gets).</span></span>
<span id="cb7-191">labelsB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rates</span>
<span id="cb7-192">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">least</span>(</span>
<span id="cb7-193">    curvesB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frac</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> Rmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (Rmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Rmin))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-194">    d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frac</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)))</span>
<span id="cb7-195">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-5" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="chart">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb8" data-startfrom="199" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 198;"><span id="cb8-199"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-8" data-nodetype="expression">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb9" data-startfrom="203" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 202;"><span id="cb9-203">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb9-204">  width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-205">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">380</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-206">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginTop</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-207">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">44</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-208">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginRight</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-209">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monthly installment R [€] →"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [Rmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Rmax]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ticks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-210">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"↑ Total interest, as a fraction of the loan"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tickFormat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-211">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb9-212">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleY</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-213">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>rates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lineY</span>(curvesB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> r)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frac"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">color</span>(r)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">clip</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-214">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lineY</span>(mineB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frac"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-highlight)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">clip</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-215">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label</span>(labelsB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frac"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-216">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleX</span>([R]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"currentColor"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeOpacity</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeDasharray</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3,3"</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-217">    mine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb9-218">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot</span>([{R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">frac</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> mine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D}]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frac"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-highlight)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--plot-background)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>})</span>
<span id="cb9-219">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-220">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tip</span>([<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>curvesB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>mineB]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frac</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pointer</span>({</span>
<span id="cb9-221">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>tipStyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-222">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frac"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-223">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">title</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`I = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">installment </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">interest </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eur</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pct</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frac</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> of the loan</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> installments (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> years)`</span></span>
<span id="cb9-224">    }))</span>
<span id="cb9-225">  ]</span>
<span id="cb9-226">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-9" data-nodetype="expression">

</div>
</div>
</div>
</div>
</section>
<section id="the-numbers-year-by-year" class="level2">
<h2 class="anchored" data-anchor-id="the-numbers-year-by-year">The numbers, year by year</h2>
<p>Your loan, one row per year.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb10" data-startfrom="236" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 235;"><span id="cb10-236">yearly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rollups</span>(</span>
<span id="cb10-237">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">schedule</span>(D<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-238">  v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">principal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">debt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> v[v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-239">  d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceil</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb10-240">)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(([year<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> v]) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({year<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>v}))</span>
<span id="cb10-241"></span>
<span id="cb10-242">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(yearly<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {</span>
<span id="cb10-243">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">columns</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interest"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"principal"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"debt"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-244">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">header</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Interest paid"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">principal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Principal paid"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">debt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Debt left"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-245">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> eur<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">principal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> eur<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">debt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> eur}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-246">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">align</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-247">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">select</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-248">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">layout</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-249">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">maxHeight</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">380</span></span>
<span id="cb10-250">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-10-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-10-2" data-nodetype="expression">

</div>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Static version of the plots
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The same two plots as images, for a loan of 100000 €.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://foglienimatteo.com/blog/bank-loan-interest-plots/monthly-interest-paid-for-different-interest-rates.png" class="img-fluid figure-img"></p>
<figcaption>Interest part of each installment for a monthly installment of 500 € (left) and 1000 € (right)</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://foglienimatteo.com/blog/bank-loan-interest-plots/total-interest-paid-for-different-interest-rates-2.png" class="img-fluid figure-img"></p>
<figcaption>Total interest paid as a fraction of the loan, against the monthly installment</figcaption>
</figure>
</div>
</div>
</div>
</div>


<div class="ojs-auto-generated hidden">
<script type="ojs-module-contents">
eyJjb250ZW50cyI6WyAgeyJtZXRob2ROYW1lIjoiaW50ZXJwcmV0IiwiaW5saW5lIjoidHJ1ZSIsInNvdXJjZSI6Imh0bC5odG1sYDxzcGFuPiR7ZXVyKEQpfTwvc3Bhbj5gIiwgImNlbGxOYW1lIjoib2pzLWVsZW1lbnQtaWQtMSJ9LCAgeyJtZXRob2ROYW1lIjoiaW50ZXJwcmV0IiwiaW5saW5lIjoidHJ1ZSIsInNvdXJjZSI6Imh0bC5odG1sYDxzcGFuPiR7ZXVyKFIpfTwvc3Bhbj5gIiwgImNlbGxOYW1lIjoib2pzLWVsZW1lbnQtaWQtMiJ9LCAgeyJtZXRob2ROYW1lIjoiaW50ZXJwcmV0IiwiaW5saW5lIjoidHJ1ZSIsInNvdXJjZSI6Imh0bC5odG1sYDxzcGFuPiR7ZXVyKEQpfTwvc3Bhbj5gIiwgImNlbGxOYW1lIjoib2pzLWVsZW1lbnQtaWQtMyJ9XX0=
</script>
</div>
</section>

 ]]></description>
  <category>math</category>
  <guid>https://foglienimatteo.com/blog/bank-loan-interest-plots/</guid>
  <pubDate>Sun, 18 Oct 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Numerov’s method: solving y″ = f without the first derivative</title>
  <link>https://foglienimatteo.com/blog/numerov-method/</link>
  <description><![CDATA[ 





<p>References:</p>
<ul>
<li>[1] J. L. M. Quiroz González, D. Thompson: <em>“Getting started with Numerov’s method”</em>, Computers in Physics, Vol. 11 Num. 5 pag. 514–515 (1997), DOI <a href="https://doi.org/10.1063/1.168593">10.1063/1.168593</a></li>
</ul>
<section id="tldr" class="level2">
<h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<p>Equation to be solved numerically</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2y%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20=%20U(x)%20+%20V(x)%5C,y%7D%20%5Ctag%7B1.1%7D%0A"></p>
<p><strong>Assumptions</strong>: we suppose the following:</p>
<ul>
<li>to have a set of <img src="https://latex.codecogs.com/png.latex?N+1"> values <img src="https://latex.codecogs.com/png.latex?x_0,%20...,%20x_N"> EQUALLY SPACED by <img src="https://latex.codecogs.com/png.latex?h=x_%7Bn+1%7D-x_%7Bn%7D%5C,%20,%5C;%20%5Cforall%5C,%20n=0,...,N-1"></li>
<li>to know all the <img src="https://latex.codecogs.com/png.latex?U_n"> and <img src="https://latex.codecogs.com/png.latex?V_n"></li>
<li>defining <img src="https://latex.codecogs.com/png.latex?y_n%20:=%20y(x_n)">, to know <img src="https://latex.codecogs.com/png.latex?y_0"> and <img src="https://latex.codecogs.com/png.latex?y_0'"></li>
<li><img src="https://latex.codecogs.com/png.latex?F_n%20%20:=%20U_n%20+%20V_n%5C,y_n"></li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0Ay_%7Bn+1%7D%20=%20%5Cfrac%7B%0A%20%20%20%202y_n%20-%20y_%7Bn-1%7D%20+%20%5Cdfrac%7Bh%5E2%7D%7B12%7D%5Cbig(U_%7Bn+1%7D%20+%2010F_n%20+%20F_%7Bn-1%7D%5Cbig)%0A%20%20%7D%7B%0A%20%20%20%201%20-%20%5Cdfrac%7Bh%5E2%20%5C,V_%7Bn+1%7D%7D%7B12%7D%0A%20%20%7D%20+%20O(h%5E6)%7D%20%5Ctag%7B2.8%7D%0A"></p>
<p>For the first step</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0Ay_1%20=%20%5Ctfrac%7B%0A%20%20%20%20y_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%5Cright)%20+%20h%5C,y'_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20%5C%5C%0A%20%20%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%20-%20%5Ctfrac%7Bh%5E4%5C,V_2%7D%7B36%7D%5Cbig(F_0%20+%202U_1%5Cbig)%0A%20%20%7D%7B%0A%20%20%20%201%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%20+%20%5Ctfrac%7BV_1V_2h%5E4%7D%7B18%7D%0A%20%20%7D%20+%20O(h%5E5)%7D%20%5Ctag%7B3.8%7D%0A"></p>
<p>The methods has a global error of <img src="https://latex.codecogs.com/png.latex?O(h%5E4)"></p>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">1. Introduction</h2>
<p>Numerov’s method is an efficient algorithm for solving second-order differential equations of the following form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2y%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20=%20U(x)%20+%20V(x)%5C,y%7D%20%5Ctag%7B1.1%7D%0A"></p>
<p>Numerov’s method relies on two properties of Eq.(1.1):</p>
<ol type="1">
<li>the first derivative <img src="https://latex.codecogs.com/png.latex?dy/dx"> is absent;</li>
<li><img src="https://latex.codecogs.com/png.latex?U(x)%20+%20V(x)%5C,%20y"> is linear wrt <img src="https://latex.codecogs.com/png.latex?y">.</li>
</ol>
<p>Examples in physics of this type of equations:</p>
<ul>
<li>1D time-independent Schrödinger equation. From <img src="https://latex.codecogs.com/png.latex?%5B-%5Cfrac%7B%5Chbar%5E2%7D%7B2m%7D%5Ctfrac%7B%5Cmathrm%7Bd%7D%5E2%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20+%20V(x)%5D%5Cpsi%20=%20E%5Cpsi">:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2%5Cpsi%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20=%20%5Cfrac%7B2m%7D%7B%5Chbar%5E2%7D%5C,%5Cbig%5BV(x)%20-%20E%5Cbig%5D%5C,%5Cpsi%20%5Ctag%7B1.2%7D%0A"></p>
<ul>
<li>Undamped forced harmonic oscillator (<img src="https://latex.codecogs.com/png.latex?x"> plays the role of time):</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Am%5C,%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2y%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20=%20f_0%5Ccos%7B(%5Comega%20x)%7D%20-%20k%5C,y%20%5Ctag%7B1.3%7D%0A"></p>
<ul>
<li>Poisson equation with spherically symmetric distribution in SI units:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cnabla%5E2%5Cphi%20=%20%5Cfrac%7B1%7D%7Br%7D%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2(r%5Cphi)%7D%7B%5Cmathrm%7Bd%7Dr%5E2%7D%20=%20-%5Cfrac%7B%5Crho(r)%7D%7B%5Cvarepsilon_0%7D%20%5Ctag%7B1.4%7D%0A"></p>
<p>Cast into the form of Eq.(1.1):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 24%">
<col style="width: 19%">
<col style="width: 29%">
<col style="width: 26%">
</colgroup>
<thead>
<tr class="header">
<th>equation</th>
<th><img src="https://latex.codecogs.com/png.latex?y"></th>
<th><img src="https://latex.codecogs.com/png.latex?U(x)"></th>
<th><img src="https://latex.codecogs.com/png.latex?V(x)"> of Eq.(1.1)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>(1.2) Schrödinger</td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cpsi"></td>
<td><img src="https://latex.codecogs.com/png.latex?0"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B2m%7D%7B%5Chbar%5E2%7D%5BV(x)%20-%20E%5D"></td>
</tr>
<tr class="even">
<td>(1.3) forced oscillator</td>
<td><img src="https://latex.codecogs.com/png.latex?y"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bf_0%7D%7Bm%7D%5Ccos%5Comega%20x"></td>
<td><img src="https://latex.codecogs.com/png.latex?-%5Cfrac%7Bk%7D%7Bm%7D"></td>
</tr>
<tr class="odd">
<td>(1.4) Poisson, spherical</td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cvarphi%20%5Cequiv%20r%5Cphi"></td>
<td><img src="https://latex.codecogs.com/png.latex?-%5Cfrac%7Br%5Crho(r)%7D%7B%5Cvarepsilon_0%7D"></td>
<td><img src="https://latex.codecogs.com/png.latex?0"></td>
</tr>
</tbody>
</table>
</section>
<section id="derivation" class="level2">
<h2 class="anchored" data-anchor-id="derivation">2. Derivation</h2>
<p>We now want to apply the central difference scheme to Eq.(1.1).</p>
<p><strong>Assumption</strong>: We suppose to have a set of <img src="https://latex.codecogs.com/png.latex?N+1"> values <img src="https://latex.codecogs.com/png.latex?x_0,%20...,%20x_N"> EQUALLY SPACED by <img src="https://latex.codecogs.com/png.latex?h=x_%7Bn+1%7D-x_%7Bn%7D%5C,%20,%5C;%20%5Cforall%5C,%20n=0,...,N-1">, and we define <img src="https://latex.codecogs.com/png.latex?y_n%20:=%20y(x_n)">.</p>
<p>We can then apply the Taylor expansion to <img src="https://latex.codecogs.com/png.latex?y_%7Bn%5Cpm1%7D">, forward (<img src="https://latex.codecogs.com/png.latex?+">) and backward (<img src="https://latex.codecogs.com/png.latex?-">):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_%7Bn%5Cpm1%7D%20=%20y(x_n%20%5Cpm%20h)%20=%0A%20%20%20%20y_n%20%5Cpm%20h%5C,y'_n%20+%20%5Cfrac%7Bh%5E2%7D%7B2!%7D%5C,y''_n%20%5Cpm%20%5Cfrac%7Bh%5E3%7D%7B3!%7D%5C,y'''_n%20%20+%0A%20%20%20%20%5Cfrac%7Bh%5E4%7D%7B4!%7D%5C,y%5E%7B(4)%7D_n%20%5Cpm%20%5Cfrac%7Bh%5E5%7D%7B5!%7D%5C,y%5E%7B(5)%7D_n%20+%20O(h%5E6)%0A%5Ctag%7B2.1%7D%0A"></p>
<p>Add the two expansions. All odd powers of <img src="https://latex.codecogs.com/png.latex?h"> cancel (symmetric stencil), so the first neglected term is <img src="https://latex.codecogs.com/png.latex?O(h%5E6)">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20+%20y_%7Bn-1%7D%20=%202%5Cleft(y_n%20+%20%5Cfrac%7Bh%5E2%7D%7B2!%7D%5C,y''_n%20+%20%5Cfrac%7Bh%5E4%7D%7B4!%7D%5C,y%5E%7B(4)%7D_n%20%5Cright)%20+%20O(h%5E6)%20%5Ctag%7B2.2a%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20=%20-%20y_%7Bn-1%7D%20+%202%5C,y_n%20+%20h%5E2%20%5C,y''_n%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%5C,y%5E%7B(4)%7D_n%20+%20O(h%5E6)%20%5Ctag%7B2.2b%7D%0A"></p>
<p>Defining the right-hand side of Eq.(1.1), which is linear in <img src="https://latex.codecogs.com/png.latex?y">, as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AF%20:=%20U(x)%20+%20V(x)%5C,y%20%5C;%20,%20%5Ctag%7B2.3a%7D%0A"></p>
<p>and on the grid <img src="https://latex.codecogs.com/png.latex?F_n%20:=%20F(x_n)%20=%20U_n%20+%20V_n%5C,y_n">, with <img src="https://latex.codecogs.com/png.latex?U_n%20:=%20U(x_n)"> and <img src="https://latex.codecogs.com/png.latex?V_n%20:=%20V(x_n)">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(1.1)%20%5C;%20%5CRightarrow%20%5C;%20%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2y%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20=%20F%20%5C;%20,%20%5Ctag%7B2.3b%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2y%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20=%20F_n%20%5C;%20,%20%5Cqquad%0A%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E4y%7D%7B%5Cmathrm%7Bd%7Dx%5E4%7D%5Cright%7C_n%20=%20%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5C!%5Cleft(%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2y%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright)%5Cright%7C_n%20=%20%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(2.2b)%20%5C;%20%5CRightarrow%20%5Cquad%20%20y_%7Bn+1%7D%20=%20-%20y_%7Bn-1%7D%20+%202%5C,y_n%20+%20h%5E2F_n%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20+%20O(h%5E6)%20%5Ctag%7B2.4%7D%0A"></p>
<p>Unfortunately, Eq.(2.4) is not yet a usable scheme, we cannot compute <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bd%7D%5E2F/%5Cmathrm%7Bd%7Dx%5E2"> analytically from its definition:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%20%20%0A%20%20%20%20&amp;=%20%5Cfrac%7B%5Cmathrm%7Bd%7D%7D%7B%5Cmathrm%7Bd%7Dx%7D%20%5Cfrac%7B%5Cmathrm%7Bd%7D%7D%7B%5Cmathrm%7Bd%7Dx%7D%0A%20%20%20%20%20%20%20%20%5Cleft%5BU(x)%20+%20V(x)%5C,%20y(x)%20%5Cright%5D%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20%20%5Cfrac%7B%5Cmathrm%7Bd%7D%7D%7B%5Cmathrm%7Bd%7Dx%7D%0A%20%20%20%20%20%20%20%20%5Cleft%5BU'%20+%20V'%5C,y%20+%20V%20%5C,%20y'%5Cright%5D%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20U''%20+%20V''%5C,y%20+%202V'%5C,y'%20+%20V%5C,y''%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20U''%20+%20V''%5C,y%20+%202V'%5C,y'%20+%20V%5C,F%0A%5Cend%7Balign*%7D%0A"></p>
<p>because that requires derivatives of <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?V"> and the slope <img src="https://latex.codecogs.com/png.latex?y'">, which the scheme does not carry.</p>
<p>However, <img src="https://latex.codecogs.com/png.latex?F%20=%20%5Cmathrm%7Bd%7D%5E2y/%5Cmathrm%7Bd%7Dx%5E2"> is itself smooth, so Eq.(2.1)–(2.2b) hold with <img src="https://latex.codecogs.com/png.latex?y%20%5Cto%20F">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0AF_%7Bn+1%7D%20-%202%20F_n%20+%20F_%7Bn-1%7D%20=%20h%5E2%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E4F%7D%7B%5Cmathrm%7Bd%7Dx%5E4%7D%5Cright%7C_n%20+%20O(h%5E6)%20%5Ctag%7B2.5a%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20=%20%5Cfrac%7BF_%7Bn+1%7D%20-%202F_n%20+%20F_%7Bn-1%7D%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5Ctag%7B2.5b%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20=%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_%7Bn+1%7D%20-%202F_n%20+%20F_%7Bn-1%7D%5Cbig)%20+%20O(h%5E6)%20%5Ctag%7B2.5c%7D%0A"></p>
<p>We then substitute Eq.(2.5c) into Eq.(2.4):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A(2.5c)%20%5C;%20&amp;%5Cmathrm%7Binto%7D%20%5C;%20(2.4)%20%5C%5C%5B7pt%5D%0A%5C;%20%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20&amp;=%202%5C,y_n%20-%20y_%7Bn-1%7D%20+%20h%5E2%5C,F_n%20+%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_%7Bn+1%7D%20-%202F_n%20+%20F_%7Bn-1%7D%5Cbig)%20+%20O(h%5E6)%20%5C%5C%5B8pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%202%5C,y_n%20-%20y_%7Bn-1%7D%20+%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_%7Bn+1%7D%20+%20(12%20-%202)%5C,F_n%20+%20F_%7Bn-1%7D%5Cbig)%20+%20O(h%5E6)%20%5C%5C%5B8pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%202%5C,y_n%20-%20y_%7Bn-1%7D%20+%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_%7Bn+1%7D%20+%2010F_n%20+%20F_%7Bn-1%7D%5Cbig)%20+%20O(h%5E6)%0A%5Cend%7Baligned%7D%20%5Ctag%7B2.6%7D%0A"></p>
<p>We didn’t finish yet, because Eq.(2.6) is implicit: <img src="https://latex.codecogs.com/png.latex?F_%7Bn+1%7D%20=%20U_%7Bn+1%7D%20+%20V_%7Bn+1%7D%5C,y_%7Bn+1%7D"> contains the unknown <img src="https://latex.codecogs.com/png.latex?y_%7Bn+1%7D">. Splitting <img src="https://latex.codecogs.com/png.latex?F_%7Bn+1%7D"> according to its definition Eq.(2.3a), and collecting the <img src="https://latex.codecogs.com/png.latex?y_%7Bn+1%7D"> terms on the left:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20&amp;=%202y_n%20-%20y_%7Bn-1%7D%20+%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cleft%5BU_%7Bn+1%7D%20+%20V_%7Bn+1%7D%5C,y_%7Bn+1%7D%20+%2010F_n%20+%20F_%7Bn-1%7D%5Cright%5D%20+%20O(h%5E6)%20%5C%5C%5B8pt%5D%0A&amp;=%202y_n%20-%20y_%7Bn-1%7D%20+%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cleft%5BU_%7Bn+1%7D%20+%2010F_n%20+%20F_%7Bn-1%7D%5Cright%5D+%20%5Cfrac%7BV_%7Bn+1%7Dh%5E2%7D%7B12%7D%5C,y_%7Bn+1%7D%20+%20O(h%5E6)%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cleft(1%20-%20%5Cfrac%7BV_%7Bn+1%7Dh%5E2%7D%7B12%7D%5Cright)%20y_%7Bn+1%7D%20=%20%202y_n%20-%20y_%7Bn-1%7D%20+%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(U_%7Bn+1%7D%20+%2010F_n%20+%20F_%7Bn-1%7D%5Cbig)%20+%20O(h%5E6)%20%5Ctag%7B2.7%7D%0A"></p>
<p>Divide by <img src="https://latex.codecogs.com/png.latex?1%20-%20V_%7Bn+1%7Dh%5E2/12%20=%201%20+%20O(h%5E2)">, which is non-zero for <img src="https://latex.codecogs.com/png.latex?V_%7Bn+1%7Dh%5E2%20%5Cneq%2012"> and leaves the remainder <img src="https://latex.codecogs.com/png.latex?O(h%5E6)">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0Ay_%7Bn+1%7D%20=%20%5Cfrac%7B%0A%20%20%20%202y_n%20-%20y_%7Bn-1%7D%20+%20%5Cdfrac%7Bh%5E2%7D%7B12%7D%5Cbig(U_%7Bn+1%7D%20+%2010F_n%20+%20F_%7Bn-1%7D%5Cbig)%0A%20%20%7D%7B%0A%20%20%20%201%20-%20%5Cdfrac%7Bh%5E2%20%5C,V_%7Bn+1%7D%7D%7B12%7D%0A%20%20%7D%20+%20O(h%5E6)%7D%20%5Ctag%7B2.8%7D%0A"></p>
<ul>
<li>The right-hand side needs only <img src="https://latex.codecogs.com/png.latex?y_n">, <img src="https://latex.codecogs.com/png.latex?y_%7Bn-1%7D"> (hence <img src="https://latex.codecogs.com/png.latex?F_n">, <img src="https://latex.codecogs.com/png.latex?F_%7Bn-1%7D">, already computed in the previous steps) and <img src="https://latex.codecogs.com/png.latex?U_%7Bn+1%7D">, <img src="https://latex.codecogs.com/png.latex?V_%7Bn+1%7D">: one evaluation of <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?V"> per step, for a local error <img src="https://latex.codecogs.com/png.latex?O(h%5E6)">.</li>
<li>Linearity in <img src="https://latex.codecogs.com/png.latex?y"> is used only in this step: for a general <img src="https://latex.codecogs.com/png.latex?F(x,%20y)">, Eq.(2.6) is a nonlinear equation for <img src="https://latex.codecogs.com/png.latex?y_%7Bn+1%7D"> (root finding at every step).</li>
<li>Eq. (2.8) is a two-step recursion: it needs <img src="https://latex.codecogs.com/png.latex?y_0"> and <img src="https://latex.codecogs.com/png.latex?y_1"> to start, which is the subject of Section 3 (the paper’s Eqs. (8)–(15)).</li>
</ul>
<section id="leading-local-error-term" class="level3">
<h3 class="anchored" data-anchor-id="leading-local-error-term">2.1 Leading local error term</h3>
<p>We can make one step further to get the leading error term of Eq.(2.8). Expanding Eq.(2.1) and Eq.(2.5a) two orders further,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%20%5Ctag%7B2.9a%7D%0Ay_%7Bn%5Cpm1%7D%20=%20y(x_n%20%5Cpm%20h)%20=%20y_n%20&amp;%5Cpm%20%20%20h%5C,y'_n%20+%20%5Cfrac%7Bh%5E2%7D%7B2!%7D%5C,y''_n%0A%20%20%20%20%5Cpm%20%5Cfrac%7Bh%5E3%7D%7B3!%7D%5C,y'''_n%20%20+%20%5Cfrac%7Bh%5E4%7D%7B4!%7D%5C,y%5E%7B(4)%7D_n%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;%5Cpm%20%5Cfrac%7Bh%5E5%7D%7B5!%7D%5C,y%5E%7B(5)%7D_n%20+%20%5Cfrac%7Bh%5E6%7D%7B6!%7D%5C,y%5E%7B(6)%7D_n%0A%20%20%20%20%5Cpm%20%5Cfrac%7Bh%5E7%7D%7B7!%7D%5C,y%5E%7B(7)%7D_n%20+%20O(h%5E8)%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20+%20y_%7Bn-1%7D%20=%202%5Cleft(y_n%20+%20%5Cfrac%7Bh%5E2%7D%7B2!%7D%5C,y''_n%20+%20%5Cfrac%7Bh%5E4%7D%7B4!%7D%5C,y%5E%7B(4)%7D_n%20+%0A%5Cfrac%7Bh%5E6%7D%7B6!%7D%5C,y%5E%7B(6)%7D_n%20%5Cright)%20+%20O(h%5E8)%20%5Ctag%7B2.9b%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20-%202%5C,y_n%20+%20y_%7Bn-1%7D%20=%20h%5E2%20%5C,y''_n%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%5C,y%5E%7B(4)%7D_n%0A+%20%5Cfrac%7Bh%5E6%7D%7B360%7D%5C,y%5E%7B(6)%7D_n%20+%20O(h%5E8)%20%5Ctag%7B2.9c%7D%0A"></p>
<p>using the definition of <img src="https://latex.codecogs.com/png.latex?F"> in Eq.(2.3b),</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_%7Bn+1%7D%20-%202y_n%20+%20y_%7Bn-1%7D%20=%20h%5E2F_n%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20+%20%5Cfrac%7Bh%5E6%7D%7B360%7D%5C,y%5E%7B(6)%7D_n%20+%20O(h%5E8)%0A%5Ctag%7B2.10%7D%0A"></p>
<p>rederiving Eq.(2.5c) from Eq.(2.5a) but keeping one step further,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(2.5a)%20%5C;%20:%20%5Cquad%0AF_%7Bn+1%7D%20-%202%20F_n%20+%20F_%7Bn-1%7D%20=%20h%5E2%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E4F%7D%7B%5Cmathrm%7Bd%7Dx%5E4%7D%5Cright%7C_n%20+%20O(h%5E6)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20=%20%5Cfrac%7BF_%7Bn+1%7D%20-%202F_n%20+%20F_%7Bn-1%7D%7D%7Bh%5E2%7D%20-%0A%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E4F%7D%7B%5Cmathrm%7Bd%7Dx%5E4%7D%5Cright%7C_n+%20O(h%5E4)%0A%5Ctag%7B2.11a%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20%5Cquad%0A%5Cfrac%7Bh%5E4%7D%7B12%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E2F%7D%7B%5Cmathrm%7Bd%7Dx%5E2%7D%5Cright%7C_n%20&amp;=%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_%7Bn+1%7D%20-%202F_n%20+%20F_%7Bn-1%7D%5Cbig)%20-%20%5Cfrac%7Bh%5E4%7D%7B144%7D%5Cleft.%20%5Cfrac%7B%5Cmathrm%7Bd%7D%5E4F%7D%7B%5Cmathrm%7Bd%7Dx%5E4%7D%5Cright%7C_n%20+%20O(h%5E8)%20%5C%5C%5B10pt%5D%0A&amp;=%20%5Cfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_%7Bn+1%7D%20-%202F_n%20+%20F_%7Bn-1%7D%5Cbig)%20-%20%5Cfrac%7Bh%5E6%7D%7B144%7D%5C,y%5E%7B(6)%7D_n%20+%20O(h%5E8)%0A%5Ctag%7B2.11b%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>and comparing Eq.(2.11b) with (2.9c) we get that the <img src="https://latex.codecogs.com/png.latex?O(h%5E6)"> in Eq.(2.6) and Eq.(2.8) is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AO(h%5E6)%5C;%20%5Cmathrm%7Bin%20%5C;%20Eq.(2.6-2.8)%7D%20=%20%5Cleft(%5Cfrac%7B1%7D%7B360%7D%20-%20%5Cfrac%7B1%7D%7B144%7D%5Cright)%20h%5E6%5C,y%5E%7B(6)%7D_n%20+%20O(h%5E8)%20=%20-%5Cfrac%7Bh%5E6%7D%7B240%7D%5C,y%5E%7B(6)%7D_n%20+%20O(h%5E8)%0A"></p>
</section>
<section id="global-error" class="level3">
<h3 class="anchored" data-anchor-id="global-error">2.2 Global Error</h3>
<p>At first glance, one could say that the global error of Numerov’s method is <img src="https://latex.codecogs.com/png.latex?O(h%5E5)">:</p>
<ul>
<li>to reach a fixed <img src="https://latex.codecogs.com/png.latex?x_n">, you take <img src="https://latex.codecogs.com/png.latex?n"> steps;</li>
<li>if each step adds an error <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cpropto%20h%5E6">,</li>
<li>and if the errors just pile up over all the <img src="https://latex.codecogs.com/png.latex?N%20%5Cpropto%201/h"> points,</li>
<li>the total error is <img src="https://latex.codecogs.com/png.latex?N%20%5Ccdot%20%5Ctau%20%20%5Cpropto%20%20(1/h)%20%5Ccdot%20h%5E6%20%20=%20%20h%5E5"></li>
</ul>
<p>This counting rule works for one-step methods for first-order ODEs, like Euler or Runge–Kutta. There the step error lands directly on the value <img src="https://latex.codecogs.com/png.latex?y">, so the global error is the local error divided by <img src="https://latex.codecogs.com/png.latex?h">.</p>
<p>However, Numerov is a two-step recursion for a second-order equation. Its local error doesn’t land on <img src="https://latex.codecogs.com/png.latex?y_n">, but on the second difference, the discrete <img src="https://latex.codecogs.com/png.latex?y''">. It’s like an error in acceleration rather than in position, and such an error in acceleration gets integrated twice before it shows up in position. In other words, one factor of h is lost per “integration”: <img src="https://latex.codecogs.com/png.latex?h%5E6"> becomes <img src="https://latex.codecogs.com/png.latex?h%5E5"> for the slope, then <img src="https://latex.codecogs.com/png.latex?h%5E4"> for the value.</p>
<p>Redoing the original argument to get Numerov’s method global error s <img src="https://latex.codecogs.com/png.latex?O(h%5E4)"> :</p>
<ul>
<li>to reach a fixed <img src="https://latex.codecogs.com/png.latex?x_n">, you take <img src="https://latex.codecogs.com/png.latex?n"> steps;</li>
<li>if each step adds an error <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cpropto%20h%5E6">,</li>
<li>and if the errors influence quadratically over all the <img src="https://latex.codecogs.com/png.latex?N%20%5Cpropto%201/h"> points</li>
<li>the total error is <img src="https://latex.codecogs.com/png.latex?N%5E2%20%5Ccdot%20%5Ctau%20%20%5Cpropto%20%20(1/h%5E2)%20%5Ccdot%20h%5E6%20%20=%20%20h%5E4"></li>
</ul>
</section>
</section>
<section id="initial-conditions" class="level2">
<h2 class="anchored" data-anchor-id="initial-conditions">3. Initial conditions</h2>
<p>Eq.(2.8) is a two-step recursion: to compute <img src="https://latex.codecogs.com/png.latex?y_%7Bn+1%7D"> we need the two previous values <img src="https://latex.codecogs.com/png.latex?y_n"> and <img src="https://latex.codecogs.com/png.latex?y_%7Bn-1%7D">. Tha means, to start the algorithm we need both <img src="https://latex.codecogs.com/png.latex?y_0"> and <img src="https://latex.codecogs.com/png.latex?y_1">.</p>
<p><strong>Assumption</strong>: As initial conditions we know the value <img src="https://latex.codecogs.com/png.latex?y_0%20=%20y(x_0)">, which gives <img src="https://latex.codecogs.com/png.latex?F_0%20=%20U_0%20+%20V_0%5C,y_0">, and the slope <img src="https://latex.codecogs.com/png.latex?y'_0%20=%20y'(x_0)">. Without loss of generality, <img src="https://latex.codecogs.com/png.latex?x_0%20=%200">.</p>
<p>How accurate must <img src="https://latex.codecogs.com/png.latex?y_1"> be? To compute <img src="https://latex.codecogs.com/png.latex?y_2"> at <img src="https://latex.codecogs.com/png.latex?O(h%5E6)"> with Eq.(2.8), we would need <img src="https://latex.codecogs.com/png.latex?y_1"> with the same accuracy. However, it is sufficient to compute <img src="https://latex.codecogs.com/png.latex?y_1"> with a precision <img src="https://latex.codecogs.com/png.latex?O(h%5E5)">: <img src="https://latex.codecogs.com/png.latex?y_1"> is computed only once, and a single error, injected once, grows only linearly, which means that its globally propagated error <img src="https://latex.codecogs.com/png.latex?N%20%5Ccdot%20O(h%5E5)%20=%20O(h%5E4)"> would be still of the same order of the global error.</p>
<p>The best we can do from scratch, without any analytical differentiation, is to Taylor-expand <img src="https://latex.codecogs.com/png.latex?y_1"> about <img src="https://latex.codecogs.com/png.latex?x_0">, using <img src="https://latex.codecogs.com/png.latex?y''%20=%20F">, <img src="https://latex.codecogs.com/png.latex?y'''%20=%20F'"> and <img src="https://latex.codecogs.com/png.latex?y%5E%7B(4)%7D%20=%20F''">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_1%20=%20y_0%20+%20h%5C,y'_0%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%5C,F_0%20+%20%5Cfrac%7Bh%5E3%7D%7B3!%7D%5C,F'_0%20+%20%5Cfrac%7Bh%5E4%7D%7B4!%7D%5C,F''_0%20+%20O(h%5E5)%20%5Ctag%7B3.1%7D%0A"></p>
<p>The problem is then: how to estimate <img src="https://latex.codecogs.com/png.latex?F'_0"> and <img src="https://latex.codecogs.com/png.latex?F_0''">?</p>
<section id="a-naive-first-step" class="level3">
<h3 class="anchored" data-anchor-id="a-naive-first-step">3.1 A naive first step</h3>
<p>A first attempt is to replace <img src="https://latex.codecogs.com/png.latex?F'_0"> with the forward difference and to drop the <img src="https://latex.codecogs.com/png.latex?F_0''"> term by including it into the error (which goes from <img src="https://latex.codecogs.com/png.latex?O(h%5E5)"> to <img src="https://latex.codecogs.com/png.latex?O(h%5E4)">):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AF'_0%20=%20%5Cfrac%7BF_1%20-%20F_0%7D%7Bh%7D%20+%20O(h)%20%5Ctag%7B3.2%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(3.2)%20%5C;%20%5Cmathrm%7Binto%7D%20%5C;%20&amp;(3.1)%5C;%20%5Cmathrm%7B+%20%5C;removing%20%5C;%7DF_0''%20%5C%20%5C%5C%5B8pt%5D%0A%5CRightarrow%20%5Cquad%0Ay_1%20&amp;=%20y_0%20+%20h%5C,y'_0%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%5C,F_0%20+%20%5Cfrac%7Bh%5E3%7D%7B6%7D%5Cleft%5B%5Cfrac%7BF_1%20-%20F_0%7D%7Bh%7D%20+%20O(h)%5Cright%5D%20+%0A%20%20%20%20%5Ccancel%7B%5Cfrac%7Bh%5E4%7D%7B4!%7D%5C,F''_0%7D%20+%20O(h%5E%7B%5Ccancel%7B5%7D%5C,4%7D)%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20y_0%20+%20h%5C,y'_0%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%5C,F_0%20+%20%5Cfrac%7Bh%5E2%7D%7B6%7D%5Cbig(F_1%20-%20F_0%5Cbig)%20+%20O(h%5E4)%20+%0A%20%20%20%20+%20O(h%5E4)%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20y_0%20+%20h%5C,y'_0%20+%20%5Cfrac%7Bh%5E2%7D%7B6%7D%5Cbig(F_1%20+%202F_0%5Cbig)%20+%20O(h%5E4)%0A%5Cend%7Balign*%7D%0A"></p>
<p>As in Eq.(2.6), <img src="https://latex.codecogs.com/png.latex?F_1%20=%20U_1%20+%20V_1%5C,y_1"> contains the unknown <img src="https://latex.codecogs.com/png.latex?y_1">, and we collect it on the left:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20%5Cquad%0Ay_1%20&amp;=%20y_0%20+%20%20h%5C,y_0'%20+%20%5Cfrac%7Bh%5E2%7D%7B6%7D(U_1%20+%20V_1%5C,y_1%20+%202%20F_0)+%20O(h%5E4)%20%5C%5C%5B8pt%5D%0A&amp;=%20y_0%20+%20%20h%5C,y_0'%20+%20%5Cfrac%7Bh%5E2%7D%7B6%7D(U_1%20+%202%20F_0%20)%20+%20%5Cfrac%7BV_1%5C,h%5E2%7D%7B6%7D%5C,y_1%20+%20O(h%5E4)%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0A%5Cleft(1%20-%20%5Cfrac%7BV_1%5C,h%5E2%7D%7B6%7D%5Cright)%20y_1%20=%20%20y_0%20+%20%20h%5C,y_0'%20+%20%5Cfrac%7Bh%5E2%7D%7B6%7D%5Cleft(U_1%20+%202F_0%20%5Cright)%20+%20O(h%5E4)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_1%20=%20%5Cfrac%7B%0A%20%20%20%20y_0%20+%20h%5C,y'_0%20+%20%5Cdfrac%7Bh%5E2%7D%7B6%7D%5Cbig(U_1%20+%202F_0%5Cbig)%0A%20%20%7D%7B%0A%20%20%20%201%20-%20%5Cdfrac%7Bh%5E2%5C,V_1%7D%7B6%7D%0A%20%20%7D%20+%20O(h%5E4)%20%5Ctag%7B3.3%7D%0A"></p>
<p>Eq.(3.3) is one order short: <img src="https://latex.codecogs.com/png.latex?y_1"> at <img src="https://latex.codecogs.com/png.latex?O(h%5E4)"> costs a factor <img src="https://latex.codecogs.com/png.latex?h"> in the global error, which becomes <img src="https://latex.codecogs.com/png.latex?O(h%5E3)">. In the literature, three main alternatives can be found:</p>
<ol type="1">
<li>compute <img src="https://latex.codecogs.com/png.latex?F''"> analytically,which is not always practical;</li>
<li>compute <img src="https://latex.codecogs.com/png.latex?y_1"> from Eq.(3.1) without <img src="https://latex.codecogs.com/png.latex?F_0'"> and <img src="https://latex.codecogs.com/png.latex?F_0''">, get a first estimate of <img src="https://latex.codecogs.com/png.latex?y_2"> with Eq.(2.8), use it to estimate <img src="https://latex.codecogs.com/png.latex?F_0''">, and iterate until <img src="https://latex.codecogs.com/png.latex?y_1"> and <img src="https://latex.codecogs.com/png.latex?y_2"> stabilise;</li>
<li>perform the first step with a different self-starting algorithm (e.g.&nbsp;Runge–Kutta), then switch to Numerov.</li>
</ol>
<p>None of them is necessary.</p>
</section>
<section id="the-y_1-starting-formula" class="level3">
<h3 class="anchored" data-anchor-id="the-y_1-starting-formula">3.2 The <img src="https://latex.codecogs.com/png.latex?y_1"> starting formula</h3>
<p>In [1], the authors find a better approach.</p>
<p>Suppose for a moment that we somehow obtained an estimation for <img src="https://latex.codecogs.com/png.latex?y_2">, hence also for <img src="https://latex.codecogs.com/png.latex?F_2=U_2%20+%20V_2%20%5C,%20y_2">. Using <img src="https://latex.codecogs.com/png.latex?F_0">, <img src="https://latex.codecogs.com/png.latex?F_1"> and <img src="https://latex.codecogs.com/png.latex?F_2"> we can estimate, via e.g.&nbsp;forward difference, <img src="https://latex.codecogs.com/png.latex?F_0''">. That means, we look for <img src="https://latex.codecogs.com/png.latex?y_1"> in the form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_1%20=%20y_0%20+%20h%5C,y'_0%20+%20h%5E2%5Cbig(a%5C,F_0%20+%20b%5C,F_1%20+%20c%5C,F_2%5Cbig)%20%5Ctag%7B3.4a%7D%0A"></p>
<p>The constants <img src="https://latex.codecogs.com/png.latex?a">, <img src="https://latex.codecogs.com/png.latex?b">, <img src="https://latex.codecogs.com/png.latex?c"> are fixed by requiring that Eq.(3.4) agrees with Eq.(3.1).</p>
<p>We expand via Taylor series <img src="https://latex.codecogs.com/png.latex?F_1"> and <img src="https://latex.codecogs.com/png.latex?F_2"> about <img src="https://latex.codecogs.com/png.latex?x_0">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0AF_1%20&amp;=%20F(x_0+h)%20=%20F_0%20+%20h%5C,F'_0%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%5C,F''_0%20+%20O(h%5E3)%20%5C%5C%5B8pt%5D%0AF_2%20&amp;=%20F(x_0%20+%202h)%20=%20F_0%20+%202h%5C,F'_0%20+%202h%5E2%5C,F''_0%20+%20O(h%5E3)%0A%5Cend%7Balign*%7D%0A"></p>
<p>which means that</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20%5Cquad%0Ah%5E2&amp;%5Cbig(a%5C,F_0%20+%20b%5C,F_1%20+%20c%5C,F_2%5Cbig)%20=%20%5C%5C%5B6pt%5D%0A&amp;=%20a%5C,h%5E2%20F_0%20+%20b%5C,h%5E2%5Cleft%5BF_0%20+%20h%5C,F'_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B2%7D%5C,F''_0%20+%20O(h%5E3)%5Cright%5D%20+%20%20%5C%5C%5B6pt%5D%0A&amp;%5Cquad%5Cquad%20+%20c%5C,h%5E2%5Cleft%5BF_0%20+%202h%5C,F'_0%20+%202h%5E2%5C,F''_0%20+%20O(h%5E3)%5Cright%5D%5C%5C%5B6pt%5D%0A&amp;=(a%20+%20b%20+%20c)%5C,h%5E2%5C,F_0%20+%20(b%20+%202c)%5C,h%5E3%5C,F'_0%20+%20%5Cleft(%5Ctfrac%7Bb%7D%7B2%7D%20+%202c%5Cright)%20h%5E4%5C,F''_0%20+%20O(h%5E5)%0A%5Cend%7Balign*%7D%0A"></p>
<p>This gives from Eq.(3.1)</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%5Ctag%7B3.4b%7D%0A%5CRightarrow%20%5Cquad%0Ay_1%20&amp;=%20y_0%20+%20h%5C,y'_0%20+%20(a%20+%20b%20+%20c)%5C,h%5E2%5C,F_0%20%20%5C%5C%5B6pt%5D%0A&amp;%5Cquad%5Cquad%20+%20(b%20+%202c)%5C,h%5E3%5C,F'_0%20+%20%5Cleft(%5Ctfrac%7Bb%7D%7B2%7D%20+%202c%5Cright)%20h%5E4%5C,F''_0%20+%20O(h%5E5)%0A%5Cend%7Balign*%7D%0A"></p>
<p>and comparing term by term Eq.(3.4b) with Eq.(3.1):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Aa%20+%20b%20+%20c%20=%20%5Ctfrac%7B1%7D%7B2%7D%20%5C%5C%5B6pt%5D%0Ab%20+%202c%20=%20%5Ctfrac%7B1%7D%7B6%7D%20%5C%5C%5B6pt%5D%0A%5Ctfrac%7Bb%7D%7B2%7D%20+%202c%20=%20%5Ctfrac%7B1%7D%7B24%7D%0A%5Cend%7Bcases%7D%0A%5C;%20%5CRightarrow%20%5C;%0A%5Ctfrac%7Bb%7D%7B2%7D%20=%20%5Ctfrac%7B1%7D%7B6%7D%20-%20%5Ctfrac%7B1%7D%7B24%7D%20=%20%5Ctfrac%7B1%7D%7B8%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cquad%20%5CRightarrow%20%5Cquad%0Aa%20=%20%5Ctfrac%7B7%7D%7B24%7D%20%5C;%20,%5Cquad%20b%20=%20%5Ctfrac%7B1%7D%7B4%7D%20%5C;%20,%20%5Cquad%20c%20=%20-%5Ctfrac%7B1%7D%7B24%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CRightarrow%20%5Cquad%0Ay_1%20=%20y_0%20+%20h%5C,y'_0%20+%20%5Cfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206F_1%20-%20F_2%5Cbig)%20+%20O(h%5E5)%20%5Ctag%7B3.5%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?F_2"> needs <img src="https://latex.codecogs.com/png.latex?y_2">, which is given by the standard Numerov step Eq.(2.8) with <img src="https://latex.codecogs.com/png.latex?n%20=%201">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_2%20=%20%5Cfrac%7B%0A%20%20%20%202y_1%20-%20y_0%20+%20%5Cdfrac%7Bh%5E2%7D%7B12%7D%5Cbig(U_2%20+%2010F_1%20+%20F_0%5Cbig)%0A%20%20%7D%7B%0A%20%20%20%201%20-%20%5Cdfrac%7Bh%5E2%5C,V_2%7D%7B12%7D%0A%20%20%7D%20+%20O(h%5E6)%20%5Ctag%7B3.6%7D%0A"></p>
<p>Eqs.(3.5) and (3.6) are two linear equations in the two unknowns <img src="https://latex.codecogs.com/png.latex?y_1"> and <img src="https://latex.codecogs.com/png.latex?y_2">. Splitting <img src="https://latex.codecogs.com/png.latex?F_1%20=%20U_1%20+%20V_1%5C,y_1"> and <img src="https://latex.codecogs.com/png.latex?F_2%20=%20U_2%20+%20V_2%5C,y_2">, we get</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A&amp;(3.5)%20%5C;%20%5CRightarrow%20%5C;%0Ay_1%20=%20y_0%20+%20h%5C,y'_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20+%206V_1%5C,y_1%20-%20U_2%20-%20V_2%5C,y_2%5Cbig)%20%5C%5C%5B6pt%5D%0A&amp;%5Cquad%5CRightarrow%20%5C;%0A%5Cleft(1%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%5Cright)%20y_1%20+%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%5C,y_2%20=%20y_0%20+%20h%5C,y'_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%20%5C%5C%5B16pt%5D%0A&amp;(3.6)%20%5C;%20%5CRightarrow%20%5C;%0A%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20y_2%20=%202y_1%20-%20y_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B12%7D%5Cbig(U_2%20+%2010U_1%20+%2010V_1%5C,y_1%20+%20F_0%5Cbig)%20%5C%5C%5B8pt%5D%0A&amp;%5Cquad%20%5CRightarrow%20%5C;%0A-%5Cleft(2%20+%20%5Ctfrac%7B5V_1h%5E2%7D%7B6%7D%5Cright)%20y_1%20+%20%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20y_2%20=%20-y_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_0%20+%2010U_1%20+%20U_2%5Cbig)%0A%5Cend%7Balign*%7D%0A"></p>
<p>i.e.&nbsp;we have to solve the linear system</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Aa_%7B11%7D%5C,y_1%20+%20a_%7B12%7D%5C,y_2%20=%20b_1%20%5C%5C%5B4pt%5D%0Aa_%7B21%7D%5C,y_1%20+%20a_%7B22%7D%5C,y_2%20=%20b_2%0A%5Cend%7Bcases%7D%20%5Ctag%7B3.7%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Aa_%7B11%7D%20&amp;=%201%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%20%5C,%20,%20&amp;%20a_%7B12%7D%20&amp;=%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%20%5C,%20,%20&amp;%20b_1%20&amp;=%20y_0%20+%20h%5C,y'_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%20%5C,%20,%20%5C%5C%5B8pt%5D%0Aa_%7B21%7D%20&amp;=%20-2%20-%20%5Ctfrac%7B5V_1h%5E2%7D%7B6%7D%20%5C,%20,%20&amp;%20a_%7B22%7D%20&amp;=%201%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%20%5C,%20,%20&amp;%20b_2%20&amp;=%20-y_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_0%20+%2010U_1%20+%20U_2%5Cbig)%20%5C,%20.%0A%5Cend%7Balign*%7D%0A"></p>
<p>Solving with Cramer’s rule:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_1%20=%20%5Cfrac%7Ba_%7B22%7D%5C,b_1%20-%20a_%7B12%7D%5C,b_2%7D%7Ba_%7B11%7D%5C,a_%7B22%7D%20-%20a_%7B12%7D%5C,a_%7B21%7D%7D%0A"></p>
<p>where:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Aa_%7B22%7D%5C,&amp;b_1%20-%20a_%7B12%7D%5C,b_2%20=%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20%5Cleft%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20y_0%20+%20h%5C,y'_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%0A%20%20%20%20%20%20%20%20%5Cright%5D%20%20%5C%5C%5B6pt%5D%0A%20%20%20%20%20%20%20%20&amp;%5Cquad%5Cquad%0A%20%20%20%20%20%20%20%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%20%5Cleft%5B%0A%20%20%20%20%20%20%20%20-y_0%20+%20%5Ctfrac%7Bh%5E2%7D%7B12%7D%5Cbig(F_0%20+%2010U_1%20+%20U_2%5Cbig)%0A%20%20%20%20%20%20%20%20%5Cright%5D%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20y_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%20+%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%5Cright)%0A%20%20%20%20%20%20%20%20+%20h%5C,y'_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%0A%20%20%20%20%20%20%20%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%20%5C%5C%5B8pt%5D%0A%20%20%20%20%20%20%20%20&amp;%5Cquad%5Cquad%20-%20%5Ctfrac%7BV_2h%5E4%7D%7B288%7D%5CBig%5B%5Cbig(7F_0%20+%206U_1%20-%20%5Ccancel%7BU_2%7D%5Cbig)%0A%20%20%20%20%20%20%20%20+%20%5Cbig(F_0%20+%2010U_1%20+%20%5Ccancel%7BU_2%7D%5Cbig)%5CBig%5D%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%20y_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%5Cright)%20+%20h%5C,y'_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;%5Cquad%5Cquad%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%20-%20%5Ctfrac%7BV_2h%5E4%7D%7B36%7D%5Cbig(F_0%20+%202U_1%5Cbig)%20%5C%5C%5B16pt%5D%0Aa_%7B11%7D%5C,&amp;a_%7B22%7D%20-%20a_%7B12%7D%5C,a_%7B21%7D%20=%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%20%5Cleft(1%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%5Cright)%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20+%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%5Cleft(2%20+%20%5Ctfrac%7B5V_1h%5E2%7D%7B6%7D%5Cright)%20%5C%5C%5B8pt%5D%0A%20%20%20%20&amp;=%201%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%20+%20V_1V_2h%5E4%5Cleft(%5Ctfrac%7B1%7D%7B48%7D%20+%20%5Ctfrac%7B5%7D%7B144%7D%5Cright)%20%5C%5C%5B6pt%5D%0A%20%20%20%20&amp;=%201%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%20+%20%5Ctfrac%7BV_1V_2h%5E4%7D%7B18%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>We get then the final expression:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%0Ay_1%20=%20%5Ctfrac%7B%0A%20%20%20%20y_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B24%7D%5Cright)%20+%20h%5C,y'_0%5Cleft(1%20-%20%5Ctfrac%7BV_2h%5E2%7D%7B12%7D%5Cright)%20%5C%5C%0A%20%20%20+%20%5Ctfrac%7Bh%5E2%7D%7B24%7D%5Cbig(7F_0%20+%206U_1%20-%20U_2%5Cbig)%20-%20%5Ctfrac%7Bh%5E4%5C,V_2%7D%7B36%7D%5Cbig(F_0%20+%202U_1%5Cbig)%0A%20%20%7D%7B%0A%20%20%20%201%20-%20%5Ctfrac%7BV_1h%5E2%7D%7B4%7D%20+%20%5Ctfrac%7BV_1V_2h%5E4%7D%7B18%7D%0A%20%20%7D%20+%20O(h%5E5)%7D%20%5Ctag%7B3.8%7D%0A"></p>
<p>Eq.(3.8) gives <img src="https://latex.codecogs.com/png.latex?y_1"> at <img src="https://latex.codecogs.com/png.latex?O(h%5E5)"> from the initial conditions <img src="https://latex.codecogs.com/png.latex?y_0">, <img src="https://latex.codecogs.com/png.latex?y_0'"> and from <img src="https://latex.codecogs.com/png.latex?U">, <img src="https://latex.codecogs.com/png.latex?V"> at <img src="https://latex.codecogs.com/png.latex?x_1"> and <img src="https://latex.codecogs.com/png.latex?x_2"> only: no iteration and no analytical derivatives are needed.</p>
<p>Then, <img src="https://latex.codecogs.com/png.latex?y_2"> follows from Eq.(3.6), and Eq.(2.8) continues for <img src="https://latex.codecogs.com/png.latex?n%20%5Cgeq%202">.</p>
</section>
<section id="numerical-check" class="level3">
<h3 class="anchored" data-anchor-id="numerical-check">3.3 Numerical check</h3>
<p>Test problem: <img src="https://latex.codecogs.com/png.latex?V(x)%20=%20-(1%20+%20x)"> and <img src="https://latex.codecogs.com/png.latex?U(x)%20=%20y''%20-%20V%5C,y"> chosen so that the exact solution is <img src="https://latex.codecogs.com/png.latex?y(x)%20=%20e%5E%7Bx/2%7D%5Ccos(2x)%20+%20x%5E2">, integrated on <img src="https://latex.codecogs.com/png.latex?x%20%5Cin%20%5B0,%202%5D"> with Eq.(2.8). Observed orders <img src="https://latex.codecogs.com/png.latex?p"> of the errors <img src="https://latex.codecogs.com/png.latex?%5Cpropto%20h%5Ep">, for <img src="https://latex.codecogs.com/png.latex?h%20=%200.02,%5C%200.01,%5C%200.005">:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 44%">
<col style="width: 20%">
<col style="width: 34%">
</colgroup>
<thead>
<tr class="header">
<th>starting value <img src="https://latex.codecogs.com/png.latex?y_1"></th>
<th>error on <img src="https://latex.codecogs.com/png.latex?y_1"></th>
<th>global error at <img src="https://latex.codecogs.com/png.latex?x%20=%202"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>paper’s Eq.(10), as printed</td>
<td><img src="https://latex.codecogs.com/png.latex?h%5E2"></td>
<td><img src="https://latex.codecogs.com/png.latex?h%5E1"></td>
</tr>
<tr class="even">
<td>Eq.(3.3), sign corrected</td>
<td><img src="https://latex.codecogs.com/png.latex?h%5E4"></td>
<td><img src="https://latex.codecogs.com/png.latex?h%5E3"></td>
</tr>
<tr class="odd">
<td>Eq.(3.8)</td>
<td><img src="https://latex.codecogs.com/png.latex?h%5E5"></td>
<td><img src="https://latex.codecogs.com/png.latex?h%5E4"></td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="see-the-orders-for-yourself" class="level2">
<h2 class="anchored" data-anchor-id="see-the-orders-for-yourself">4. See the orders for yourself</h2>
<p>The table above is really a set of claims about how fast the error shrinks as the grid is refined. Here they are, live — the method runs in your browser. Pick a test problem, choose how the first step <img src="https://latex.codecogs.com/png.latex?y_1"> is seeded, and slide the number of intervals <img src="https://latex.codecogs.com/png.latex?N"> (so the step is <img src="https://latex.codecogs.com/png.latex?h%20=%20(x_N%20-%20x_0)/N">). The orange dots are the Numerov solution, the grey line the exact one.</p>
<div class="controls">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="566" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 565;"><span id="cb1-566">viewof prob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">entries</span>(problems)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(([k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> v]) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> [v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> k]))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Problem"</span>})</span>
<span id="cb1-567">viewof starter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radio</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Map</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">entries</span>(starters)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(([k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> v]) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> [v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> k]))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eq38"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seed y₁ with"</span>})</span>
<span id="cb1-568">viewof N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Intervals N"</span>})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-3" data-nodetype="declaration">

</div>
</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="576" data-source-offset="-241" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 575;"><span id="cb2-576">problems <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ({</span>
<span id="cb2-577">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">post</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb2-578">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post's test problem"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-579">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y0p</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-580">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">V</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-581">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exact</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-582">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>    x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-583">  }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-584">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">osc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb2-585">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oscillator  y'' = -y"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-586">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">PI</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y0p</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-587">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">V</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-588">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exact</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(x)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-589">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>    x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(x)</span>
<span id="cb2-590">  }</span>
<span id="cb2-591">})</span>
<span id="cb2-592">Uof <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">d2</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exact</span>(x)</span>
<span id="cb2-593"></span>
<span id="cb2-594"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// ---- three ways to seed y1, of increasing order (Sections 3.1-3.2) ----</span></span>
<span id="cb2-595">starters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ({</span>
<span id="cb2-596">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">naive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"naive: y₁ = y₀ + h y₀′"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-597">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0p</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-598">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">eq33</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Eq. (3.3)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-599">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-600">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-601">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> F0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(x1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> V1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(x1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-602">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0p</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (U1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> F0)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> V1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-603">    } }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-604">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">eq38</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Eq. (3.8)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-605">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-606">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-607">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> F0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(x1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(x2)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> V1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(x1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> V2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(x2)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-608">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> V2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0p</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> V2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb2-609">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> F0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> U1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> U2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> V2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (F0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> U1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-610">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> den <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> V1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> V1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> V2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-611">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> den<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-612">    } }</span>
<span id="cb2-613">})</span>
<span id="cb2-614"></span>
<span id="cb2-615"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// ---- the Numerov recursion, Eq. (2.8) ----</span></span>
<span id="cb2-616"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integrate</span>(p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N) {</span>
<span id="cb2-617">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> V <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> xf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-618">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Array</span>(N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-619">  y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-620">  y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> starters[key]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">y1</span>(p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> h)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-621">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>) {</span>
<span id="cb2-622">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> Fn  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n))     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n))     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y[n]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-623">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> Fnm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y[n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-624">    y[n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y[n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y[n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">U</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Fn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Fnm))</span>
<span id="cb2-625">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-626">  }</span>
<span id="cb2-627">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> xf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-628">}</span>
<span id="cb2-629"></span>
<span id="cb2-630"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">errorOf</span>(p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N) {</span>
<span id="cb2-631">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> xf } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integrate</span>(p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-632">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> maxerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> maxabs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-633">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>) {</span>
<span id="cb2-634">    maxerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(maxerr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(y[n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exact</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n))))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-635">    maxabs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(maxabs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exact</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n))))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-636">  }</span>
<span id="cb2-637">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">abs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> maxerr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> maxerr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> maxabs }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-638">}</span>
<span id="cb2-639"></span>
<span id="cb2-640">P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> problems[prob]</span>
<span id="cb2-641">U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Uof</span>(P)</span>
<span id="cb2-642">run <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integrate</span>(P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> starter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N)</span>
<span id="cb2-643">err <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">errorOf</span>(P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> starter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> N)</span>
<span id="cb2-644">pObs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(err<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">abs</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">errorOf</span>(P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> starter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">abs</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-5" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-6" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-7" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-8" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-9" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-10" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb3" data-startfrom="647" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 646;"><span id="cb3-647"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div class="stats"&gt;</span></span>
<span id="cb3-648"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div&gt;&lt;div class="stat__label"&gt;Step h&lt;/div&gt;</span></span>
<span id="cb3-649"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>run<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-650"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__note"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> intervals&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-651"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div&gt;&lt;div class="stat__label"&gt;Max absolute error&lt;/div&gt;</span></span>
<span id="cb3-652"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>err<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">abs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toExponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-653"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div&gt;&lt;div class="stat__label"&gt;Max relative error&lt;/div&gt;</span></span>
<span id="cb3-654"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>err<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toExponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-655"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__note"&gt;in the max norm&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-656"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div&gt;&lt;div class="stat__label"&gt;Observed order p&lt;/div&gt;</span></span>
<span id="cb3-657"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__value"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">isFinite</span>(pObs) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> pObs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"—"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb3-658"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">       &lt;div class="stat__note"&gt;error ∝ hᵖ, from N vs 2N&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb3-659"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-3" data-nodetype="expression">

</div>
</div>
</div>
<div class="chart">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb4" data-startfrom="664" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 663;"><span id="cb4-664"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div class="chart-legend"&gt;</span></span>
<span id="cb4-665"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;span&gt;&lt;i style="color:var(--site-muted)"&gt;&lt;/i&gt;exact&lt;/span&gt;</span></span>
<span id="cb4-666"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;span&gt;&lt;i style="color:var(--chart-highlight)"&gt;&lt;/i&gt;Numerov&lt;/span&gt;</span></span>
<span id="cb4-667"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-4" data-nodetype="expression">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb5" data-startfrom="672" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 671;"><span id="cb5-672">dataA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb5-673">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { xf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> run<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> step <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">220</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-674">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> step) pts<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">num</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> y[n]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exact</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exact</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xf</span>(n)) })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-675">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> fine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>) { <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> fine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push</span>({ x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exact</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exact</span>(x) })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> }</span>
<span id="cb5-676">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> { pts<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> fine }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-677">}</span>
<span id="cb5-678">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb5-679">  width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginTop</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb5-680">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x →"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">nice</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb5-681">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"↑ y"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb5-682">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb5-683">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">line</span>(dataA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fine</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exact"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-muted)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb5-684">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot</span>(dataA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"num"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-highlight)"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb5-685">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tip</span>(dataA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pointerX</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"num"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>tipStyleN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb5-686">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">title</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`x = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Numerov </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">num</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">exact   </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exact</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">|error| </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">num</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exact</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toExponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> }))</span>
<span id="cb5-687">  ]</span>
<span id="cb5-688">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-2" data-nodetype="expression">

</div>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb6" data-startfrom="689" data-source-offset="-911" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 688;"><span id="cb6-689">tipStyleN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-bg)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--site-muted)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> })</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-3" data-nodetype="declaration">

</div>
</div>
</div>
</div>
</div>
<p>The real payoff is the next plot. It runs each of the three starting formulas over a whole range of step sizes and plots the error against <img src="https://latex.codecogs.com/png.latex?h"> on log–log axes, where a method of order <img src="https://latex.codecogs.com/png.latex?p"> shows up as a straight line of slope <img src="https://latex.codecogs.com/png.latex?p">. The naive first step drags the whole scheme down to first order; Eq. (3.3) gives third order; only Eq. (3.8) keeps the full fourth order the recursion is capable of — the three claims of the table. At the smallest steps the fourth-order line bends and flattens: that is rounding error, the subject of <a href="../../blog/floating-point-rounding-error/index.html">another post</a>, hitting its floor — not the method failing.</p>
<div class="chart">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb7" data-startfrom="697" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 696;"><span id="cb7-697">conv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb7-698">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> Ns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">512</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4096</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-699">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> key <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keys</span>(starters))</span>
<span id="cb7-700">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> Ns)</span>
<span id="cb7-701">      rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">err</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">errorOf</span>(P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> U<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> n)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">abs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-18</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">starter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> starters[key]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-702">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-703">}</span>
<span id="cb7-704">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb7-705">  width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginTop</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginBottom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">44</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-706">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"step h →  (coarse → fine)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">reverse</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-707">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"↑ max absolute error"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-708">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">color</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Object</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(starters)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-709">           <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">range</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-2)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-4)"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var(--chart-6)"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">legend</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-710">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb7-711">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">line</span>(conv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"h"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"err"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"starter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">strokeWidth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marker</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"circle"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-712">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tip</span>(conv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pointer</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"h"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"err"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"starter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>tipStyleN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-713">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">title</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">starter</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">h = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toExponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">error </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">err</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toExponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> }))</span>
<span id="cb7-714">  ]</span>
<span id="cb7-715">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-6-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-6-2" data-nodetype="expression">

</div>
</div>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>math</category>
  <category>physics</category>
  <guid>https://foglienimatteo.com/blog/numerov-method/</guid>
  <pubDate>Wed, 14 Oct 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Basics of Numerical Analysis</title>
  <link>https://foglienimatteo.com/blog/basics-numerical-analysis/</link>
  <description><![CDATA[ 





<p>References:</p>
<p>[1] Wikipedia page <a href="https://en.wikipedia.org/wiki/Finite_difference">“Finite Difference”</a> [2] Wikipedia page <a href="https://en.wikipedia.org/wiki/Finite_difference_coefficient">“Finite difference coefficient”</a></p>
<section id="tldr" class="level2">
<h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<p>We summarise the important equations derived in this article, for a function <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20f:%5Cmathbb%7BR%7D%20%5Cto%20%5Cmathbb%7BR%7D%20%7D"> that is <img src="https://latex.codecogs.com/png.latex?%7BN%20%5Cgeq%203%7D"> times differentiable:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20=%20%5Cfrac%7B%5Cdelta_h%5Bf%5D(x)%7D%7Bh%7D%20+%20O(h%5E2)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Cdelta_h%5Bf%5D(x)%20:=%20f(x+%5Ctfrac%7Bh%7D%7B2%7D)%20-%20f(x-%5Ctfrac%7Bh%7D%7B2%7D)%20%20%5Ctag%7B2.4a%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af''(x)%20=%20%5Cfrac%7B%5Cdelta%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Cdelta%5E2_h%5Bf%5D(x)%20=%20f(x+h)%20-%202%5C,f(x)%20+%20f(x-h)%20%5Ctag%7B3.1a%7D%0A"></p>
<p>Rewritten using the index notation <img src="https://latex.codecogs.com/png.latex?x_n"> and <img src="https://latex.codecogs.com/png.latex?f_n%20=%20f(x_n)"> and assuming that all the elements are equally spaced by <img src="https://latex.codecogs.com/png.latex?h=x_%7Bn+1%7D-x_%7Bn%7D%5C,%20,%5C;%20%5Cforall%5C,%20n=0,...,N-1">, for the first derivative:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af_%7Bn%7D'%20=%20%5Cbegin%7Bcases%7D%5Ctag%7B2.5a-c%7D%0A%5Cdisplaystyle%20%5Cfrac%7Bf_%7Bn+1%7D%20-%20f_%7Bn-1%7D%7D%7B2h%7D%20+%20O(h%5E2)%20%5C%5C%5B10pt%5D%0A%5Cdisplaystyle%20%5Cfrac%7Bf_%7Bn+1%7D%20-%20f_n%7D%7Bh%7D%20%20+%20O(h)%20%20%5C%5C%5B10pt%5D%0A%5Cdisplaystyle%20%5Cfrac%7Bf_%7Bn%7D%20-%20f_%7Bn-1%7D%7D%7Bh%7D%20%20+%20O(h)%20%20%5C%5C%5B10pt%5D%0A%5Cend%7Bcases%7D%0A"></p>
<p>and for the second:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af_%7Bn%7D''%20=%20%5Cbegin%7Bcases%7D%5Ctag%7B3.2a-c%7D%0A%5Cdisplaystyle%20%5Cfrac%7Bf_%7Bn+1%7D%20-%202f_n%20+%20f_%7Bn-1%7D%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5C%5C%5B10pt%5D%0A%5Cdisplaystyle%20%5Cfrac%7Bf_%7Bn+2%7D%20-%202f_%7Bn+1%7D%20+%20f_%7Bn%7D%20%7D%7Bh%5E2%7D%20+%20O(h)%20%20%5C%5C%5B10pt%5D%0A%5Cdisplaystyle%20%5Cfrac%7Bf_%7Bn%7D%20-%202f_%7Bn-1%7D%20+%20f_%7Bn-2%7D%20%7D%7Bh%5E2%7D%20+%20O(h)%20%20%5C%5C%5B10pt%5D%0A%5Cend%7Bcases%7D%0A"></p>
</section>
<section id="reminder-big-o-and-little-o-notation" class="level2">
<h2 class="anchored" data-anchor-id="reminder-big-o-and-little-o-notation">Reminder: big-<img src="https://latex.codecogs.com/png.latex?o"> and little-<img src="https://latex.codecogs.com/png.latex?o"> notation</h2>
<p>We remind the definitions of big-<img src="https://latex.codecogs.com/png.latex?O"> and little-<img src="https://latex.codecogs.com/png.latex?o">. In this article, we focus only on real numbers with the limit <img src="https://latex.codecogs.com/png.latex?%5Crightarrow%200">, so we will show their definitions in this context.</p>
<p><strong>Big-<img src="https://latex.codecogs.com/png.latex?O"></strong> Given two real functions <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20f,%20g:%5Cmathbb%7BR%7D%20%5Cto%20%5Cmathbb%7BR%7D%20%7D">, we define <img src="https://latex.codecogs.com/png.latex?f"> as big-<img src="https://latex.codecogs.com/png.latex?O"> of <img src="https://latex.codecogs.com/png.latex?g"> if, in the limit <img src="https://latex.codecogs.com/png.latex?x%20%5Crightarrow%200">, there exists a positive real number <img src="https://latex.codecogs.com/png.latex?M%20%5Cin%20(0,+%5Cinfty)"> such that, for <img src="https://latex.codecogs.com/png.latex?x%20%5Crightarrow%200"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)%20=%20O(g(x))%20%5Cquad%20%5Ciff%20%5Cquad%20%5Cexist%5C,%20M,%20%5Cdelta%20%5Cin(0,+%5Cinfty):%20%5Cquad%20%5Cleft%7C%5Cfrac%7Bf(x)%7D%7Bg(x)%7D%5Cright%7C%20%5Cleq%20M%20%5C;%20,%20%5Cquad%20%5Cforall%5C,%20%200%20%5Cleq%20%7Cx%7C%20%5Cleq%20%5Cdelta%0A"></p>
<p>In simpler words: <em>“<img src="https://latex.codecogs.com/png.latex?O(x)"> is something that, divided by <img src="https://latex.codecogs.com/png.latex?x">, is like a constant <img src="https://latex.codecogs.com/png.latex?c">”.</em></p>
<p><strong>Little-<img src="https://latex.codecogs.com/png.latex?o"></strong> Given two real functions <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20f,%20g:%5Cmathbb%7BR%7D%20%5Cto%20%5Cmathbb%7BR%7D%20%7D">, we define <img src="https://latex.codecogs.com/png.latex?f"> as little-<img src="https://latex.codecogs.com/png.latex?o"> of <img src="https://latex.codecogs.com/png.latex?g"> if, in the limit <img src="https://latex.codecogs.com/png.latex?x%20%5Crightarrow%200">, their ratio goes to zero:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)%20=%20o(g(x))%20%5Cquad%20%5Ciff%20%5Cquad%20%5Clim_%7Bx%20%5Crightarrow%200%7D%20%5Cleft%7C%5Cfrac%7Bf(x)%7D%7Bg(x)%7D%5Cright%7C%20=0%0A"></p>
<p>In simpler words: <em>“<img src="https://latex.codecogs.com/png.latex?o(x)"> is something that, divided by <img src="https://latex.codecogs.com/png.latex?x">, goes to <img src="https://latex.codecogs.com/png.latex?0">”.</em></p>
<p>Their relation is not always trivial: you cannot replace mechanically <img src="https://latex.codecogs.com/png.latex?o(h%5Ep)"> with <img src="https://latex.codecogs.com/png.latex?O(h%5E%7Bp+1%7D)">; e.g., if we consider a quantity <img src="https://latex.codecogs.com/png.latex?h%20%5Crightarrow%200">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ah%5E%7B3/2%7D=o(h),%20%5Cqquad%20h%5E%7B3/2%7D%5Cne%20O(h%5E2).%0A"></p>
<p>The first statement follows because <img src="https://latex.codecogs.com/png.latex?h%5E%7B3/2%7D/h%5Cto0">; the second is false because <img src="https://latex.codecogs.com/png.latex?h%5E%7B3/2%7D/h%5E2%5Cto%5Cinfty">.</p>
<p>For finite differences, however, the leading error is often proportional to integer powers of <img src="https://latex.codecogs.com/png.latex?h"> (like <img src="https://latex.codecogs.com/png.latex?h">, <img src="https://latex.codecogs.com/png.latex?h%5E2">, <img src="https://latex.codecogs.com/png.latex?h%5E3"> etc), so the relation <img src="https://latex.codecogs.com/png.latex?o(h%5Ep)%20=%20O(h%5E%7Bp+1%7D)"> holds in this context.</p>
</section>
<section id="taylors-theorem" class="level2">
<h2 class="anchored" data-anchor-id="taylors-theorem">Taylor’s theorem</h2>
<p><strong>Taylor’s theorem</strong> Given a function <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20f:%5Cmathbb%7BR%7D%20%5Cto%20%5Cmathbb%7BR%7D%20%7D"> that is <img src="https://latex.codecogs.com/png.latex?%7BN%20%5Cgeq%201%7D"> times differentiable at the point <img src="https://latex.codecogs.com/png.latex?%7Ba%5Cin%20%5Cmathbb%20%7BR%7D%20%7D">, there exists a function <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20g_%7BN%7D:%5Cmathbb%20%7BR%7D%20%5Cto%20%5Cmathbb%20%7BR%7D%20%7D"> such that</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)%20=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bf%5E%7B(k)%7D(a)%7D%7Bk!%7D%7D(x-a)%5E%7Bk%7D%20+%20g_%7BN%7D(x)(x-a)%5E%7BN%7D%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Clim%20_%7Bx%5Cto%20a%7Dg_%7BN%7D(x)=0.%0A%5Ctag%7B1.1%7D%0A"></p>
<p>The polynomial appearing in Taylor’s theorem is the <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20%7B%5Cboldsymbol%20%7BN%7D%7D%7D">-th order Taylor polynomial of the function <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20f%7D"> at the point <img src="https://latex.codecogs.com/png.latex?%7B%5Cdisplaystyle%20a%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%7BN%7D(x)%20=%20f(a)+f'(a)(x-a)+%7B%5Cfrac%20%7Bf''(a)%7D%7B2!%7D%7D(x-a)%5E%7B2%7D+%5Ccdots%20+%7B%5Cfrac%20%7Bf%5E%7B(N)%7D(a)%7D%7BN!%7D%7D(x-a)%5E%7BN%7D%0A%5Ctag%7B1.2%7D%0A"></p>
<p>Using the little-<img src="https://latex.codecogs.com/png.latex?o"> and big-<img src="https://latex.codecogs.com/png.latex?O"> notation, the statement in Taylor’s theorem reads as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)%20=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bf%5E%7B(k)%7D(a)%7D%7Bk!%7D%7D(x-a)%5E%7Bk%7D%20+%20o(%5Clvert%20x-a%20%5Crvert%5EN)%0A%5Ctag%7B1.3a%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)%20=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bf%5E%7B(k)%7D(a)%7D%7Bk!%7D%7D(x-a)%5E%7Bk%7D%20+%20O(%5Clvert%20x-a%20%5Crvert%5E%7BN+1%7D)%0A%5Ctag%7B1.3b%7D%0A"></p>
<p>NOTE: the big-<img src="https://latex.codecogs.com/png.latex?O"> notation in (1.3b) has slightly stronger assumptions than the little-<img src="https://latex.codecogs.com/png.latex?o"> one in (1.3a): the former requires that <img src="https://latex.codecogs.com/png.latex?f"> is differentiable <img src="https://latex.codecogs.com/png.latex?N+1"> times at <img src="https://latex.codecogs.com/png.latex?a"> instead of <img src="https://latex.codecogs.com/png.latex?N"> times.</p>
<p>We can rewrite the theorem in a slightly different notation, with the substitutions <img src="https://latex.codecogs.com/png.latex?x%20%5Crightarrow%20x+h"> and <img src="https://latex.codecogs.com/png.latex?a%20%5Crightarrow%20x"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%20%5Ctag%7B1.4a-b%7D%0Af(x+h)%20%20&amp;=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bh%5Ek%7D%7Bk!%7D%7Df%5E%7B(k)%7D(x)%20+%20o(h%5EN)%20&amp;%0Af(x+h)%20%20&amp;=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bh%5Ek%7D%7Bk!%7D%7Df%5E%7B(k)%7D(x)%20+%20O(h%5E%7BN+1%7D)%5C%5C%5B15pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20f(x)%20+%20o(1)%20&amp;%0A%20%20%20%20%20%20%20%20&amp;=%20f(x)%20+%20O(h)%20%5C%5C%5B7pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20f(x)%20+%20h%20%5C,%20f'(x)%20+%20o(h)%20&amp;%0A%20%20%20%20%20%20%20%20&amp;=%20f(x)%20+%20h%20%5C,%20f'(x)%20+%20O(h%5E2)%20%5C%5C%5B7pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20f(x)%20+%20h%20%5C,%20f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f''(x)%20+%20o(h%5E2)%20&amp;%0A%20%20%20%20%20%20%20%20&amp;=%20f(x)%20+%20h%20%5C,%20f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f''(x)%20+%20O(h%5E3)%20%5C%5C%5B7pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20...%20&amp;%0A%20%20%20%20%20%20%20%20&amp;=%20...%0A%5Cend%7Balign*%7D%0A"></p>
<p>Rewritten using the index notation <img src="https://latex.codecogs.com/png.latex?x_n"> and <img src="https://latex.codecogs.com/png.latex?f_n%20=%20f(x_n)"> and assuming that all the elements are equally spaced by <img src="https://latex.codecogs.com/png.latex?h=x_%7Bn+1%7D-x_%7Bn%7D%5C,%20,%5C;%20%5Cforall%5C,%20n=0,...,N-1"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%20%5Ctag%7B1.5%7D%0Af_%7Bn+1%7D%20%20&amp;=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bh%5Ek%7D%7Bk!%7D%7Df%5E%7B(k)%7D_n%20+%20o(h%5EN)%20%5C%5C%5B15pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Csum_%7Bk=0%7D%5E%7BN%7D%7B%5Cfrac%20%7Bh%5Ek%7D%7Bk!%7D%7Df%5E%7B(k)%7D_n%20+%20O(h%5E%7BN+1%7D)%20%5C%5C%5B15pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20f_n%20+%20h%20%5C,%20f_n'%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f_n''%20+%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20f_n'''%20+%20...%20%5C%5C%5B7pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
</section>
<section id="first-order-finite-differences" class="level2">
<h2 class="anchored" data-anchor-id="first-order-finite-differences">First order finite differences</h2>
<p>The derivative of a function <img src="https://latex.codecogs.com/png.latex?f"> at a point <img src="https://latex.codecogs.com/png.latex?x"> is defined by the limit:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20:=%20%5Clim_%7Bh%20%5Crightarrow%200%7D%20%5Cfrac%7Bf(x+h)-f(x)%7D%7Bh%7D%0A%5Ctag%7B2.1a%7D%0A"></p>
<p>More precicely, it is said that the derivative <img src="https://latex.codecogs.com/png.latex?f"> at a point <img src="https://latex.codecogs.com/png.latex?x"> is the limit in (2.1a) if both the right and the limit exists, are finite and coincide, and such value is the limit of (2.1a)</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aa%20:=%20%5Clim_%7Bh%20%5Crightarrow%200%5E%7B+%7D%7D%20%5Cfrac%7Bf(x+h)-f(x)%7D%7Bh%7D%0A%5C;%20,%20%5Cquad%0Ab%20:=%20%5Clim_%7Bh%20%5Crightarrow%200%5E%7B-%7D%7D%20%5Cfrac%7Bf(x+h)-f(x)%7D%7Bh%7D%20%5C;%20,%0A%5Ctag%7B2.1b%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cexist%20f'(x)%20%5Ciff%20a,%20b%20%5Cin%20%5Cmathbb%7BR%7D:%20%5C;%20a=b=:f'(x)%20%5C,%20.%0A"></p>
<p>We define the forward difference <img src="https://latex.codecogs.com/png.latex?%7B%5CDelta_%7Bh%7D%5Bf%5D%7D">, backward difference <img src="https://latex.codecogs.com/png.latex?%7B%5Cnabla_%7Bh%7D%5Bf%5D%7D"> and central difference <img src="https://latex.codecogs.com/png.latex?%7B%5Cdelta_%7Bh%7D%5Bf%5D%7D"> as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%20%20%20%20%5Cmathrm%7BForward%20%20%5C;%20difference:%7D%20%5Cquad%20&amp;%5CDelta_%7Bh%7D%5Bf%5D(x)%20:=%20f(x+h)-f(x)%20%5Ctag%7B2.2a%7D%5C%5C%5B10pt%5D%0A%20%20%20%20%5Cmathrm%7BBackward%20%5C;%20difference:%7D%20%5Cquad%20&amp;%5Cnabla_%7Bh%7D%5Bf%5D(x)%20:=%20f(x)-f(x-h)%20%5Ctag%7B2.2b%7D%5C%5C%5B10pt%5D%0A%20%20%20%20%5Cmathrm%7BCentral%20%20%5C;%20difference:%7D%20%5Cquad%20&amp;%5Cdelta_%7Bh%7D%5Bf%5D(x)%20:=%20f%5Cleft(x+%5Cfrac%7Bh%7D%7B2%7D%5Cright)-f%5Cleft(x-%5Cfrac%7Bh%7D%7B2%7D%5Cright)%20%5Ctag%7B2.2c%7D%5C%5C%5B15pt%5D%0A%5Cend%7Balign%7D%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%20%20%20%20%5CRightarrow%20%5Cquad%20%5Cnabla_%7Bh%7D%5Bf%5D(x)%20&amp;=%20%5CDelta_%7Bh%7D%5Bf%5D(x-h)%20%5Ctag%7B2.3a%7D%5C%5C%5B15pt%5D%0A%20%20%20%20%5CRightarrow%20%5Cquad%20%5Cdelta_%7Bh%7D%5Bf%5D(x)%20&amp;=%20%5CDelta_%7B%5Ctfrac%20%7Bh%7D%7B2%7D%7D%5Bf%5D(x)%20+%20%5Cnabla_%7B%5Ctfrac%20%7Bh%7D%7B2%7D%7D%5Bf%5D(x)%20%5Ctag%7B2.3b%7D%5C%5C%5B15pt%5D%0A%5Cend%7Balign%7D%0A"></p>
<p>We can approximate the derivative of a function (assuming <img src="https://latex.codecogs.com/png.latex?f"> is twice differentiable, three times for centered-difference) using each of the previous three differences. Applying Taylor’s theorem, we can get the error of each approximation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Cfrac%7B%5CDelta_h%5Bf%5D(x)%7D%7Bh%7D%20-%20f'(x)%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7Bf(x+h)%20-%20f(x)%7D%7Bh%7D%20-%20f'(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7B1%7D%7Bh%7D%5Cbigg%5C%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Cleft%5B%5Ccancel%7Bf(x)%7D%20+%20h%20%5C,%20f'(x)%20+%20%5Ctfrac%7Bh%5E2%7D%7B2%7Df''(x)+o(h%5E2)%20%5Cright%5D%20-%20%5Ccancel%7Bf(x)%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Cbigg%5C%7D%20-%20f'(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Ctfrac%7Bh%7D%7B2%7Df''(x)+o(h)%20%5C%5C%5B15pt%5D%20%20%20%20%0A%20%20%20%20%20%20%20%20&amp;=%20O(h)%20%5Cquad%5Cmathrm%7Band%7D%20%5Cquad%20=o(1)%20%5C%5C%5B15pt%5D%20%20%20%20%0A%20%20%20%20%5Cfrac%7B%5Cnabla_h%5Bf%5D(x)%7D%7Bh%7D%20-%20f'(x)%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7Bf(x)%20-%20f(x-h)%7D%7Bh%7D%20-%20f'(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7B1%7D%7Bh%7D%5Cbigg%5C%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Ccancel%7Bf(x)%7D%20-%20%5Cleft%5B%5Ccancel%7Bf(x)%7D%20-%20h%20%5C,%20f'(x)%20+%20%5Ctfrac%7Bh%5E2%7D%7B2%7Df''(x)+o(h%5E2)%20%5Cright%5D%5Cbigg%5C%7D%20-%20f'(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Ctfrac%7Bh%7D%7B2%7Df''(x)+o(h)%20%5C%5C%5B15pt%5D%20%20%20%20%0A%20%20%20%20%20%20%20%20&amp;=%20O(h)%20%5Cquad%5Cmathrm%7Band%7D%20%5Cquad%20=o(1)%20%5C%5C%5B15pt%5D%20%20%20%20%0A%20%20%20%20%5Cfrac%7B%5Cdelta_h%5Bf%5D(x)%7D%7Bh%7D%20-%20f'(x)%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7B1%7D%7Bh%7D%5Cleft%5Bf%5Cleft(x+%5Cfrac%7Bh%7D%7B2%7D%5Cright)%20-%20f%5Cleft(x-%5Cfrac%7Bh%7D%7B2%7D%5Cright)%5Cright%5D%20-%20f'(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7B1%7D%7Bh%7D%5Cbigg%5C%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Cleft%5B%5Ccancel%7Bf(x)%7D%20+%20%5Cfrac%7Bh%7D%7B2%7D%20%5C,%20f'(x)%20+%20%5Ccancel%7B%5Cfrac%7Bh%5E2%7D%7B8%7D%20f''(x)%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20+%20%5Cfrac%7Bh%5E3%7D%7B48%7D%20f'''(x)+%20o(h%5E3)%5Cright%5D-%5C%5C%5B15pt%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20&amp;%5Cquad%5Cquad%5C;%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Cleft%5B%5Ccancel%7Bf(x)%7D%20-%20%5Cfrac%7Bh%7D%7B2%7D%20%5C,%20f'(x)%20+%20%5Ccancel%7B%5Cfrac%7Bh%5E2%7D%7B8%7D%20f''(x)%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20%5Cfrac%7Bh%5E3%7D%7B48%7D%20f'''(x)+%20o(h%5E3)%5Cright%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cbigg%5C%7D%20-%20f'(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Cfrac%7B1%7D%7Bh%7D%20%5Cleft%5B%0A%20%20%20%20%20%20%20%20%20%20%20%202%5C,%5Cfrac%7Bh%7D%7B2%7D%20%5C,%20f'(x)%20+%202%5C,%5Cfrac%7Bh%5E3%7D%7B48%7D%20f'''(x)%20+%20o(h%5E3)%5Cright%5D%20-%20f'(x)%5C%5C%5B15pt%5D%0A%20%20%20%20%20%20%20%20&amp;=%20%5Ctfrac%7Bh%5E2%7D%7B24%7Df'''(x)%20+%20o(h%5E2)%20%5C%5C%5B15pt%5D%20%20%20%20%0A%20%20%20%20%20%20%20%20&amp;=%20O(h%5E2)%20%5Cquad%5Cmathrm%7Band%7D%20%5Cquad%20=%20o(h)%20%5C%5C%5B15pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>To summarize:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Af'(x)%20&amp;=%20%5Cfrac%7B%5Cdelta_h%5Bf%5D(x)%7D%7Bh%7D%20+%20O(h%5E2)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Cdelta_h%5Bf%5D(x)%20:=%20f(x+%5Ctfrac%7Bh%7D%7B2%7D)%20-%20f(x-%5Ctfrac%7Bh%7D%7B2%7D)%20%5Ctag%7B2.4a%7D%20%5C%5C%5B10pt%5D%0Af'(x)%20&amp;=%20%5Cfrac%7B%5CDelta_h%5Bf%5D(x)%7D%7Bh%7D%20+%20O(h)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5CDelta_h%5Bf%5D(x)%20:=%20f(x+h)%20-%20f(x)%20%5Ctag%7B2.4b%7D%5C%5C%5B10pt%5D%0Af'(x)%20&amp;=%20%5Cfrac%7B%5Cnabla_h%5Bf%5D(x)%7D%7Bh%7D%20+%20O(h)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Cnabla_h%5Bf%5D(x)%20:=%20f(x)%20-%20f(x-h)%20%5Ctag%7B2.4c%7D%5C%5C%5B10pt%5D%0A%5Cend%7Balign%7D%0A"></p>
<p>Rewritten using the index notation <img src="https://latex.codecogs.com/png.latex?x_n"> and <img src="https://latex.codecogs.com/png.latex?f_n%20=%20f(x_n)"> and assuming that all the elements are equally spaced by <img src="https://latex.codecogs.com/png.latex?h=x_%7Bn+1%7D-x_%7Bn%7D%5C,%20,%5C;%20%5Cforall%5C,%20n=0,...,N-1"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Af_%7Bn%7D'%20&amp;=%20%5Cfrac%7Bf_%7Bn+1%7D%20-%20f_n%7D%7Bh%7D%20+%20O(h)%20%5Ctag%7B2.5b%7D%5C%5C%5B10pt%5D%0Af_%7Bn%7D'%20&amp;=%20%5Cfrac%7Bf_%7Bn%7D%20-%20f_%7Bn-1%7D%7D%7Bh%7D%20+%20O(h)%20%5Ctag%7B2.5c%7D%20%5C%5C%5B10pt%5D%0A%5Cend%7Balign%7D%0A"></p>
<p>Unfortunately, the central difference has (and will for every odd <img src="https://latex.codecogs.com/png.latex?n">) <img src="https://latex.codecogs.com/png.latex?h"> multiplied by the non-integer <img src="https://latex.codecogs.com/png.latex?1/2">. This is often a problem because it amounts to changing the interval of discretization, but we can remedy by applying the Taylor theorem of Eq.(1.4):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(1.4)%20%5C;%20:%20%5Cbegin%7Bcases%7D%0Af(x%20+%20h)%20=%20f(x)%20+%20h%20%5C,%20f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20%5C,%20f''(x)%20+%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20%5C,%20f'''(x)%20+%20...%20%5C%5C%5B7pt%5D%0Af(x%20-%20h)%20=%20f(x)%20-%20h%20%5C,%20f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20%5C,%20f''(x)%20-%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20%5C,%20f'''(x)%20+%20...%5C%5C%5B10pt%5D%0A%5Cend%7Bcases%7D%0A"></p>
<p>which for the sum leads to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5C,%20%5CRightarrow%20%5C;%0A%5Cfrac%7Bf(x%20+%20h)%20+%20f(x-h)%7D%7B2%7D%20=%20%20f(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20%5C,%20f''(x)%20+%20...%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctag%7B2.6%7D%0A%5CRightarrow%20%5Cquad%20f(x)%20=%20%5Cfrac%7Bf(x+h)%20+%20f(x-h)%7D%7B2%7D%20+%20O(h%5E2)%0A"></p>
<p>and for the difference leads to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5C,%20%5CRightarrow%20%5C;%0A%5Cfrac%7Bf(x%20+%20h)%20-%20f(x-h)%7D%7B2%7D%20=%20%20h%5C,f'(x)%20+%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20%5C,%20f'''(x)%20+%20...%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctag%7B2.7%7D%0A%5CRightarrow%20%5Cquad%20f'(x)%20=%20%5Cfrac%7Bf(x+h)%20-%20f(x-h)%7D%7B2h%7D%20+%20O(h%5E2)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctag%7B2.5a%7D%0A%5CRightarrow%20%5Cquad%20f_%7Bn%7D'%20=%20%5Cfrac%7Bf_%7Bn+1%7D%20-%20f_%7Bn-1%7D%7D%7B2h%7D%20+%20O(h%5E2)%0A"></p>
</section>
<section id="second-order-finite-differences" class="level2">
<h2 class="anchored" data-anchor-id="second-order-finite-differences">Second order finite differences</h2>
<p>In an analogous way, one can obtain finite difference approximations to higher order derivatives and differential operators.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cdisplaystyle%20f(x+h)%20=%0A%20%20%20%20f(x)%20+%20h%5C,f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f''(x)%20+%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20f'''(x)%20+%20%5Cfrac%7Bh%5E4%7D%7B24%7D%20f''''(x)%20+%20O(h%5E5)%20%5C%5C%5B12pt%5D%0A%5Cdisplaystyle%20f(x-h)%20=%0A%20%20%20%20f(x)%20-%20h%5C,f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f''(x)%20-%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20f'''(x)%20+%20%5Cfrac%7Bh%5E4%7D%7B24%7D%20f''''(x)%20+%20O(h%5E5)%0A%5Cend%7Bcases%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20f(x+h)%20+%20f(x-h)%20&amp;=%202%5C,f(x)%20+%20h%5E2%20f''(x)%20%20+%20%5Cfrac%7Bh%5E4%7D%7B12%7D%20f''''(x)%20+%20O(h%5E5)%20%5C%5C%5B12pt%5D%0A%5CRightarrow%20h%5E2%20f''(x)%20&amp;=%20f(x+h)%20-%202%20%5C,%20f(x)+%20f(x-h)%20-%20%5Cfrac%7Bh%5E4%7D%7B12%7D%20f''''(x)%20+%20O(h%5E5)%20%5C%5C%5B12pt%5D%0A%5CRightarrow%20f''(x)%20&amp;=%20%5Cfrac%7Bf(x+h)%20-%202%20%5C,%20f(x)+%20f(x-h)%7D%7Bh%5E2%7D%20-%20%5Cfrac%7Bh%5E2%7D%7B12%7D%20f''''(x)%20+%20O(h%5E3)%20%5C%5C%5B12pt%5D%0A&amp;=%20%5Cfrac%7Bf(x+h)%20-%202%20%5C,%20f(x)+%20f(x-h)%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5C%5C%5B12pt%5D%0A&amp;=%20%5Cfrac%7Bf(x+h)%20-%202%20%5C,%20f(x)+%20f(x-h)%7D%7Bh%5E2%7D%20+%20o(h)%20%5C%5C%5B12pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>Analogously for backward and forward differences:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cdisplaystyle%20f(x+2h)%20=%20f(x)%20+%202%5C,h%5C,f'(x)%20+%202%20%5C,%20h%5E2%20f''(x)%20+%20%5Cfrac%7B4h%5E3%7D%7B3%7D%20f'''(x)%20+%20O(h%5E4)%20%5C%5C%5B12pt%5D%0A%5Cdisplaystyle%20f(x+h)%20=%0A%20%20%20%20f(x)%20+%20h%5C,f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f''(x)%20+%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20f'''(x)%20+%20O(h%5E4)%0A%5Cend%7Bcases%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20f(x+2h)%20-%202%5C,f(x+h)%20&amp;=%20-%5C,f(x)%20+%20h%5E2%20f''(x)%20%20+%20h%5E3%20f'''(x)%20+%20O(h%5E4)%20%5C%5C%5B12pt%5D%0A%5CRightarrow%20h%5E2%20f''(x)%20&amp;=%20f(x+2h)%20-%202%20%5C,%20f(x+h)%20+%20f(x)%20-%20h%5E3%20f'''(x)%20+%20O(h%5E4)%20%5C%5C%5B12pt%5D%0A%5CRightarrow%20f''(x)%20&amp;=%20%5Cfrac%7Bf(x+2h)%20-%202%20%5C,%20f(x+h)+%20f(x)%7D%7Bh%5E2%7D%20-%20h%5C,%20f'''(x)%20+%20O(h%5E2)%20%5C%5C%5B12pt%5D%0A&amp;=%20%5Cfrac%7Bf(x+2h)%20-%202%20%5C,%20f(x+h)+%20f(x)%7D%7Bh%5E2%7D%20+%20O(h)%20%5C%5C%5B12pt%5D%0A&amp;=%20%5Cfrac%7Bf(x+2h)%20-%202%20%5C,%20f(x+h)+%20f(x)%7D%7Bh%5E2%7D%20+%20o(1)%20%5C%5C%5B12pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cdisplaystyle%20f(x-2h)%20=%20f(x)%20-%202%5C,h%5C,f'(x)%20+%202%20%5C,%20h%5E2%20f''(x)%20-%20%5Cfrac%7B4h%5E3%7D%7B3%7D%20f'''(x)%20+%20O(h%5E4)%20%5C%5C%5B12pt%5D%0A%5Cdisplaystyle%20f(x-h)%20=%0A%20%20%20%20f(x)%20-%20h%5C,f'(x)%20+%20%5Cfrac%7Bh%5E2%7D%7B2%7D%20f''(x)%20-%20%5Cfrac%7Bh%5E3%7D%7B6%7D%20f'''(x)%20+%20O(h%5E4)%0A%5Cend%7Bcases%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CRightarrow%20f(x-2h)%20-%202%5C,f(x-h)%20&amp;=%20-%5C,f(x)%20+%20h%5E2%20f''(x)%20%20-%20h%5E3%20f'''(x)%20+%20O(h%5E4)%20%5C%5C%5B12pt%5D%0A%5CRightarrow%20h%5E2%20f''(x)%20&amp;=%20f(x)%20-%202%20%5C,%20f(x-h)%20+%20f(x-2h)%20+%20h%5E3%20f'''(x)%20+%20O(h%5E4)%20%5C%5C%5B12pt%5D%0A%5CRightarrow%20f''(x)%20&amp;=%20%5Cfrac%7Bf(x)%20-%202%20%5C,%20f(x-h)+%20f(x-2h)%7D%7Bh%5E2%7D%20+%20h%5C,%20f'''(x)%20+%20O(h%5E2)%20%5C%5C%5B12pt%5D%0A&amp;=%20%5Cfrac%7Bf(x)%20-%202%20%5C,%20f(x-h)+%20f(x-2h)%7D%7Bh%5E2%7D%20+%20O(h)%20%5C%5C%5B12pt%5D%0A&amp;=%20%5Cfrac%7Bf(x)%20-%202%20%5C,%20f(x-h)+%20f(x-2h)%7D%7Bh%5E2%7D%20+%20o(1)%20%5C%5C%5B12pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>Note that we can also rewrite it in the following way:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CDelta%5E2_h%5Bf%5D(x)%20&amp;:=%20%20%5CDelta_h%5B%5CDelta_h%5Bf%5D%5D(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%20%5CDelta_h%5Bg_h%5D(x)%20%5Cquad%20,%20%5Cquad%20%5Cquad%20g_h(x):=f(x+h)-f(x)%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%20g_h(x+h)-g_h(x)%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%5Cleft%5B%20f(x+2h)%20-%20f(x+h)%5Cright%5D%20-%20%5Cleft%5B%20f(x+h)%20-%20f(x)%5Cright%5D%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=f(x+2h)%20-%202%5C,f(x+h)%20+%20f(x)%20%5C%5C%5B10pt%5D%0A%5CRightarrow%20%5Cquad%20f''(x)%20&amp;=%20%5Cfrac%7B%5CDelta%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h)%20%5C%5C%5B15pt%5D%0A%5Cnabla%5E2_h%5Bf%5D(x)%20&amp;:=%20%20%5Cnabla_h%5B%5Cnabla_h%5Bf%5D%5D(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%20%5Cnabla_h%5Bg_h%5D(x)%20%5Cquad%20,%20%5Cquad%20%5Cquad%20g_h(x):=f(x)-f(x-h)%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%20g_h(x)-g_h(x-h)%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%5Cleft%5B%20f(x)%20-%20f(x-h)%5Cright%5D%20-%20%5Cleft%5B%20f(x-h)%20-%20f(x-2h)%5Cright%5D%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=f(x)%20-%202%5C,f(x-h)%20+%20f(x-2h)%20%5C%5C%5B10pt%5D%0A%5CRightarrow%20%5Cquad%20f''(x)%20&amp;=%20%5Cfrac%7B%5Cnabla%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h)%20%5C%5C%5B15pt%5D%0A%5Cdelta%5E2_h%5Bf%5D(x)%20&amp;:=%20%20%5Cdelta_h%5B%5Cdelta_h%5Bf%5D%5D(x)%20%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%20%5Cdelta_h%5Bg_h%5D(x)%20%5Cquad%20,%20%5Cquad%20%5Cquad%20g_h(x):=f(x+%5Ctfrac%7Bh%7D%7B2%7D)-f(x-%5Ctfrac%7Bh%7D%7B2%7D)%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%20g_h(x+%5Ctfrac%7Bh%7D%7B2%7D)-g_h(x-%5Ctfrac%7Bh%7D%7B2%7D)%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=%5Cleft%5B%20f(x+h)%20-%20f(x)%5Cright%5D%20-%20%5Cleft%5B%20f(x)%20-%20f(x-h)%5Cright%5D%20%5C%5C%5B10pt%5D%0A%20%20%20%20&amp;=f(x+h)%20-%202%5C,f(x)%20+%20f(x-h)%20%5C%5C%5B10pt%5D%0A%5CRightarrow%20%5Cquad%20f''(x)%20&amp;=%20%5Cfrac%7B%5Cdelta%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5C%5C%5B15pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>So, to summarize:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Af''(x)%20&amp;=%20%5Cfrac%7B%5Cdelta%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Cdelta%5E2_h%5Bf%5D(x)%20=%20f(x+h)%20-%202%5C,f(x)%20+%20f(x-h)%20%5Ctag%7B3.1a%7D%5C%5C%5B8pt%5D%0Af''(x)%20&amp;=%20%5Cfrac%7B%5CDelta%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5CDelta%5E2_h%5Bf%5D(x)%20=%20f(x+2h)%20-%202%5C,f(x+h)%20+%20f(x)%20%20%20%20%5Ctag%7B3.1b%7D%5C%5C%5B8pt%5D%0Af''(x)%20&amp;=%20%5Cfrac%7B%5Cnabla%5E2_h%5Bf%5D(x)%7D%7Bh%5E2%7D%20+%20O(h)%20%5C;%20,%20%5Cquad%20%5Cquad%20%5Cnabla%5E2_h%5Bf%5D(x)%20=%20f(x)%20-%202%5C,f(x-h)%20+%20f(x-2h)%20%20%20%20%5Ctag%7B3.1c%7D%5C%5C%5B8pt%5D%0A%5Cend%7Balign%7D%0A"></p>
<p>Rewritten using the index notation <img src="https://latex.codecogs.com/png.latex?x_n"> and <img src="https://latex.codecogs.com/png.latex?f_n%20=%20f(x_n)"> and assuming that all the elements are equally spaced by <img src="https://latex.codecogs.com/png.latex?h=x_%7Bn+1%7D-x_%7Bn%7D%5C,%20,%5C;%20%5Cforall%5C,%20n=0,...,N-1"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Af_%7Bn%7D''%20&amp;=%20%5Cfrac%7Bf_%7Bn+1%7D%20-%202f_n%20+%20f_%7Bn-1%7D%7D%7Bh%5E2%7D%20+%20O(h%5E2)%20%5Ctag%7B3.2a%7D%5C%5C%5B10pt%5D%0Af_%7Bn%7D''%20&amp;=%20%5Cfrac%7Bf_%7Bn+2%7D%20-%202f_%7Bn+1%7D%20+%20f_%7Bn%7D%20%7D%7Bh%5E2%7D%20+%20O(h)%20%5Ctag%7B3.2b%7D%20%5C%5C%5B10pt%5D%0Af_%7Bn%7D''%20&amp;=%20%5Cfrac%7Bf_%7Bn%7D%20-%202f_%7Bn-1%7D%20+%20f_%7Bn-2%7D%20%7D%7Bh%5E2%7D%20+%20O(h)%20%5Ctag%7B3.2c%7D%20%5C%5C%5B10pt%5D%0A%5Cend%7Balign%7D%0A"></p>
</section>
<section id="higher-order-finite-differences" class="level2">
<h2 class="anchored" data-anchor-id="higher-order-finite-differences">Higher order finite differences</h2>
<p>More generally, the <img src="https://latex.codecogs.com/png.latex?n">-th order forward, backward, and central differences are given by the following expressions, which we will not derive here:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%5Cfrac%7B%5Cmathrm%7Bd%7D%5E%7Bn%7Df%7D%7B%5Cmathrm%7Bd%7Dx%5En%7D(x)%20=%20%5Cfrac%7B%5Cdelta_h%5En%5Bf%5D(x)%7D%7Bh%5En%7D%20+%20O(h%5E2)%0A%5C;%20,%20%5Cquad%20%5Cquad%0A%5Cdelta_h%5En%5Bf%5D(x)%20&amp;=%20%5Csum_%7Bj=0%7D%5En%20(-1)%5E%7Bj%7D%20%20%5C,%20%5Cbinom%7Bn%7D%7Bj%7D%20%5C,%20f(x+(%5Ctfrac%7Bn%7D%7B2%7D-j)h)%20%5Ctag%7B4.1a%7D%5C%5C%5B15pt%5D%0A%5Cfrac%7B%5Cmathrm%7Bd%7D%5E%7Bn%7Df%7D%7B%5Cmathrm%7Bd%7Dx%5En%7D(x)%20=%20%5Cfrac%7B%5CDelta_h%5En%5Bf%5D(x)%7D%7Bh%5En%7D%20+%20O(h)%0A%5C;%20,%20%5Cquad%20%5Cquad%0A%5CDelta_h%5En%5Bf%5D(x)%20&amp;=%20%5Csum_%7Bj=0%7D%5En%20(-1)%5E%7Bn-j%7D%5C,%20%5Cbinom%7Bn%7D%7Bj%7D%20%5C,%20f(x+jh)%20%5Ctag%7B4.1b%7D%5C%5C%5B10pt%5D%0A%5Cfrac%7B%5Cmathrm%7Bd%7D%5E%7Bn%7Df%7D%7B%5Cmathrm%7Bd%7Dx%5En%7D(x)%20=%20%5Cfrac%7B%5Cnabla_h%5En%5Bf%5D(x)%7D%7Bh%5En%7D%20+%20O(h)%0A%5C;%20,%20%5Cquad%20%5Cquad%0A%5Cnabla_h%5En%5Bf%5D(x)%20&amp;=%20%5Csum_%7Bj=0%7D%5En%20(-1)%5E%7Bj%7D%20%20%5C,%20%5Cbinom%7Bn%7D%7Bj%7D%20%5C,%20f(x-jh)%20%5Ctag%7B4.1c%7D%5C%5C%5B15pt%5D%0A%5Cbinom%7Bn%7D%7Bj%7D%20:&amp;=%20%5Cfrac%7Bn!%7D%7Bj!(n-j)!%7D%20%5Cnotag%0A%5Cend%7Balign%7D%0A"></p>
<p>And this is just the top of the iceberg: <a href="https://en.wikipedia.org/wiki/Finite_difference_coefficient#Central_finite_difference">Ref.[2]</a> shows the whole set of coefficients to approximate the <img src="https://latex.codecogs.com/png.latex?n">-th derivative with an error of order <img src="https://latex.codecogs.com/png.latex?O(h%5Em)">, for both forward, backward and central differences:</p>


</section>

 ]]></description>
  <category>math</category>
  <category>physics</category>
  <guid>https://foglienimatteo.com/blog/basics-numerical-analysis/</guid>
  <pubDate>Mon, 12 Oct 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Rolling my own cubic spline (because Dierckx wouldn’t run on the GPU)</title>
  <link>https://foglienimatteo.com/blog/spline-gapse/</link>
  <description><![CDATA[ 





<section id="myspline" class="level2">
<h2 class="anchored" data-anchor-id="myspline">MySpline</h2>
<p>As a side project, I was working together with my colleague <a href="https://www.linkedin.com/in/salvo-cielo-a561992b2/">Salvatore Cielo</a> on the parallelisation of the Julia code I wrote in my Master thesis, <a href="https://github.com/foglienimatteo/GaPSE.jl">GaPSE.jl</a>.</p>
<p>After some research, I find out a package, <a href="https://github.com/JuliaGPU/KernelAbstractions.jl">KernelAbstractions.jl</a>, that allows to write kernels architecture agnostic, i.e.&nbsp;that work on CPUs and on every major GPU backend (<code>Metal.jl</code>, <code>CUDA.jl</code>, <code>oneAPI.jl</code> and <code>AMDGPU.jl</code>)</p>
<p>As a MWE:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Equivalent sintazes are available in oneAPI, Metal and AMDGPU</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">CUDA</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">KernelAbstractions</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Test</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> backend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CUDA.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CUDABackend</span>()</span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> DevArray <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CuArray</span>
<span id="cb1-5"></span>
<span id="cb1-6">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ones</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span>)</span>
<span id="cb1-7">dev_a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">DevArray</span>(a)</span>
<span id="cb1-8">dev_b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">similar</span>(dev_a)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@kernel</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_kernel!</span>(b, a, num)</span>
<span id="cb1-11">    i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@index</span>(Global, Linear)</span>
<span id="cb1-12">    b[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> num</span>
<span id="cb1-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">compiled_kernel! <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_kernel!</span>(backend, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 32 is the group size</span></span>
<span id="cb1-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compiled_kernel!</span>(dev_b, dev_a, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>; ndrange<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sizeof</span>(dev_a))</span>
<span id="cb1-17"></span>
<span id="cb1-18">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Array</span>(dev_b)    </span>
<span id="cb1-19"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@test</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.≈</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<p>In doing so, I discovered that I couldn’t offload to the GPU the <code>Dierckx</code> splines I was using. As an interesting exercise and with the help of my wonderful colleague <a href="https://github.com/ivan-pi">Ivan Pribec</a>, I decided to try to implement the code for 1D Splines from scratch.</p>
<p>The mathematical procedure I implemented in GaPSE.jl is based on: Parviz Moin, <em>“Fundamentals of Engineering Numerical Analysis”</em> (2010), Cambridge University Press, Second edition, Chapter 1.2 (<em>“Cubic Spline Interpolation”</em>)</p>
</section>
<section id="derivation-of-the-equation-system-for-the-cubic-spline" class="level2">
<h2 class="anchored" data-anchor-id="derivation-of-the-equation-system-for-the-cubic-spline">Derivation of the equation system for the cubic spline</h2>
<p>Let us suppose to have a set of <img src="https://latex.codecogs.com/png.latex?N"> data points <img src="https://latex.codecogs.com/png.latex?(x_i,%20y_i),%20%5Cforall%20i=1,...,N"> that we want to use to create our cubic spline. Let <img src="https://latex.codecogs.com/png.latex?g_i(x)"> be the cubic in the interval <img src="https://latex.codecogs.com/png.latex?x_i%20%5Cleq%20x%20%5Cleq%20x_%7Bi+1%7D"> and let <img src="https://latex.codecogs.com/png.latex?g(x)"> denote the collection of all the cubics for the entire range of x. Since <img src="https://latex.codecogs.com/png.latex?g"> is piecewise cubic, its second derivative <img src="https://latex.codecogs.com/png.latex?g''"> is piecewise linear. For the interval <img src="https://latex.codecogs.com/png.latex?x_i%20%5Cleq%20x%20%5Cleq%20x_%7Bi+1%7D">, we can then write the equation for the corresponding straight line as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ag''_i(x)%20=%20g''(x_i)%5Cfrac%7Bx-x_%7Bi+1%7D%7D%7Bx_%7Bi%7D%20-%20x_%7Bi+1%7D%7D%20+%20g''(x_%7Bi+1%7D)%5Cfrac%7Bx-x_%7Bi%7D%7D%7Bx_%7Bi+1%7D%20-%20x_%7Bi%7D%7D%5C;.%0A"></p>
<p>Integrating two times and imposing to match the known values at the endpoints <img src="https://latex.codecogs.com/png.latex?y_i%20=%20g_i(x_i)"> and <img src="https://latex.codecogs.com/png.latex?y_%7Bi+1%7D%20=%20g_i(x_%7Bi+1%7D)">, we get:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;%5Cforall%20x%20%5Cin%20%5Bx_i,%20x_%7Bi+1%7D%5D%20%5C;,%20%5C,%20%5Cforall%20i=1,...,N-1:%20%20%5C%5C%5B10pt%5D%0Ag_i(x)%20&amp;=%0A%20%20%20%20%5Ctfrac%7Bg''(x_i)%7D%7B6%7D%5Cleft%5B-%5Ctfrac%7B(x%20-%20x_%7Bi+1%7D)%5E3%7D%7B%5CDelta_i%7D%20+%0A%20%20%20%20%5CDelta_i(x%20-%20x_%7Bi+1%7D)%5Cright%5D%20+%20%5Ctfrac%7Bg''(x_%7Bi+1%7D)%7D%7B6%7D%5Cleft%5B%0A%20%20%20%20%20%20%20%20%5Ctfrac%7B(x%20-%20x_%7Bi%7D)%5E3%7D%7B%5CDelta_i%7D%20-%20%5CDelta_i(x%20-%20x_%7Bi%7D)%0A%20%20%20%20%5Cright%5D%20%5C%5C%5B7pt%5D%0A%20%20%20%20&amp;%5Cqquad+%20y_%7Bi+1%7D%20%5Ctfrac%7Bx%20-%20x_%7Bi%7D%7D%7B%5CDelta_i%7D%20-%20y_%7Bi%7D%20%5Ctfrac%7Bx%20-%20x_%7Bi+1%7D%7D%7B%5CDelta_i%7D%20%5C;%20,%0A%5Cend%7Bsplit%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5CDelta_i%20=%20x_%7Bi+1%7D%20-%20x_i">.</p>
<p>The <img src="https://latex.codecogs.com/png.latex?g''(x_i)"> are the <img src="https://latex.codecogs.com/png.latex?N"> unknowns, and applying the continuity of the first derivative condition <img src="https://latex.codecogs.com/png.latex?g_i'(x_i)%20=%20g_%7Bi-1%7D'(x_i)%20%5C,%20,%20%5C;%5Cforall%20i=2,...,N-1">, we get</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;%5Cqquad%20%5Cforall%20i%20=%202,...N-1:%20%5C%5C%5B8pt%5D%0A&amp;%5Ctfrac%7B%5CDelta_%7Bi-1%7D%7D%7B6%7Dg''(x_%7Bi-1%7D)%20+%0A%5Ctfrac%7B%5CDelta_%7Bi-1%7D%20+%20%5CDelta_%7Bi%7D%7D%7B3%7Dg''(x_%7Bi%7D)%20+%0A%5Ctfrac%7B%5CDelta_%7Bi%7D%7D%7B6%7Dg''(x_%7Bi+1%7D)%20=%0A%5Ctfrac%7By_%7Bi+1%7D%20+%20y_i%7D%7B%5CDelta_i%7D%20-%20%5Ctfrac%7By_%7Bi%7D%20+%20y_%7Bi-1%7D%7D%7B%5CDelta_%7Bi-1%7D%7D%20%20%5C;%20.%0A%5Cend%7Bsplit%7D%0A"></p>
<p>We have then <img src="https://latex.codecogs.com/png.latex?N-2"> equations for <img src="https://latex.codecogs.com/png.latex?N"> unknowns. The remaining ones are the “end conditions” that one may want to apply. The most common ones are:</p>
<ul>
<li>Free run-out or Natural spline: <img src="https://latex.codecogs.com/png.latex?g''(x_1)=g''(x_N)=0">
<ul>
<li>most commonly used;</li>
<li>this spline is the smoothest interpolant (i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cint_%7Bx_1%7D%5E%7Bx_N%7Dg''(x)%5C,%20%5Cmathrm%7Bd%7Dx"> is the smallest possible)</li>
</ul></li>
<li>Parabolic run-out: <img src="https://latex.codecogs.com/png.latex?g''(x_1)=g''(x_2)%20%5C,%20,%20%5C;%20g''(x_%7BN-1%7D)%20=%20g''(x_N)">
<ul>
<li>the interpolating polynomials in the first and last intervals are parabolas rather than cubics</li>
</ul></li>
<li>Combination of the first two: <img src="https://latex.codecogs.com/png.latex?g''(x_1)=%5Calpha%20%5C%20g''(x_2)%20%5C,%20,%20%5C;%5C;%20g''(x_%7BN-1%7D)%20=%20%5Cbeta%20%5C%20g''(x_N)"></li>
<li>Third Derivative continuity: for this look at the appropriate section</li>
</ul>
<p>Using the coefficients <img src="https://latex.codecogs.com/png.latex?c_1">, <img src="https://latex.codecogs.com/png.latex?a_N">, <img src="https://latex.codecogs.com/png.latex?b_1">, <img src="https://latex.codecogs.com/png.latex?b_N">, <img src="https://latex.codecogs.com/png.latex?d_1"> and <img src="https://latex.codecogs.com/png.latex?d_N"> for this Initial Conditions (IC), we can write the complete set of equation in the matrix form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20b_1%20&amp;%20c_1%20&amp;%200%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%200%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cfrac%7B%5CDelta_%7B1%7D%7D%7B6%7D%20%20&amp;%20%5Cfrac%7B%5CDelta_%7B1%7D%20+%20%5CDelta_%7B2%7D%7D%7B3%7D%20&amp;%20%5Cfrac%7B%5CDelta_%7B2%7D%7D%7B6%7D%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%200%20&amp;%20%5Cfrac%7B%5CDelta_%7B2%7D%7D%7B6%7D%20%20&amp;%20%5Cfrac%7B%5CDelta_%7B2%7D%20+%20%5CDelta_%7B3%7D%7D%7B3%7D%20&amp;%20%20%5Cddots%20&amp;%20%5Ccdots%20&amp;%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cddots%20&amp;%20%5Cddots%20&amp;%20%5Cddots%20&amp;%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cdots%20&amp;%20%5Cfrac%7B%5CDelta_%7BN-2%7D%7D%7B6%7D%20&amp;%20%5Cfrac%7B%5CDelta_%7BN-2%7D%20+%20%5CDelta_%7BN-1%7D%7D%7B3%7D%20&amp;%20%5Cfrac%7B%5CDelta_%7BN-1%7D%7D%7B6%7D%5C%5C%5B5pt%5D%0A%20%20%20%200%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%200%20&amp;%20a_N%20&amp;%20b_N%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20g''(x_1)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_2)%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_%7BN-1%7D)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_N)%0A%5Cend%7Bbmatrix%7D=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20d_1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cfrac%7By_%7B3%7D%20+%20y_2%7D%7B%5CDelta_2%7D%20-%20%5Cfrac%7By_%7B2%7D%20+%20y_%7B1%7D%7D%7B%5CDelta_%7B1%7D%7D%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cfrac%7By_%7BN%7D%20+%20y_%7BN-1%7D%7D%7B%5CDelta_%7BN-1%7D%7D%20-%20%5Cfrac%7By_%7BN-1%7D%20+%20y_%7BN-2%7D%7D%7B%5CDelta_%7BN-2%7D%7D%20%5C%5C%5B5pt%5D%0A%20%20%20%20d_N%0A%5Cend%7Bbmatrix%7D%0A"></p>
</section>
<section id="apply-the-tdma" class="level2">
<h2 class="anchored" data-anchor-id="apply-the-tdma">Apply the TDMA</h2>
<p>The equations are in tridiagonal form and diagonally dominant, so we can easily apply the TriDiagonal Matrix Algorithm (TDMA) to this system</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20b_1%20&amp;%20c_1%20&amp;%200%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%200%20%5C%5C%5B5pt%5D%0A%20%20%20%20a_2%20%20&amp;%20b_2%20&amp;%20c_2%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%20%5Cvdots%20%5C%5C%5B10%20pt%5D%0A%20%20%20%200%20&amp;%20a_3%20%20&amp;%20b_3%20&amp;%20%20%5Cddots%20&amp;%20%5Ccdots%20&amp;%20%5Cvdots%20%5C%5C%5B10pt%5D%0A%20%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cddots%20&amp;%20%5Cddots%20&amp;%20%5Cddots%20&amp;%20%5Cvdots%20%5C%5C%5B10pt%5D%0A%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cdots%20&amp;%20a_%7BN-1%7D%20&amp;%20b_%7BN-1%7D%20&amp;%20c_%7BN-1%7D%5C%5C%5B10pt%5D%0A%20%20%20%200%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%200%20&amp;%20a_N%20&amp;%20b_N%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20g''(x_1)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_2)%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_%7BN-1%7D)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_N)%0A%5Cend%7Bbmatrix%7D=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20d_1%20%5C%5C%5B5pt%5D%0A%20%20%20%20d_2%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20d_%7BN-1%7D%20%5C%5C%5B5pt%5D%0A%20%20%20%20d_N%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>with the following coefficients:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aa_i=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20%5Cdfrac%7B%5CDelta_%7Bi-1%7D%7D%7B6%7D%20&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20a_N%20%20&amp;%5Cquad%20i=N%0A%20%20%20%20%5Cend%7Bcases%7D%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cqquad%5Cqquad%0Ab_i=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20b_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Ctfrac%7B%5CDelta_%7Bi-1%7D%20+%20%5CDelta_%7Bi%7D%7D%7B3%7D%20&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20b_N%20%20&amp;%5Cquad%20i=N%0A%20%20%20%20%5Cend%7Bcases%7D%0A"> <img src="https://latex.codecogs.com/png.latex?%0Ac_i=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20c_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Ctfrac%7B%5CDelta_%7Bi%7D%7D%7B6%7D%20&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%0A%20%20%20%20%5Cend%7Bcases%7D%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cqquad%5Cqquad%0Ad_i=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20d_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Ctfrac%7By_%7Bi+1%7D%20+%20y_i%7D%7B%5CDelta_i%7D%20-%20%5Ctfrac%7By_%7Bi%7D%20+%20y_%7Bi-1%7D%7D%7B%5CDelta_%7Bi-1%7D%7D%20&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20d_N%20%20&amp;%5Cquad%20%20i=N%0A%20%20%20%20%5Cend%7Bcases%7D%0A"></p>
<p>and the corresponding IC values:</p>
<ul>
<li>Free run-out:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Ab_1=b_N=1%20%5C;,%5C;%20c_1=a_N=d_1=d_N=0%0A"></p>
<ul>
<li>Parabolic run-out:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Ab_1=b_N=1%20%5C;,%5C;%20c_1=a_N=-1%20%5C;,%5C;d_1=d_N=0%0A"></p>
<ul>
<li>Combination of first two run-out:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Ab_1=b_N=1%20%5C;,%5C;%20c_1=-%5Cbeta%20%5C;,%5Cquad%20a_N=-%5Calpha%20%5C;,%5C;%20d_1=d_N=0%0A"></p>
<ul>
<li>Third Derivative continuity:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;c_1=%5Cdfrac%7B%5CDelta_1%7D%7B6%7D%20%5C;%20,%20%5Cquad%20%20a_N=%5Cdfrac%7B%5CDelta_%7BN-1%7D%7D%7B6%7D%20%5C;%20,%20%5Cquad%0Ab_1=-%5Cdfrac%7B%5CDelta_1%7D%7B6%7D%20%5C;%20,%20%5Cquad%20%20b_N=-%5Cdfrac%7B%5CDelta_%7BN-1%7D%7D%7B6%7D%20%5C,%20,%5C%5C%5B10pt%5D%0A&amp;%5Cqquad%5Cqquad%20d_1=%5CDelta_1%5E2%5C;%20%5Ceta_1%5E%7B(3)%7D%20%5C;%20,%20%5Cquad%20%20d_N=-%5CDelta_%7BN-1%7D%5E2%5C;%20%5Ceta_%7BN-3%7D%5E%7B(3)%7D%20%20%5C;%20,%5C%5C%5B10pt%5D%0A&amp;%5Ceta_i%5E%7B(3)%7D%20=%20%5Cdfrac%7B%5Ceta_%7Bi+1%7D%5E%7B(2)%7D-%5Ceta_%7Bi%7D%5E%7B(2)%7D%7D%7Bx_%7Bi+3%7D%20-%20x_i%7D%20%5C;%20,%20%5Cquad%0A%5Ceta_i%5E%7B(2)%7D%20=%20%5Cdfrac%7B%5Ceta_%7Bi+1%7D-%5Ceta_%7Bi%7D%7D%7Bx_%7Bi+2%7D%20-%20x_i%7D%20%5C;%20,%20%5Cquad%0A%5Ceta_i%20=%20%5Cdfrac%7By_%7Bi+1%7D%20-%20y_i%7D%7Bx_%7Bi+%7D%20-%20x_i%7D%0A%5Cend%7Bsplit%7D%0A"></p>
<p>Then, we can compute the new coefficients <img src="https://latex.codecogs.com/png.latex?%5Calpha">, <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> and <img src="https://latex.codecogs.com/png.latex?%5Cdelta">, that give us a new simplified matrix:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Cbeta_1%20&amp;%20%5Cgamma_1%20&amp;%200%20%20&amp;%20%5Ccdots%20&amp;%200%20%5C%5C%5B5pt%5D%0A%20%20%20%200%20%20&amp;%20%5Cbeta_2%20&amp;%20%5Cgamma_2%20&amp;%20%5Ccdots%20&amp;%20%5Cvdots%20%5C%5C%5B10%20pt%5D%0A%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cddots%20&amp;%20%5Cddots%20&amp;%20%5Cvdots%20%5C%5C%5B10pt%5D%0A%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cdots%20%20&amp;%20%5Cbeta_%7BN-1%7D%20&amp;%5Cgamma_%7BN-1%7D%5C%5C%5B10pt%5D%0A%20%20%20%200%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%200%20&amp;%20%5Cbeta_N%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20g''(x_1)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_2)%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_%7BN-1%7D)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_N)%0A%5Cend%7Bbmatrix%7D=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Cdelta_1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cdelta_2%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cdelta_%7BN-1%7D%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cdelta_N%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>with:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A%5Calpha_i%20&amp;=0%20%5C;%20,%5Cqquad%5Cforall%20i=2,...,N%20%5C%5C%5B5pt%5D%0A%5Cbeta_i&amp;=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20b_1%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Ctfrac%7B%5CDelta_%7Bi-1%7D%20+%20%5CDelta_%7Bi%7D%7D%7B3%7D%5Cbeta_%7Bi-1%7D%20-%20%5Ctfrac%7B%5CDelta_%7Bi-1%7D%7D%7B6%7D%5Cgamma_%7Bi-1%7D%20&amp;%5Cquad%5Cforall%20i=2,...,N-1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20b_N%20%5Cbeta_%7BN-1%7D%20-%20a_N%20%5Cgamma_%7BN-1%7D%20&amp;%5Cquad%20i=N%0A%20%20%20%20%5Cend%7Bcases%7D%5C%5C%5B25pt%5D%0A%5Cgamma_i&amp;=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20c_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Ctfrac%7B%5CDelta_%7Bi%7D%7D%7B6%7D%5Cbeta_%7Bi-1%7D%20&amp;%5Cquad%5Cforall%20i=2,...,N-1%0A%20%20%20%20%5Cend%7Bcases%7D%5C%5C%5B20pt%5D%0A%5Cdelta_i&amp;=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20d_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Czeta_i%20%5Cbeta_%7Bi-1%7D%20-%20%5Ctfrac%7B%5CDelta_%7Bi-1%7D%7D%7B6%7D%5Cdelta_%7Bi-1%7D%20&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20d_N%20%5Cbeta_%7BN-1%7D%20-%20a_N%20%5Cdelta_%7BN-1%7D%20%20&amp;%5Cquad%20i=N%0A%20%20%20%20%5Cend%7Bcases%7D%5C%5C%5B30pt%5D%0A%5Czeta_i%20&amp;=%20%5Cfrac%7By_%7Bi+1%7D%20+%20y_i%7D%7B%5CDelta_i%7D%20-%20%5Cdfrac%7By_%7Bi%7D%20+%20y_%7Bi-1%7D%7D%7B%5CDelta_%7Bi-1%7D%7D%0A%5Cend%7Bsplit%7D%0A"></p>
<p>Even better, we can divide everything for <img src="https://latex.codecogs.com/png.latex?%5Cbeta_i">, and obtain:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20&amp;%20%5Ctilde%7B%5Cgamma%7D_1%20&amp;%200%20%20&amp;%20%5Ccdots%20&amp;%200%20%5C%5C%5B5pt%5D%0A%20%20%20%200%20%20&amp;%201%20&amp;%20%5Ctilde%7B%5Cgamma%7D_2%20&amp;%20%5Ccdots%20&amp;%20%5Cvdots%20%5C%5C%5B10%20pt%5D%0A%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cddots%20&amp;%20%5Cddots%20&amp;%20%5Cvdots%20%5C%5C%5B10pt%5D%0A%20%20%20%20%5Cvdots%20&amp;%20%5Cdots%20&amp;%20%5Cdots%20%20&amp;%201%20&amp;%5Ctilde%7B%5Cgamma%7D_%7BN-1%7D%5C%5C%5B10pt%5D%0A%20%20%20%200%20&amp;%20%5Ccdots%20&amp;%20%5Ccdots%20&amp;%200%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20g''(x_1)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_2)%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_%7BN-1%7D)%20%5C%5C%5B5pt%5D%0A%20%20%20%20g''(x_N)%0A%5Cend%7Bbmatrix%7D=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Ctilde%7B%5Cdelta%7D_1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Ctilde%7B%5Cdelta%7D_2%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Cvdots%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Ctilde%7B%5Cdelta%7D_%7BN-1%7D%20%5C%5C%5B5pt%5D%0A%20%20%20%20%5Ctilde%7B%5Cdelta%7D_N%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>with</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A%5Ctilde%7B%5Calpha%7D_i%20&amp;=%5Cfrac%7B%5Calpha_i%7D%7B%5Cbeta_i%7D=0%20%5C;%20,%5Cqquad%5Cforall%20i=2,...,N%20%5C%5C%5B5pt%5D%0A%5Ctilde%7B%5Cbeta%7D_i&amp;=%20%5Cfrac%7B%5Cbeta_i%7D%7B%5Cbeta_i%7D=%201%5C;%20,%5Cqquad%5Cforall%20i=1,...,N%20%5C%5C%5B5pt%5D%0A%5Ctilde%7B%5Cgamma%7D_i&amp;=%5Cfrac%7B%5Cgamma_i%7D%7B%5Cbeta_i%7D=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20c_1/b_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20c_i%20%5Cfrac%7B%5Cbeta_%7Bi-1%7D%7D%7B%5Cbeta_i%7D%20=%0A%20%20%20%20%20%20%20%20%5Cfrac%7Bc_i%20%5Cbeta_%7Bi-1%7D%7D%7Bb_i%20%5Cbeta_%7Bi-1%7D%20-%20%5Cgamma_%7Bi-1%7Da_i%7D%20=%20%5Cfrac%7Bc_i%7D%7Bb_i%20-%20%5Ctilde%7B%5Cgamma%7D_%7Bi-1%7Da_i%7D=%20%5Cfrac%7B%5CDelta_i%7D%7B2(%5CDelta_i%20+%20%5CDelta_%7Bi-1%7D)%20-%20%5CDelta_%7Bi-1%7D%5Ctilde%7B%5Cgamma%7D_%7Bi-1%7D%7D&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%0A%20%20%20%20%5Cend%7Bcases%7D%5C%5C%5B30pt%5D%0A%5Ctilde%7B%5Cdelta%7D_i&amp;=%5Cfrac%7B%5Cdelta_i%7D%7B%5Cbeta_i%7D=%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20d_1/b_1%20%20&amp;%5Cquad%20i=1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20%5Cfrac%7Bd_i%20%5Cbeta_%7Bi-1%7D%20-%20%5Cdelta_%7Bi-1%7Da_i%7D%7Bb_i%20%5Cbeta_%7Bi-1%7D%20-%20%5Cgamma_%7Bi-1%7Da_i%7D%20=%20%5Cfrac%7Bd_i%20-%20%5Ctilde%7B%5Cdelta%7D_%7Bi-1%7Da_i%7D%7Bb_i%20-%20%5Ctilde%7B%5Cgamma%7D_%7Bi-1%7Da_i%7D%20=%20%5Cfrac%7B6%5Czeta_i%20-%20%5CDelta_%7Bi-1%7D%20%5Ctilde%7B%5Cdelta%7D_%7Bi-1%7D%20%7D%7B2(%5CDelta_%7Bi-1%7D%20+%20%5CDelta_i)-%20%5CDelta_%7Bi-1%7D%5Ctilde%7B%5Cgamma%7D_%7Bi-1%7D%7D%20&amp;%5Cquad%20%5Cforall%20i=2,...,N-1%20%5C%5C%5B5pt%5D%0A%20%20%20%20%20%20%20%20d_N%20%5Cbeta_%7BN-1%7D%20-%20a_N%20%5Cdelta_%7BN-1%7D%20%20&amp;%5Cquad%20i=N%0A%20%20%20%20%5Cend%7Bcases%7D%5C%5C%5B30pt%5D%0A%5Czeta_i%20&amp;=%20%5Cdfrac%7By_%7Bi+1%7D%20+%20y_i%7D%7B%5CDelta_i%7D%20-%20%5Cdfrac%7By_%7Bi%7D%20+%20y_%7Bi-1%7D%7D%7B%5CDelta_%7Bi-1%7D%7D%0A%5Cend%7Bsplit%7D%0A"></p>
<p>The unknowns <img src="https://latex.codecogs.com/png.latex?g''(x_i)"> are given from:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A&amp;%5Cqquad%20g''(x_N)%20=%20%5Cfrac%7B%5Cdelta_N%7D%7B%5Cbeta_N%7D%20=%20%5Ctilde%7B%5Cdelta%7D_N%5C%5C%5B11pt%5D%0A&amp;g''(x_i)%20=%20%5Cfrac%7B%5Cdelta_i%20-%20%5Cgamma_i%5C,%20g''(x_%7Bi+1%7D)%7D%7B%5Cbeta_i%7D%20=%20%5Ctilde%7B%5Cdelta%7D_i%20-%20%5Ctilde%7B%5Cgamma%7D_i%5C,%20g''(x_%7Bi+1%7D)%0A%5Cend%7Balign*%7D%0A"></p>
</section>
<section id="final-spline-polynomial-coefficients" class="level2">
<h2 class="anchored" data-anchor-id="final-spline-polynomial-coefficients">Final spline polynomial coefficients</h2>
<p>The final expression for <img src="https://latex.codecogs.com/png.latex?g(x)"> is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A&amp;%5Cqquad%20%5Cqquad%20g(x)%20=%20%5Csum_%7Bi=1%7D%5EN%20g_i(x)%20%5C;,%5Cquad%20%5Cforall%20x%20%5Cin%20%5Bx_1,%20x_N%5D%5C%5C%5B12pt%5D%0A&amp;%5Cquad%20%5Cforall%20x%20%5Cin%20%5Bx_i,%20x_%7Bi+1%7D%5D%20%5C;%20,%20%5Cquad%20%5Cforall%20i=1,...,N-1:%20%5C%5C%5B8pt%5D%0A&amp;%20g_i(x)%20=%20y_i%20+%20B_i%20(x-x_i)%20+%20C_i%20(x-x_i)%5E2%20+%20D_i%20(x-x_i)%5E3%20%5C;,%20%5C%5C%5B8pt%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>where:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC_i%20=%20%5Cfrac%7Bg''(x_i)%7D%7B2%7D%20=%20%5Cbegin%7Bcases%7D%0A%20%20%20%20%5Cdfrac%7B%5Cdelta_N%7D%7B2%5Cbeta_N%7D%20=%20%5Cdfrac%7B%5Ctilde%7B%5Cdelta%7D_N%7D%7B2%7D%20&amp;%5Cquad%20i=N%20%5C%5C%5B10pt%5D%0A%20%20%20%20%5Cdfrac%7B%5Cdelta_i%20-%20%5Cgamma_i%5C,%20g''(x_%7Bi+1%7D)%7D%7B2%5Cbeta_i%7D%20=%20%5Cdfrac%7B%5Cdelta_i%20-%20%5Cgamma_i%5C,%20C_%7Bi+1%7D%7D%7B2%5Cbeta_i%7D%20=%5Cdfrac%7B%5Ctilde%7B%5Cdelta%7D_i%20-%20%5Ctilde%7B%5Cgamma%7D_i%5C,%20C_%7Bi+1%7D%7D%7B2%7D%20&amp;%5Cquad%20%5Cforall%20i%20=%201,...,N-1%0A%5Cend%7Bcases%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AB_i%20&amp;=%20%5Cfrac%7By_%7Bi+1%7D-y_i%7D%7B%5CDelta_i%7D%20-%20%5Cfrac%7B%5CDelta_i%7D%7B6%7D%5Cleft%5B2%20g''(x_i)%20+%20g''(x_%7Bi+1%7D)%20%5Cright%5D%20%5C%5C%5B10pt%5D%0A&amp;=%20%5Cfrac%7By_%7Bi+1%7D-y_i%7D%7B%5CDelta_i%7D%20-%20%5Cfrac%7B%5CDelta_i%7D%7B3%7D%5Cleft%5B2%20C_i%20+%20C_%7Bi+1%7D%20%5Cright%5D%20%5C;%20,%20%5Cqquad%20%5Cforall%20i%20=%201,...,N-1%0A%5Cend%7Bsplit%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AD_i%20&amp;=%20%5Cfrac%7Bg''(x_%7Bi+1%7D)-g''(x_i)%7D%7B6%5CDelta_i%7D%20%5C%5C%5B10pt%5D%0A&amp;=%20%5Cfrac%7BC_%7Bi+1%7D-C_i%7D%7B3%5CDelta_i%7D%20%5Cqquad%20,%20%5C;%20%5Cforall%20i%20=%201,...,N-1%0A%5Cend%7Bsplit%7D%0A"></p>
<p>NOTE: the coefficients <img src="https://latex.codecogs.com/png.latex?B_i">, <img src="https://latex.codecogs.com/png.latex?C_i"> and <img src="https://latex.codecogs.com/png.latex?D_i"> are <img src="https://latex.codecogs.com/png.latex?3(N-1)"> tuples! We defined <img src="https://latex.codecogs.com/png.latex?C_N"> just to get rid of <img src="https://latex.codecogs.com/png.latex?g''(x_N)"> and write <img src="https://latex.codecogs.com/png.latex?B_i"> and <img src="https://latex.codecogs.com/png.latex?D_i"> in function of <img src="https://latex.codecogs.com/png.latex?C_i"> only. However, that <img src="https://latex.codecogs.com/png.latex?C_N"> has no importance after the recursive computation, and you can get rid of it.</p>


</section>

 ]]></description>
  <category>math</category>
  <category>physics</category>
  <guid>https://foglienimatteo.com/blog/spline-gapse/</guid>
  <pubDate>Sat, 26 Apr 2025 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
