<?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>My Piece of the Inter(.)net &#187; code</title>
	<atom:link href="http://mypieceoftheinter.net/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://mypieceoftheinter.net</link>
	<description>A place for my geekish ramblings or whatever I find interesting.</description>
	<lastBuildDate>Mon, 16 Jan 2012 16:35:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Cowsay as much as possible</title>
		<link>http://mypieceoftheinter.net/archives/1942</link>
		<comments>http://mypieceoftheinter.net/archives/1942#comments</comments>
		<pubDate>Fri, 08 Jan 2010 01:39:00 +0000</pubDate>
		<dc:creator>David McGrath</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Cowsay]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Silly]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://mypieceoftheinter.net/archives/1942</guid>
		<description><![CDATA[CowsayIfPossible() { if [ -z &#34;${1}&#34; ]; then CowsayIfPossible &#34;Supply some text!&#34;; return; fi if which cowsay &#62;&#62; /dev/null; then cowsay &#34;${1}&#34; echo ======================================= else echo &#34;${1}&#34; fi } Cowsay is win.&#160; EEEPPIIIICCC&#160; WIIIIN. This BASH script snippet allows the unashamed use of cowsay within bash scripts without fear that it isn’t installed.&#160; World peace?&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>
<pre></pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">CowsayIfPossible()
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">{
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">  if [ -z &quot;${1}&quot; ]; then
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">    CowsayIfPossible &quot;Supply some text!&quot;;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">    return;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">  fi
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">  if which cowsay &gt;&gt; /dev/null; then
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">    cowsay &quot;${1}&quot;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">    echo =======================================
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">  else
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">    echo &quot;${1}&quot;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">  fi
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%; margin: 0em">}</pre>
</p>
<p><a href="http://en.wikipedia.org/wiki/Cowsay">Cowsay</a> is win.&#160; EEEPPIIIICCC&#160; WIIIIN.</p>
<p>This BASH script snippet allows the unashamed use of cowsay within bash scripts without fear that it isn’t installed.&#160; World peace?&#160; That’d be nice, but for now cowsay moooooo!</p>
<div id='extendedEntryBreak' name='extendedEntryBreak'></div>
<p>sudo aptitude install cowsay</p>
]]></content:encoded>
			<wfw:commentRss>http://mypieceoftheinter.net/archives/1942/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pi Calculator</title>
		<link>http://mypieceoftheinter.net/archives/351</link>
		<comments>http://mypieceoftheinter.net/archives/351#comments</comments>
		<pubDate>Sun, 24 Feb 2008 05:12:52 +0000</pubDate>
		<dc:creator>David McGrath</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[pi]]></category>
		<category><![CDATA[Visual Studio Express]]></category>
		<category><![CDATA[π]]></category>

		<guid isPermaLink="false">http://mypieceoftheinter.net/archives/351</guid>
		<description><![CDATA[Everyone loves π!  I was bored today, so obviously I got the urge to make a program to calculate it.  (Who doesn&#8217;t, right?)  A friend of mine has hacked one together in Java, but I feel the need to show him how it&#8217;s done in C#.  There&#8217;s nothing wrong with a little healthy competition after [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone loves π!  I was bored today, so <em>obviously</em> I got the urge to make a program to calculate it.  (Who doesn&#8217;t, right?)  A friend of mine has hacked one together in <a href="http://en.wikipedia.org/wiki/Java_%28programming_language%29">Java</a>, but I feel the need to show him how it&#8217;s done in <a href="http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29">C#</a>.  There&#8217;s nothing wrong with a little healthy competition after all.  Bring it on! <img src='http://mypieceoftheinter.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The first challenge was to figure out <em>how</em> to calculate π.  I came up with <a href="http://staff.washington.edu/skykilo/Pi/Pi.html">this</a> website.  I decided to use &#8220;Gregory&#8217;s Formula&#8221; at the bottom of the page, which is an alternating series.</p>
<p>Here&#8217;s what I saw on the website:<br />
<img width="368" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/01-equation-1.png" alt="basic concept" height="63" /></p>
<p>So that means that&#8230;<br />
<img width="181" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/02-equation-2.png" alt="intermediate equation" height="88" /></p>
<p>and finally&#8230;<br />
<img width="198" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/03-equation-3.png" alt="magical equation of awesome" height="84" /></p>
<p>The last equation is just the calculus version of the first one.  In either way the basic pattern is that I start with 1/1, subtract 1/3, add 1/5, and so on.  Then at whatever point I want multiply that resulting summation by 4 to get an approximation of π.  The fun part is translating that into code.</p>
<p>So what <em>does</em> that look like in code? Well, I made a whole PiCalculator class, but this is the part that runs over and over:<br />
<img width="382" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/04-startcalculating-method.png" alt="startCalculating method" height="293" /></p>
<p>And whenever Pi is requested, it just multiplies aFourh by 4.<br />
<img width="273" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/05-pi-accessor.png" alt="pi accessor" height="123" /></p>
<p>That&#8217;s the basic idea.  There&#8217;s more code to initiate the calculation process (and run it on a separate thread), a bunch of accessors so that the main program can get information to display, and finally there&#8217;s code to actually display all of the information.</p>
<p>When the program is first opened it looks like this:<br />
<img width="512" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/06-beginning.png" alt="inital view" height="301" /></p>
<p>It&#8217;s calculating π in the background, but for performance sake it only shows the user the values when the &#8220;Update&#8221; button or enter is pressed.<br />
<img width="521" src="http://mypieceoftheinter.net/wp-content/uploads/2008/02/07-30-seconds.png" alt="after 30 seconds" height="301" /></p>
<p>After 30 seconds it has more pi than any sane person would care about.</p>
<p>One thing I had to look out for was numbers that were too extreme.  Eventually that fraction that is being added and subtracted in the summation is going to be rounded to zero, so I account for that in the code, stopping the calculations in that event.  As you can see, though, even with my computer&#8217;s <a href="http://mypieceoftheinter.net/archives/46">Intel Core 2 Quad Q660</a>, I would have to run this program for several years to have that happen.</p>
<p>So this was a fun little project.  Feel free to try it out for yourself!</p>
<p><a href="http://download.somewhatgeeky.com?downloadID=pi_calculator_version_1.0.0.0">Download the executable here</a></p>
<p><a href="http://download.somewhatgeeky.com?downloadID=pi_calculator_version_1.0.0.0_source">Download the C# source code here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mypieceoftheinter.net/archives/351/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

