    <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  :: More Notes on Code Critique Competition 12</title>
        <link>https://members.accu.org/index.php/articles/1161</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">CVu Journal Vol 14, #2 - Apr 2002</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/c76/">Journals</a>

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c115/">142</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;More Notes on Code Critique Competition 12</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 April 2002 13:15:50 +01:00 or Wed, 03 April 2002 13:15:50 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Francis Glassborow's Student Code Critique Competition is a
section of C Vu that I ensure I read in every issue. I can usually
make a start at them, to the point of knowing what I would do next
to isolate the problems.</p>
<p>But sticking my neck out and attempting to complete them is
something I prefer to leave to those with more time and
understanding of 'C' so you will generally not find any response
from me to this particular column. And anyway, those who wish to
take a swing at me will find quite enough material of mine with my
neck over extended elsewhere in the pages of this Journal.</p>
<p>So Competition Number 12, on the C++ example finding square
roots by Newton's method has been set, the responses printed in the
December 2001 issue of C Vu and the prize awarded. Yet I think the
evidence is incomplete on one point. So perhaps I will write a few
words on this particular competition.</p>
<p>Let me first say that I know a little of 'C', but nothing of
'C++'. The example is in the latter, but I wish to make my comments
with respect to the former. So I hope that someone will say &quot;all
you have to do to correct this in 'C++' is...&quot;. If that someone
responds that there is no fix in 'C++', then I think the Standards
Committee needs another session.</p>
<p>The problem is the line:</p>
<pre class="programlisting">
const double TOLERANCE = 1.0e-7;
</pre>
<p>This variable is used to detect when the difference between the
square of the square root estimate and the number being rooted is
so small that further calculations will not further refine the
estimate. This happens when that difference approaches the accuracy
of the floating-point numbers on the target machine. To further
refine the estimate would require numbers more accurate than a
floating-point number can hold. And a pretty good estimate of that
point on the usual 32-bit floating point numbers is, of course,
1.0e-7 which is the value used.</p>
<p>And as The Harpist observes, this value seems pretty generous
given that the example uses double precision floating point numbers
throughout. A value of 1.0e-15, or perhaps slightly smaller, would
be more appropriate in this case.</p>
<p>But who said anything about the example using 32-bit single
precision, or 64-bit double precision, numbers?</p>
<p>ICL could process 24 bit floating-point numbers when asked. All
right, those were quite limited and would now be prohibited by the
'C' standard. At the other end of the scale, CDC (a long since
deceased supplier of behemoths) used 60 bits for their floating
point numbers. With Intel working on their IA-64 architecture, we
cannot be far off the day when every desktop machine uses 64 bits
for single precision and 128 bits for double precision floating
point numbers. Some workstations already do. And as for <tt class=
"type">long double</tt>...</p>
<p>So how do you find out what a sensible value for the <tt class=
"constant">TOLERANCE</tt> variable is on your machine?</p>
<p>The 'C' standard (and this is where I switch into 'C', and
invite someone to extend this piece to 'C++') has thought of this
problem and specified two header files: <tt class=
"filename">&lt;limits.h&gt;</tt> and <tt class=
"filename">&lt;float.h&gt;</tt>. They tell you useful things about
the numbers on the computer you are using. <tt class=
"filename">&lt;limits.h&gt;</tt> deals with the sizes of, and
largest and smallest numbers that can be held in, character and
integer variables. For instance, this is the place to look if you
want to know how many bits there are in a byte: it may be more than
8. Look at <tt class="constant">CHAR_BIT</tt> to see what your
compiler does on your machine.</p>
<p>But I digress: the file of interest in this problem is
<tt class="filename">&lt;float.h&gt;</tt>, which tells us all about
how the compiler implements floating point numbers, of whatever
length, on the target hardware. And <tt class=
"constant">FLT_EPSILON</tt> gives the relative accuracy to which
numbers can be stored in a single precision floating point number,
which is one of type <tt class="type">float</tt>. <tt class=
"constant">DBL_EPSILON</tt> and <tt class=
"constant">LDBL_EPSILON</tt> do the same for <tt class=
"type">double</tt> and <tt class="type">long double</tt> floating
point numbers respectively.</p>
<p>The temptation is to initialise <tt class=
"constant">TOLERANCE</tt> with the value of <tt class=
"constant">DBL_EPSILON</tt> and then, as The Harpist says, scale
this by <tt class="varname">x</tt> which is the number whose square
root is to be found.</p>
<p>This is necessary because all the *_EPSILON symbols are actually
&quot;the difference between 1.0 and the least number greater then 1.0
that is representable in the given floating point type.&quot; Or, if you
prefer, the difference between 1.0 and the next largest number. So
<tt class="constant">DBL_EPSILON</tt> says the sort of accuracy you
will get near 1.0 for arithmetic with <tt class="type">double</tt>,
you have to multiply it by <tt class="varname">x</tt> to find the
accuracy you can expect near <tt class="varname">x</tt>.</p>
<p>But to do so may, and probably will, result in the loop in
<tt class="function">sqroot</tt> failing to terminate for certain
arguments. Why? Because the computer hardware can only approximate
the result of each operation required to form the next estimate of
the square root. That is, each operation introduces a small error
into the new estimate. So the square of the estimated square root
may never get as close to <tt class="varname">x</tt> as <tt class=
"literal">x * DBL_EPSILON</tt>.</p>
<p>Newton-Raphson iteration, which is the generalisation of
Newton's method, is usually well behaved when finding a square
root, so this extra error introduced by the computer is normally
unimportant. But when the estimate is close to the value of the
square root, this can result in successive estimates which
oscillate either side of the true value, but neither of which is
within <tt class="literal">x * DBL_EPSILON</tt> of the true
value.</p>
<p>I spent Sunday morning trying to find an example of this
happening and failed. However, if you try the approach of
<tt class="function">sqroot</tt> with integer arithmetic (and
remember that on divide, integer arithmetic always discards the
remainder so that the result is truncated towards zero) you will
get results with the numbers 1022 and 1023 which demonstrate the
sort of problems that can arise. Both of these numbers have square
roots just less than 32.</p>
<p>Trying to find the square root of 1023 will give estimates which
alternate between 31 and 32. So a <tt class=
"constant">TOLERANCE</tt> of 1, which is the 'epsilon' for integer
arithmetic, will stop the iteration with a value of 32. But not
31.</p>
<p>With an argument of 1022, the approach yields a square root of
31, which does not change with further iteration, and whose square
is 961. The error is 61 and no sensible value of <tt class=
"constant">TOLERANCE</tt> will detect that so the loop will run
forever.</p>
<p>The solution is to make <tt class="constant">TOLERANCE</tt> a
little bigger. If <tt class="varname">n</tt> arithmetic operations
are required to form each estimate (and <tt class="varname">n</tt>
is 5 here: an add, two divides, a multiply and a subtract), then
somewhere between <tt class="function">sqrt(n)</tt> and <tt class=
"varname">n</tt> times <tt class="constant">DBL_EPSILON</tt> should
be sufficient.</p>
<p>So the original line must be replaced with the two lines of:</p>
<pre class="programlisting">
#include &lt;float.h&gt;
const double TOLERANCE = 5 * DBL_EPSILON;
</pre>
<p>I have chosen to multiply by 'n' here.' If you want a more
accurate approximation, you could try a smaller multiple at the
risk of the iteration never terminating. You can stop that by
counting the number of iterations and stopping when this becomes
too large. You can try predicting what 'too large' means: I would
guess more than 10 iterations for 'double' arithmetic, but you
really have to try it and see.</p>
<p>As a finale, if you really want to evaluate a square root, or
any other mathematical function on a computer, about the only book
which will tell you how to is the <i class="citetitle">Software
Manual for the Elementary Functions</i> by William J Cody, Jr, and
William Waite, published by Prentice-Hall, 1980, ISBN:
0-13-822064-6.</p>
<p>It reads more like a cookbook than a textbook, and has no
mathematics at all in it, just flow charts with careful
explanations for you to code. This is a 'how to' book, not a 'why'
book. The book tells you how to check that you coded the flow chart
correctly for your chosen function by checking both some random
general, and specified critical, cases. Interestingly, it does this
by presenting a FORTRAN listing, not another flow chart.</p>
<p>My only quibble is that this book does now need another edition
to cater for the larger precision computers that are starting to
appear which provide floating point numbers of 128 bits or more.
Such an edition could also address the &quot;photo-reduced typescript&quot;
look of the current edition which was caused by the production
techniques in vogue when it was published.</p>
<p>As I now seem to have slipped a book review into the 'Features'
section, I am going to stop before the Editor notices. But please
keep those Code Critiques coming: I have never found as many points
in the problem set as any of your published Code Critiques.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
