    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Eight Rooty Pieces</title>
        <link>https://members.accu.org/index.php/articles/2294</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #135 - October 2016</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c366/">o135</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-366/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+366/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Eight Rooty Pieces</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 05 October 2016 21:13:48 +01:00 or Wed, 05 October 2016 21:13:48 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Finding a square root is a common interview question. Patrick Martin demonstrates eight different ways to find a root.</p>
<p><strong>Body:</strong>&nbsp;<p><em>Sighâ€¦</em> Some things we have to deal with â€¦ like interview questions. Recently Iâ€™ve been interviewing candidates a bit more and naturally some old coding exercises Iâ€™ve collected over time have come to the fore, along with some impressions Iâ€™ve developed.</p>

<p>Letâ€™s assume itâ€™s that time in the interview when the candidate shows signs of being suitable to step up to the next level. At this point it really starts to matter whether the interviewer has prepared sufficiently well for this eventuality. Therefore, a question that has several such plateaus to provide some good challenge for the candidates who are on a roll would be very useful. Iâ€™m also suggesting the topic should generate discussion points so that in the initial 15 minutes that the candidate and I are forming a mutual opinion, I will get (and generate) as representative an impression as possible. Remember, the candidate is also interviewing you, and they might well form an opinion if all youâ€™re asking them to do is regurgitate facts.</p>

<p>So are there interview questions that have genuine â€˜breadth and depthâ€™?<sup><a href="#FN01">1</a></sup></p>

<p>Well, hereâ€™s a fun little question Iâ€™ve been carting along to interviews in note form for some time that I aim to persuade you will generate discussion points, and my notes have grown to either being</p>

<ul>
	<li>a significant number of sheets of paper</li>
	<li>or one page of an entirely unusable font size</li>
</ul>

<p>So, without further ado.</p>

<h2>The question</h2>

<p class="blockquote">Please implement the square root function[<a href="#[Wikipedia_1]">Wikipedia_1</a>] [<a href="#[monkeys_sqrt]">monkeys_sqrt</a>]</p>

<p>One thing I like about this question as that itâ€™s really quite easy to run and test even in some minimal web based online coding tool.</p>

<h3>What one learns in asking this question</h3>

<ul>
	<li>First up: some people are really quite wary of <code>sqrt()</code> in this context. I am not judging, let us be clear.</li>
	
	<li>There is a giant range in the comfort level for working through the issues in implementing this deceptively simple function.</li>
	
	<li>People are generally wrong to be frightened of the problem.
		<p>They often surprise themselves when they reach the end.</p>
	</li>

	<li>There are quite a few approaches that are recognisable.</li>
</ul>

<h3>5.000000 stages of shock.</h3>

<p>It would be a fair point that there is a sneaky element of testing character and resilience with this question. I am going to argue this is both legitimate and worthwhile, based on my assertion that [i] itâ€™s not that hard and [ii] there is so much to discuss that running out of steam / time is not that much of an issue in the wider scheme of things.</p>

<p>Nevertheless it seems people pass through shock and a number of other stages when presented with this challenge: Denial, Anger, Bargaining, Depression. I would like to think we can short-circuit this and skip straight to Acceptance (and perhaps a little Fun?). Letâ€™s dive in and see what Iâ€™m talking about.</p>

<h3>Initial unstructured points</h3>

<p>The exercise typically goes through a number of phases, sometimes the first of which is akin to scoping out the problem.</p>

<p>This can be a very revealing phase: demonstrating the candidateâ€™s process for collecting information. Amusingly, some make adequate assumptions and plough on, because as we will see later: â€˜double is just fineâ€™<sup><a href="#FN02">2</a></sup>, whereas some might ask about which arbitrary precision packages weâ€™re allowed to use.</p>

<p>Assuming weâ€™re here though: hereâ€™s an incomplete list of things one might want to touch upon</p>

