<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Shape of Code &#187; Maxima</title>
	<atom:link href="http://shape-of-code.coding-guidelines.com/tag/maxima/feed/" rel="self" type="application/rss+xml" />
	<link>http://shape-of-code.coding-guidelines.com</link>
	<description></description>
	<lastBuildDate>Sun, 29 Jan 2012 23:49:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Variations in the literal representation of Pi</title>
		<link>http://shape-of-code.coding-guidelines.com/2010/03/12/variations-in-the-literal-representation-of-pi/</link>
		<comments>http://shape-of-code.coding-guidelines.com/2010/03/12/variations-in-the-literal-representation-of-pi/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 01:54:45 +0000</pubDate>
		<dc:creator>Derek-Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[floating-point]]></category>
		<category><![CDATA[Fortran]]></category>
		<category><![CDATA[Maxima]]></category>
		<category><![CDATA[Pi]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://shape-of-code.coding-guidelines.com/?p=183</guid>
		<description><![CDATA[The numbers system I am developing attempts to match numeric literals contained in a file against a database of interesting numbers. One of the things I did to quickly build a reasonably sized database of reliable values was to extract numeric literals from a few well known programs that I thought I could trust. R [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.coding-guidelines.com/numbers">numbers</a> system I am developing attempts to match numeric literals contained in a file against a <a href="http://shape-of-code.coding-guidelines.com/2010/02/using-numeric-literals-to-identify-application-domains/">database of interesting numbers</a>.  One of the things I did to quickly build a reasonably sized database of reliable values was to extract numeric literals from a few well known programs that I thought I could trust.</p>
<p><a href="http://en.wikipedia.org/wiki/R_%28programming_language%29">R</a> is a widely used statistical package and <a href="http://en.wikipedia.org/wiki/Maxima_%28software%29">Maxima</a> is a computer algebra system with a long history.  Both contain a great deal of functionality and are actively maintained.</p>
<p>To my surprise the source code of both packages contain a large variety of different literal values for <a href="http://en.wikipedia.org/wiki/Pi"><img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_8edb2cf68079344a2edd739531259f6c.png" style="vertical-align:-6.5px; display: inline-block ;" alt="pi" title="pi"/></a>, or to be exact the number of digits contained in the literals varied by more than I expected.  In the following table the value to the left of the <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_8edb2cf68079344a2edd739531259f6c.png" style="vertical-align:-6.5px; display: inline-block ;" alt="pi" title="pi"/> representation is the number of occurrences; values listed in increasing literal order:</p>
<pre>     Maxima                              R
   2 3.14159
                                      14 3.141592
   1 3.1415926
   1 3.14159265                        2 3.14159265
   3 3.1415926535
   4 3.14159265358979
  14 3.141592653589793
   3 3.1415926535897932385             3 3.1415926535897932385
   9 3.14159265358979324
                                       1 3.14159265359
                                       1 3.1415927
                                       1 3.141593</pre>
<p>The comments in the Maxima source led me to believe that some thought had gone into ensuring that the numerical routines were robust.  Over 3/4 of the literal representations of <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_8edb2cf68079344a2edd739531259f6c.png" style="vertical-align:-6.5px; display: inline-block ;" alt="pi" title="pi"/> have a precision comparable to at least that of 64-bit floating-point  (I&#8217;m assuming an <a href="http://en.wikipedia.org/wiki/IEEE_754-1985">IEEE 754</a> representation in this post).</p>
<p>In the R source approximately 2/3 of the literal representations of <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_8edb2cf68079344a2edd739531259f6c.png" style="vertical-align:-6.5px; display: inline-block ;" alt="pi" title="pi"/> have a precision comparable to that of 32-bit floating-point.</p>
<p>Closer examination of the source suggests one reason for this difference.  Both packages make heavy use of existing code (translated from Fortran to Lisp for Maxima and from Fortran to C for R); using existing code makes good sense and because of its use in scientific and engineering applications many numerical libraries have been written in Fortran.  Maxima has adapted the <a href="http://www.netlib.org/slatec/">slatec</a> library, whereas the R developers have used a variety of different libraries (e.g., <a href="http://www.netlib.org/specfun/index.html">specfun</a>).</p>
<p>How important is variation in the representation of Pi?</p>
<ul>
<li>A calculation based on a literal that is only accurate to 32-bits is likely to be limited to that level of accuracy (unless errors cancel out somewhere).
</li>
<li>Inconsistencies in the value used to represent Pi are a source of error.  These inconsistencies may be implicit, for instance literals used to denote a value derived from <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_8edb2cf68079344a2edd739531259f6c.png" style="vertical-align:-6.5px; display: inline-block ;" alt="pi" title="pi"/> such as <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993_069e789797479fd125c6b856dfb909ab.png" style="vertical-align:-7px; display: inline-block ;" alt="pi^0.5" title="pi^0.5"/> often seem to be be based on more precise values of Pi than appear in the code.</li>
</ul>
<p>The obvious solution to this representation issue of creating a file containing definitions of all of the frequently used literal values has possible drawbacks.  For instance, numerical accuracy is a strange beast and increasing the precision of one literal without doing the same for other literals appearing in a calculation can sometimes reduce the accuracy of the final result.</p>
<p>Pulling together existing libraries to build a package is often very cost effective, but numerical accuracy is a slippery beast and this inconsistent usage of literals suggests that developers from these two communities have not addressed the system level consequences of software reuse.</p>
<p><strong>Update</strong> 6 April: After further rummaging around in the R source distribution I found that things are not as bad as they first appear.  Only two of the single precision instances of <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_8edb2cf68079344a2edd739531259f6c.png" style="vertical-align:-6.5px; display: inline-block ;" alt="pi" title="pi"/> listed above occur in the C or Fortran source code, the rest appear in support files (e.g., m4 scripts and R examples).</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fshape-of-code.coding-guidelines.com%2F2010%2F03%2F12%2Fvariations-in-the-literal-representation-of-pi%2F&amp;title=Variations%20in%20the%20literal%20representation%20of%20Pi" id="wpa2a_2"><img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://shape-of-code.coding-guidelines.com/2010/03/12/variations-in-the-literal-representation-of-pi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