<ul>
	<li>what is the return type?
		<p><em>discussion points</em> might be considering arbitrary precision</p></li>
		
	<li>whatâ€™s the input type?
		<p><em>discussion points</em> â€“ is it the same as the return type, what bit size is the range, compared to the domain?<sup>2</sup></p></li>

	<li>what happens for inputs of 1, &gt; 1, &lt; 1 or negative values?
		<p>is this going to influence your thinking on the approach you take?</p></li>

	<li>what is your criterion for accuracy?</li>
	
	<li>how about float denormal values inputs, results [<a href="#[Wikipedia_2]">Wikipedia_2</a>]</li>
	
	<li>what about NAN, NaNQ, NaNS? [<a href="#[Wikipedia_3]">Wikipedia_3</a>]</li>
	
	<li>â€˜Oh hey, what do CPUs do?â€™ <em>discussion points</em><sup><a href="#FN03">3</a></sup>
		<p>you may want to keep your powder dry when asked, so push it, and pop it later</p></li>

	<li>finally, $bright_spark may well know the POSIX prototypes [<a href="#[posix]">posix</a>].
		<p>These prototypes address a lot of the above questions</p>
		<pre class="programlisting">
        #include &lt;math.h&gt;
        double sqrt(double x);
        float sqrtf(float x);
        long double sqrtl(long double x);</pre></li>
</ul>

<h2>Eight approaches</h2>

<p>So, having got past the initial stage of get to know the question, itâ€™s probably time to start writing code. Here follow eight implementations of varying quality, nominally in C++.</p>

<h3>Caveat</h3>

<p>Please remember that for some of these implementations, it may be hard to find canonical examples â€˜out thereâ€™ of some of these algorithms. This is because they are in fact a bit rubbish. The more â€˜recognisable versionsâ€™ are pretty much shadows of the many already thoroughly written-up versions available for research. Remember though, the name of the game here is to get <em>discussion points</em>, any and all means are acceptable.</p>

<h3>Alien technology</h3>

<p>An additional benefit of these discussions is when a novel-looking implementation arises, having some preparation under your belt will serve you well in recognising a variant of one of the following principles and steering the code/conversation in a more productive direction for <em>discussion points</em>.</p>

<h2>â€˜One linersâ€™</h2>

<h3>Closed form FOR THE WIN</h3>

<p>Explanation: closed form for the win!</p>

<pre class="programlisting">
  return exp(0.5 * log(val));</pre>

<p>This hinges on the identity</p>

<p style="margin-left:1em">log <em>x</em><sup>y</sup> = <em>y</em> log <em>x</em></p>

<p>and if we remind ourselves that the power that generates a square root is 0.5, and exp is the inverse of log</p>

<p style="margin-left:1em">sqrt(<em>x</em>) == <em>x</em><sup>1/2</sup>, log(exp(<em>x</em>)) == <em>x</em></p>

<p>it all drops into place.<sup><a href="#FN04">4</a></sup></p>

<p>Note that I did eliminate <code>pow(x, 0.5)</code> as a possible solution as that felt a bit too much like cheating to me.</p>

<h2>Search algorithms</h2>

<p>This class of solution hinges on iterating upon a trial value until convergence is attained â€“ Iâ€™ve introduced a <code>seed_root()</code> function with no explanation that returns a â€˜good initial guessâ€™ for sqrt() in order to concentrate on the details. Weâ€™ll come back to <code>seed_root()</code> later on.</p>

<h3>The Babylonian method or Heroâ€™s method</h3>

<p>The graphical explanation of this algorithm is: iterative search for square root by successive reduction of difference in length between the 2 sides of a rectangle with the area of the input value. [<a href="#[Wikipedia_4]">Wikipedia_4</a>]:</p>

<p class="blockquote">pick side</p>
<p class="blockquote">derive other_side by A / side </p>
<p class="blockquote">if side == other_side: return side</p>
<p class="blockquote">else split the difference for the next side and loop</p>
<p>and hence Listing 1.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
double my_sqrt_bablyonian(double val) {
  double x = seed_root();
  while (fabs((x * x) - val) &gt; (val * TOLERANCE))
  {
    x = 0.5 * (x + (val / x));
  }
  return x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>The loop is controlled by a test on whether weâ€™re â€˜near enoughâ€™ to the answer, which may be a <em>discussion point</em>. Also note the mechanism for generating a new trial value always narrows the difference between the trial and trial / input.</p>

<p>Notable points:</p>

<ul>
	<li>itâ€™s quite possibly the only algorithm to be presented here that you can implement using a piece of rope and a setsquare. See [<a href="#[Wikipedia_5]">Wikipedia_5</a>] for the classical Ancient toolset</li>
	
	<li>this algorithm is somewhat unique in that it can handle finding the negative root if the trial value passed in is negative</li>
	
	<li>there is one more interesting fact we will discover shortly</li>
</ul>

<p>Although there is the amazing Bablyonian Tablet YBC 7289 [<a href="#[YBC7289]">YBC7289</a>], itâ€™s hard to find a lo-fi image of this implementation so I persuaded a 12-year old to do it for me. Figure 1 shows a Heroâ€™s Method contemporary reimplementation for the value 23. We started with a trial value of 6 and got the result 4.8 which is accurate to 0.08%.</p>

<table class="sidebartable">
	<tr>
		<td><img src="http://accu.org/content/images/journals/ol135/Martin/Martin-01.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 1</td>
	</tr>
</table>

<p>Note the Babylonian tablet has sqrt(2) to 9 decimal digits of precision â€“ how did they do that?</p>

<h3>Finding the root using Newton Raphson</h3>

<p>Explanation: Newton Raphson [<a href="#[Wikipedia_6]">Wikipedia_6</a>] searches for the value of <em>x</em> yielding zero for <em>x</em><sup>2</sup> - value, (hence <em>x</em><sup>2</sup> = value)</p>

<p>Graphical explanation:</p>
<p class="blockquote">pick a trial value</p>
<p class="blockquote">search for the zero</p>
<p class="blockquote">by building the line passing through</p>
<p class="blockquote">the current trial output with the gradient</p>
<p class="blockquote">of the function at that point</p>
<p class="blockquote">    â€“ a numerically estimated gradient will do, for <em>discussion points</em>.</p>
<p class="blockquote">the intersection of that triangle with zero is the new trial</p>
<p class="blockquote">exit when desired accuracy attained</p>

<p>Listing 2 is one interpretation.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
double my_sqrt_newtonraphson(double val) {
  double x = seed_root();
  while (fabs((x * x) - val) &gt; (val * TOLERANCE))
  {
    // x * x - value is the root sought
    double gradient =
      (((x * 1.5) * (x * 1.5)) -
       ((x * 0.5) * (x * 0.5))) / (x);
    x = x - ((x * x - value) / gradient);
  }
  return x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>For <em>discussion points</em> see also the related Householder methods [<a href="#[Wikipedia_7]">Wikipedia_7</a>]</p>

<h3>Newton Raphson with a closed form identity for the gradient</h3>

<p>Now, some may know that there is a very simple result d(<em>x</em><sup>2</sup>)/d<em>x</em> = 2<em>x</em> for the gradient that is needed for Newton Raphson and hence plugging in the closed form result for <em>dy</em>/<em>dx</em>, we can skip some typing to yield this (see Listing 3).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
double my_sqrt_newtonraphson(double val) {
  double x = seed_root();
  while (fabs((x * x) - val) &gt; (val * TOLERANCE))
  {
    // x * x - val is root sought
    x = x - ((x * x - val) / (2 * x));
  }
  return x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<p>Note the original expression containing the gradient:</p>

<p style="margin-left:1em">double gradient = (((<em>x</em> * 1.5) * (<em>x</em> * 1.5)) - ((<em>x</em> * 0.5) * (<em>x</em> * 0.5)));</p>

<p>This is the lazy manâ€™s version of calculating the gradient around the domain value <em>x</em> using the values at <em>x</em> +/- <em>b.</em></p>

<p style="margin-left:1em">	(<em>x</em> + <em>b</em>)<sup>2</sup> - (<em>x</em> - <em>b</em>)<sup>2</sup> / 2<em>b</em></p>
<p style="margin-left:1em">=	<em>x</em><sup>2</sup> + 2<em>bx</em> + <em>b</em><sup>2</sup> - <em>x</em><sup>2</sup> + 2<em>bx</em> - <em>b</em><sup>2</sup> / 2<em>b</em></p>
<p style="margin-left:1em">=	2<em>x</em></p>

<p>If <em>b</em> were a constant this would not scale with the value of <em>x</em>; however, <em>b</em> can be substituted by <em>x</em>/2 and we recover the initial gradient calculation, and hence an equivalent expression for the closed form expression.</p>

<p>Confession time: I first picked 0.5 * <em>x</em> and 1.5 * <em>x</em> intuitively, having been hand-bodging numerical estimates into code for some time now, so I didnâ€™t think too hard about it (this time around) and serendipitously hit a solution that can be transformed using simple algebra into the closed form solution.</p>

<h2>3.0, 2.0 or 1.0 methods?</h2>

<p>So far the last 3 solutions have used identical outer loops, merely with different expressions for generating new trial values in the middle. Letâ€™s take a closer look at that expression: with the closed form for the gradient we get this expression:</p>

<p style="margin-left:1em">	<em>x</em> = <em>x</em> - ((<em>x</em> * <em>x</em> - value) / (2 * <em>x</em>));</p>
<p style="margin-left:1em">â‡’	<em>x</em> = 0.5 * (2<em>x</em> - (<em>x</em> - (value /<em>x</em>)))</p>
<p style="margin-left:1em">â‡’	<em>x</em> = 0.5 * (<em>x</em> + (value / <em>x</em>))</p>

<p>This is the Heroâ€™s method expression, so the final notable point about Heroâ€™s method is that itâ€™s a condensed version of the more taxing Newton Raphson approach.</p>

<h3>Confession time</h3>

<p>Having encountered the two methods (Babylonian and Newton Raphson) independently, I missed the equivalence between them until I took a look at the iteration values.</p>

<p>Another confession â€“ even with the mathematical equivalence, there was still a difference as the version just shown has an issue: it fails to locate values for roots above sqrt(<code>std::numeric_limits::max()</code>). This is due to an overflow in the expression to generate the new trial value.</p>

<p>The fix â€“ perhaps unsurprisingly enough â€“ is thus:</p>

<p>     - double <em>x</em> = seed_root();</p>
<p>     + <strong>long</strong> double <em>x</em> = seed_root();</p>

<p>Another set of <em>discussion points</em> arise from the necessity of introducing the long version of the type in the algorithm. Is this choice leading to an implicit conversion in the return statement a maintenance wart? What if we need this to be a generic algorithm, parameterised on the input type?</p>

<h2>Slow but sure (?)</h2>

<h3>A range reduction approach</h3>

<p>Graphical explanation: a range reduction approach which aims to halve the range [upper, lower] upon each iteration (does not rely upon a particularly good initial guess, though the bounds do need to be ordered). Newton Raphson / Hero can be proven to converge quadratically [<a href="#[Wikipedia_8]">Wikipedia_8</a>], whereas this approach effectively converges linearly, hence it requires many more iterations. The algorithm takes 30 iterations for a double sqrt as achieving over 10 digits of decimal precision will typically require approximately 30 halvings of the interval. (See Listing 4.)</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
double my_sqrt_range(double val) {
  double upper = seed_root(value) * 10;
  double lower = seed_root(value) / 10;

  double x = (lower + upper) / 2;
  int n = 1;

  while ((n &lt; RANGE_ITERATIONS) &amp;&amp;
    (fabs((x * x) - value) &gt; (value * TOLERANCE)))
  {
    if (((x * x) &gt; value))
      upper = x;
    else
      lower = x;
    x = (lower + upper) / 2;
    n++;
  }
  return x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<p>If this is found in the wild it would probably be best to put it out of its misery. The possible benefit of this is that candidates less confident of their mathematics will be able to implement this by concentrating purely upon the logic of searching.</p>

<h3>Scan and step reduction</h3>

<p>This is a very naive guess step and scan approach, reversing and decreasing the step on each transition from above to below. Feed it a decent enough initial guess and it will work its way towards the solution, as it is another linearly convergent solution. (See Listing 5).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
double my_sqrt_naive(double val) {
  int n = 1;
  double x = seed_root(value) / 2;
  double step = x / 4;
  double lastdiff = 0;
  double diff = (x * x) - value;

  while ((n &lt; RANGE_ITERATIONS) &amp;&amp;
         (fabs(diff) &gt; (value * TOLERANCE))) {
    if (diff &gt; 0)
      x -= step;
    else
      x += step;

    if ((diff &gt; 0) != (lastdiff &gt; 0)) {
      step = step * 0.5;
    }
    lastdiff = diff;
    diff = (x * x) - value;
  }

  return x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 5</td>
	</tr>
</table>

<h3>â€˜Homage to Carmackâ€™ method</h3>

<p>Finally, the origin of seed_root() can be revealed. Yes, just for fun, an old example of a very fast approximate inverse square root. Here is the obligatory xkcd reference [xkcd_1]. This still works (on Intel), and there is also a good write-up of how this works [<a href="#[Wikipedia_9]">Wikipedia_9</a>]. Note there are other values for the magic value than 0x5f375a86 â€“ which oddly get more search hits in Google(?!!).</p>

<p>The original code, sadly has comments and <code>#ifdef</code> rendering it unsuitable for printing in a family oriented programming publication, so Listing 6 is a modified version from Stack Overflow [<a href="#[SO_2]">SO_2</a>], and Listing 7 is a version supporting <code>double</code>, with the appropriate 64-bit magic value.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
float my_sqrt_homage_to_carmack(float x) {
  // PMM: adapted from the doubly cleaner 
  // Chris Lomont version

  float xhalf = 0.5f * x;
  int i = *(int *)&amp;x; 
    // get bits for floating value
  i = 0x5f375a86 - (i &gt;&gt; 1); 
      // gives initial guess y0
  x = *(float *)&amp;i;  // convert bits back to float

  // PMM: initial guess: to within 10% already!
  x = x * (1.5f - xhalf * x * x); 
    // Newton step, repeating increases accuracy

  return 1 / x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 6</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
double my_sqrt_homage_to_carmack64(double x) {
  double xhalf = x * 0.5;
  // get bits for floating value
  long long i = *(long long *)&amp;x;    
  // gives initial guess y0
  i = 0x5fe6eb50c7b537a9 - (i &gt;&gt; 1); 
  // convert bits back into double
  x = *(double *)&amp;i;                 

  // one Newton Raphson step
  x = x * (1.5f - xhalf * x * x); 

  return 1 / x;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 7</td>
	</tr>
</table>

<p>The result is not super accurate, but works in constant time and can be used as a seed into another algorithm.</p>

<p>For the most condensed explanation as to how that even works, see the closed form solution and consider that the bits of a floating point number when interpreted as an integer can be used to approximate its logarithm.</p>

<h2>â€˜Also ranâ€™</h2>

<p>In the grand tradition of sort algorithms [<a href="#[Wikipedia_10]">Wikipedia_10</a>], one could always break the ice by discussing solutions that make brute force look cunning.</p>

<h3>brutesqrt</h3>

<pre class="programlisting">
  d = min_double()
  while true:
    if (d * d == input) return d
    d = next_double(d)</pre>

<h3>bogosqrt (homage to bogosort)</h3>

<pre class="programlisting">
  d = random_double()
  while true:
    if (d * d == input) return d
    d = random_double()</pre>
	
<p>This and the prior approach will need an approach to define the accuracy of match. And perhaps a rather forgiving user calling that code.</p>

<h3>Quantum computer method</h3>

<pre class="programlisting">
  for value in all_doubles:
    return value if value ^ 2 == input</pre>
	
<p>It would be hoped that parallelising this would lead to good wall clock times?</p>

<h2>Code and tests</h2>

<p>Code demonstrating C++ implementations with tests of all the following are available at: <a href="http://www.github.com/patrickmmartin/2.8284271247461900976033774484194">http://www.github.com/patrickmmartin/2.8284271247461900976033774484194</a></p>


<h2>Conclusion</h2>

<p>So, letâ€™s review what we can get out of â€˜implement sqrt()â€™ in terms of <em>discussion topics</em>: closed form results versus algorithmic solutions â€“ discussion on the many interesting properties of floating point calculations, bronze age mathematical algorithms, consideration of domains and ranges. I havenâ€™t even touched upon error handling, but itâ€™s needed.</p>

<p>And finally there are other really fascinating techniques I havenâ€™t touched upon as I judged them too abstruse for an interview scenario: like Lagrangeâ€™s continued fractions [<a href="#[Wikipedia_11]">Wikipedia_11</a>], and also the Vedic techniques mentioned in [<a href="#[Wikipedia_1]">Wikipedia_1</a>].</p>

<p>You may have some questions.</p>

<p>Hereâ€™s my attempt to anticipate them.</p>

<ol>
	<li>Whatâ€™s with the name for the repo?
		<p>Itâ€™s the square root of 8, the number of methods, of course cube root would be have yielded a simpler name â€“ presaging the next installment! Of course, there will be no next installment, as one thing we have learned is that this topic is a giant nerd trap [<a href="#[xkcd_nerd_sniping]">xkcd_2</a>]. Merely perusing the references to this article for a short time will show how many areas of exploration exist to be followed.</p></li>
	
	<li>Will the Fast sqrt work on big-endian?
		<p>Very funny.</p></li>
</ol>

<h2>Acknowledgements</h2>

<p>I would like to take the opportunity to thank Frances Buontempo and the <em>Overload</em> review team for their careful review comments.</p>

<p>Gabriel Martin recreated the ancient world glories of calculating the square root of 23.</p>

<p>Also thanks to Hillel Y. Sims for spotting an issue in a code sample that got past everyone.</p>

<h2>References</h2>

<p class="bibliomixed"><a id="[monkeys_sqrt]"></a>[monkeys_sqrt] <a href="http://www.azillionmonkeys.com/qed/sqroot.html">http://www.azillionmonkeys.com/qed/sqroot.html</a></p>

<p class="bibliomixed"><a id="[posix]"></a>[posix] <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/sqrt.html">http://pubs.opengroup.org/onlinepubs/9699919799/functions/sqrt.html</a></p>

<p class="bibliomixed"><a id="[SO_1]"></a>[SO_1]  <a href="http://math.stackexchange.com/questions/537383/why-is-x-frac12-the-same-as-sqrt-x">http://math.stackexchange.com/questions/537383/why-is-x-frac12-the-same-as-sqrt-x</a>although the alleged duplicate has a beautiful answer:<a href="http://math.stackexchange.com/questions/656198/why-the-square-root-of-x-equals-x-to-the-one-half-power">http://math.stackexchange.com/questions/656198/why-the-square-root-of-x-equals-x-to-the-one-half-power</a></p>

<p class="bibliomixed"><a id="[SO_2]"></a>[SO_2] <a href="http://stackoverflow.com/questions/1349542/john-carmacks-unusual-fast-inverse-square-root-quake-iii">http://stackoverflow.com/questions/1349542/john-carmacks-unusual-fast-inverse-square-root-quake-iii</a></p>

<p class="bibliomixed"><a id="[SAR]"></a>[SAR] <a href="http://assemblyrequired.crashworks.org/timing-square-root/">http://assemblyrequired.crashworks.org/timing-square-root/</a></p>

<p class="bibliomixed"><a id="[Wikipedia_1]"></a>[Wikipedia_1] <a href="https://en.wikipedia.org/wiki/Methods_of_computing_square_roots">https://en.wikipedia.org/wiki/Methods_of_computing_square_roots</a></p>

<p class="bibliomixed"><a id="[Wikipedia_2]"></a>[Wikipedia_2] <a href="https://en.wikipedia.org/wiki/Denormal_number">https://en.wikipedia.org/wiki/Denormal_number</a></p>

<p class="bibliomixed"><a id="[Wikipedia_3]"></a>[Wikipedia_3] <a href="https://en.wikipedia.org/wiki/NaN">https://en.wikipedia.org/wiki/NaN</a></p>

<p class="bibliomixed"><a id="[Wikipedia_4]"></a>[Wikipedia_4] <a href="https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method">https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method</a></p>

<p class="bibliomixed"><a id="[Wikipedia_5]"></a>[Wikipedia_5] <a href="https://en.wikipedia.org/wiki/Compass-and-straightedge_construction">https://en.wikipedia.org/wiki/Compass-and-straightedge_construction</a></p>

<p class="bibliomixed"><a id="[Wikipedia_6]"></a>[Wikipedia_6] <a href="https://en.wikipedia.org/wiki/Newton%27s_method">https://en.wikipedia.org/wiki/Newton%27s_method</a></p>

<p class="bibliomixed"><a id="[Wikipedia_7]"></a>[Wikipedia_7] <a href="https://en.wikipedia.org/wiki/Householder%27s_method">https://en.wikipedia.org/wiki/Householder%27s_method</a></p>

<p class="bibliomixed"><a id="[Wikipedia_8]"></a>[Wikipedia_8] <a href="https://en.wikipedia.org/wiki/Rate_of_convergence">https://en.wikipedia.org/wiki/Rate_of_convergence</a></p>

<p class="bibliomixed"><a id="[Wikipedia_9]"></a>[Wikipedia_9] <a href="https://en.wikipedia.org/wiki/Fast_inverse_square_root">https://en.wikipedia.org/wiki/Fast_inverse_square_root</a></p>

<p class="bibliomixed"><a id="[Wikipedia_10]"></a>[Wikipedia_10] <a href="https://en.wikipedia.org/wiki/Bogosort">https://en.wikipedia.org/wiki/Bogosort</a></p>

<p class="bibliomixed"><a id="[Wikipedia_11]"></a>[Wikipedia_11] <a href="https://en.wikipedia.org/wiki/Square_root">https://en.wikipedia.org/wiki/Square_root</a></p>

<p class="bibliomixed"><a id="[xkcd_1]"></a>[xkcd_1] <a href="http://www.xkcd.com/664/">http://www.xkcd.com/664/</a></p>

<p class="bibliomixed"><a id="[xkcd_2]"></a>[xkcd_2] <a href="https://xkcd.com/356/">https://xkcd.com/356/</a></p>

<p class="bibliomixed"><a id="[YBC7289]"></a>[YBC7289] <a href="https://www.math.ubc.ca/~cass/Euclid/ybc/analysis.html">https://www.math.ubc.ca/~cass/Euclid/ybc/analysis.html</a></p>

<p class="footnotes"></p>
<ol>
	<li><a id="FN01"></a>Why are we using questions?</li>
	<li><a id="FN02"></a>For IEEE 754 double, the maximum sqrt will exceed the maximum value for IEEE 754 float, so this forces us to consider the same return type as the input type.</li>
	<li><a id="FN03"></a>These might be using dedicated FPU hardware or native CPU commands. In the silicon itself, one might find GoldSchmidtâ€™s method, or Newton Raphson; Some Assembly Required [<a href="#[SAR]">SAR</a>] has a large number of interesting comparisons, including old and modern native SQRT instructions.</li>
	<li><a id="FN04"></a>When multiplied, powers are added, hence sqrt is pow(0.5). Two very good examples of working through this identity are available at [<a href="#[SO_1]">SO_1</a>].</li>
</ol>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
