<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.exploringbinary.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Exploring Binary</title>
	
	<link>http://www.exploringbinary.com</link>
	<description>Binary Numbers, Binary Code, and Binary Logic</description>
	<lastBuildDate>Tue, 29 May 2012 13:57:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.exploringbinary.com/ExploringBinary" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="exploringbinary" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">ExploringBinary</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Bicimals</title>
		<link>http://www.exploringbinary.com/bicimals/</link>
		<comments>http://www.exploringbinary.com/bicimals/#comments</comments>
		<pubDate>Wed, 23 May 2012 14:13:02 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Decimals]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=372</guid>
		<description><![CDATA[There is no widely accepted term for fractional binary numbers like 0.11001. A fractional decimal number like 0.427 is called a decimal or decimal fraction. A fractional binary number is called many things, including binary fraction, binary decimal, binary expansion, bicimal, binimal, binary radix fraction, and binary fractional (my term). In this article, I&#8217;m going [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/bicimals/">Bicimals</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There is no widely accepted term for fractional binary numbers like 0.11001. A fractional decimal number like 0.427 is called a <em>decimal</em> or <em>decimal fraction</em>. A fractional binary number is called many things, including <em>binary fraction</em>, <em>binary decimal</em>, <em>binary expansion</em>, <em>bicimal</em>, <em>binimal</em>, <em>binary radix fraction</em>, and <em>binary fractional </em> (<a title="Read Rick Regan's Article &ldquo;Base Conversion in PHP Using BCMath&rdquo;" href="http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/">my term</a>). In this article, I&#8217;m going to argue that <strong>bicimal</strong> should be the universal term.</p>
<p>(Please let me know what you think &#8212; take the poll at the end of this article.)</p>
<p><span id="more-372"></span></p>
<h2>Why I Don&#8217;t Like &lsquo;Binary Fraction&rsquo;</h2>
<p>Of all the existing terms, <em>binary fraction</em> is probably the most commonly used. I don&#8217;t like it because its analog, <em>decimal fraction</em>, is not clearly defined. I want to avoid a term that inherits this problem.</p>
<p><em>Decimal fraction</em> is commonly defined as any number with an explicit or implicit power of ten denominator, either entirely fractional or not. For example, 254/1000, 0.254, 15/10, and 1.5 are decimal fractions. But what about 2/5? It can be written as 4/10 or 0.4, although as written its denominator is not a power of ten. And what about 1/3? Its equivalent form as a decimal is 0.<span style="text-decoration:overline">3</span> &#8212; a repeating decimal. It can never be written with a power of ten denominator. To make things more confusing, 2/5 and 1/3 are fractions &#8212; fractions written in decimal numerals.</p>
<p>You could argue that <em>decimal fraction</em> includes 2/5 but excludes 1/3 and still have a reasonable definition. However, I&#8217;m looking for the equivalent of <em>decimal</em>, a term which includes 0.4 and 0.<span style="text-decoration:overline">3</span>, but excludes 2/5, 4/10, and 1/3.</p>
<h2>&lsquo;Bicimal&rsquo;</h2>
<p>I discovered the term <em>bicimal</em> <a title="Google Search for &ldquo;bicimal&rdquo;" href="http://www.google.com/search?q=bicimal+binary">on the Web</a> and in <a title="Google Books Search for &ldquo;bicimal&rdquo;" href="http://www.google.com/search?q=bicimal&#038;btnG=Search+Books&#038;tbm=bks&#038;tbo=1">Google Books</a>, but I don&#8217;t know its origin. I pronounce it &ldquo;bye’ suh mull&rdquo;, or as <a title="Merriam-Webster Dictionary Pronunciation Guide" href="http://www.merriam-webster.com/help/pronguide.htm">Merriam-Webster</a> might express it, \<span class="unicode">ˈ</span>bī-sə-məl\. A bicimal is built with <a title="Read Rick Regan's Article &ldquo;A Table of Negative Powers of Two&rdquo;" href="http://www.exploringbinary.com/a-table-of-negative-powers-of-two/">negative powers of two</a>, whereas a decimal is built with negative powers of ten.</p>
<p>Like the term <em>decimal</em>, <em>bicimal</em> usually means a pure fractional value, like 0.11001. However, in some contexts, it could mean numbers with a whole and fractional part, like 101.11. In this case, <a title="Read Rick Regan's Article &ldquo;A Table of Nonnegative Powers of Two&rdquo;" href="http://www.exploringbinary.com/a-table-of-nonnegative-powers-of-two/">nonnegative powers of two</a> come into play &#8212; for the whole part.</p>
<h2>Why I Like &lsquo;Bicimal&rsquo;</h2>
<p>Ideally, there would be a base-independent term for the fractional part of a number. I invented the term <a title="Read Rick Regan's Article &ldquo;Base Conversion in PHP Using BCMath&rdquo;" href="http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/">fractional</a> for this purpose. I&#8217;ve called a fractional decimal number a <em>decimal fractional</em>, and a fractional binary number a <em>binary fractional</em>. The purpose of this new term was to separate the form of a number &#8212; a number with a &ldquo;point&rdquo; in it &#8212; from its base. If 0.427 is a decimal, does that make 0.11001 a binary decimal? You can see why we need a better term.</p>
<p>One problem with my terminology is that I&#8217;ve created two terms (<em>decimal fractional</em> and <em>binary fractional</em>) when I really only needed to create one. Why not stick with <em>decimal</em> and invent a new term just for binary? <em>Decimal</em> is easier to say than <em>decimal fractional</em>, and everyone knows what it means. So what&#8217;s a good replacement for binary fractional? I&#8217;ve come to like the term <strong>bicimal</strong>.</p>
<p>At first glance, there&#8217;s not much to like about <em>bicimal</em>. It&#8217;s base-dependent, and it is a <a title="Wikipedia article on portmanteaus" href="http://en.wikipedia.org/wiki/Portmanteau">portmanteau</a> for <em>binary decimal</em>. It is a poorly formed portmanteau at that. While the prefix &lsquo;bi-&rsquo; is perfectly acceptable, its pairing with the suffix &lsquo;-cimal&rsquo; seems ill-formed. <em>Binimal</em> seems linguistically the better choice; it swaps out the prefix &lsquo;dec-&rsquo; for the prefix &lsquo;bin-&rsquo; and retains the suffix &lsquo;-imal&rsquo;.</p>
<p>On the other hand, say <em>bicimal</em> and <em>binimal</em> outloud, over and over; I think you&#8217;ll find that <em>bicimal</em> sounds better, as do its associated terms: <strong>bicimal point</strong>, <strong>bicimal places</strong>, <strong>bicimal part</strong>, <strong>terminating bicimal</strong>, <strong>repeating bicimal</strong>, <strong>infinite bicimal</strong>, etc. And <em>bicimal</em> produces natural sounding phrases like &ldquo;multiply bicimals&rdquo;, &ldquo;convert a decimal to a bicimal&rdquo;, &ldquo;convert a bicimal to a decimal&rdquo;, &ldquo;convert a bicimal to a fraction&rdquo;, &ldquo;convert a fraction to a bicimal&rdquo;, etc. </p>
<p>I think <em>bicimals</em> will be immediately understood by newcomers. It evokes all the feelings and terminology and operations of <em>decimals</em> (for better or worse <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ). I don&#8217;t think <em>binimals</em> &#8212; or any of the alternative terms &#8212; has this property. So all things considered, I like <em>bicimal</em> the best.</p>
<h2>What Do You Think?</h2>
<p>Please leave a comment or take the poll (Polldaddy poll is available only if Javascript is enabled). If you like <em>binary fraction</em>, please tell me why.</p>
<p><script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6253454.js"></script><br />
<noscript><a href="http://polldaddy.com/poll/6253454/">Which term do you prefer for fractional binary numbers like 0.11001?</a></noscript></p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/bicimals/">Bicimals</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/bicimals/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Bug in the Bigcomp Function of David Gay’s strtod()</title>
		<link>http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/</link>
		<comments>http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 13:08:38 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Numbers in computers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Floating-point]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=370</guid>
		<description><![CDATA[Last week, a reader of my blog, Geza Herman, told me about a bug he found in David Gay&#8217;s strtod() function. In random testing of decimal numbers nearly halfway between double-precision floating-point numbers, he discovered this 53-digit number, which converts incorrectly: 1.8254370818746402660437411213933955878019332885742187 As Geza noted, the problem is in the bigcomp() function, an optimization that [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/">A Bug in the Bigcomp Function of David Gay’s strtod()</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Last week, a reader of my blog, Geza Herman, told me about a bug he found in <a title="David Gay's dtoa.c" href="http://www.netlib.org/fp/dtoa.c">David Gay&#8217;s strtod() function</a>. In random testing of decimal numbers nearly halfway between double-precision floating-point numbers, he discovered this 53-digit number, which converts incorrectly:</p>
<p>1.8254370818746402660437411213933955878019332885742187</p>
<p>As Geza noted, the problem is in the bigcomp() function, an optimization that kicks in for long decimal inputs. I traced his example through bigcomp() &#8212; I&#8217;ll show you what&#8217;s going on.</p>
<p><span id="more-370"></span>(Geza has reported the bug, and David Gay has already fixed it.)</p>
<h2>strtod() Returns a Value That Is One ULP Too High</h2>
<p><a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">In binary</a>, 1.8254370818746402660437411213933955878019332885742187 is a number with a non-terminating binary fraction portion; here are its first 175 significant bits:</p>
<pre class="noborder">
1.1101001101001111110110000011011110001110101010000011<span class="highlight_gray4">0</span>111111111111
1111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111110...
</pre>
<p>This is extremely close to &#8212; but less than &#8212; <a title="Read Rick Regan's article &ldquo;Incorrectly Rounded Conversions in Visual C++&rdquo;" href="http://www.exploringbinary.com/incorrectly-rounded-conversions-in-visual-c-plus-plus/#halfway-case">halfway between two double-precision numbers</a>: it has a 0 at significant bit 54 (highlighted), followed by 120 1s. (A halfway case has a 1 at bit 54 followed by all 0s.)</p>
<p>Correctly rounded to the nearest double-precision value this is</p>
<p>1.1101001101001111110110000011011110001110101010000011,</p>
<p>or 0&#120;1.d34fd8378ea83p+0 in <a title="Read Rick Regan's article &ldquo;Hexadecimal Floating-Point Constants&rdquo;" href="http://www.exploringbinary.com/hexadecimal-floating-point-constants/">hexadecimal &ldquo;%a&rdquo; notation</a>.</p>
<p>In decimal, this equals</p>
<p>1.8254370818746401550214386588777415454387664794921875.</p>
<p>strtod() returns 0&#120;1.d34fd8378ea84p+0, which is 1 binary ULP too high.</p>
<h2>What strtod() Does</h2>
<p>strtod() starts by computing its <a title="Read Rick Regan's Article &ldquo;strtod()&rsquo;s Initial Decimal to Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/">floating-point approximation</a> as 0&#120;1.d34fd8378ea83p+0 (which happens to be the correct result). Next, it uses big integer arithmetic to <a title="Read Rick Regan's Article &ldquo;Using Integers to Check a Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">compare the approximation to the decimal input</a>. With the bigcomp optimization enabled, which it is by default, only the first 18 digits of the decimal input are used for the comparison; this truncation, in a close case like this, means strtod() must call <a title="Read Rick Regan's Article &ldquo;Bigcomp: Deciding Truncated, Near Halfway Conversions&rdquo;" href="http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/">bigcomp()</a> to determine if the approximation is correct.</p>
<h3>bigcomp()</h3>
<p>bigcomp() decides which of two adjacent floating-point numbers is closest to the decimal input string. It generates the decimal digits of the number halfway between these two values, comparing them digit-by-digit to the input string. In this case, the halfway value is</p>
<p>1.82543708187464026604374112139339558780193328857421875,</p>
<p>which is the same as the input but with a &lsquo;5&rsquo; appended as the 54th digit. bigcomp() quits after examining the 53 digits of its input &#8212; as it should &#8212; but it fails to account for the implied 0 at digit 54. Mistakenly, it deems the input greater than the halfway value, when it should see that the implied 0 makes the input <em>less</em> than the halfway value.</p>
<h2>The Fix</h2>
<p>Here is the description of the fix, as <a title="Change Log for David Gay's dtoa.c" href="http://www.netlib.org/fp/changes">logged by David Gay</a>:</p>
<pre class="noborder">
20120417
 dtoa.c:  augment a test in bigcomp() to correctly compare a long
input string of digits with a computed string after all of the
input string has otherwise been processed.  This matters only
rarely; a string for which it matters (reported by Herman Geza <em>[sic]</em>)
is 1.8254370818746402660437411213933955878019332885742187 .
</pre>
<p>The fix changes one line of code; this line</p>
<pre class="noborder">
if (b->x[0] || b->wds > 1)
</pre>
<p>is changed to this:</p>
<pre class="noborder">
if (b->x[0] || b->wds > 1 <strong>|| dig > 0</strong>)
</pre>
<p>In this example, dig = 5, so the &lsquo;if&rsquo; branch is taken, resulting in the smaller of the two neighboring values to be chosen.</p>
<h3>Alternate Fix</h3>
<p>You can avoid the bug by disabling bigcomp, which you can do by &ldquo;&#35;defining&rdquo; the variable &ldquo;NO_STRTOD_BIGCOMP&rdquo;.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/">A Bug in the Bigcomp Function of David Gay’s strtod()</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Binary Division</title>
		<link>http://www.exploringbinary.com/binary-division/</link>
		<comments>http://www.exploringbinary.com/binary-division/#comments</comments>
		<pubDate>Sat, 31 Mar 2012 19:26:32 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Binary arithmetic]]></category>
		<category><![CDATA[Binary integers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Convert to decimal]]></category>
		<category><![CDATA[Decimals]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=368</guid>
		<description><![CDATA[This is the fourth of a four part series on &#8220;pencil and paper&#8221; binary arithmetic, which I&#8217;ve written as a supplement to my binary calculator. The first article discusses binary addition; the second article discusses binary subtraction; the third article discusses binary multiplication; this article discusses binary division. The pencil-and-paper method of binary division is [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-division/">Binary Division</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>This is the fourth of a four part series on &ldquo;pencil and paper&rdquo; binary arithmetic, which I&#8217;ve written as a supplement to my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>. The first article discusses <a title="Read Rick Regan's Article &ldquo;Binary Addition&rdquo;" href="http://www.exploringbinary.com/binary-addition/">binary addition</a>; the second article discusses <a title="Read Rick Regan's Article &ldquo;Binary Subtraction&rdquo;" href="http://www.exploringbinary.com/binary-subtraction/">binary subtraction</a>; the third article discusses <a title="Read Rick Regan's Article &ldquo;Binary Multiplication&rdquo;" href="http://www.exploringbinary.com/binary-multiplication/">binary multiplication</a>; this article discusses binary division.</em></p>
<div class="wp-caption aligncenter" style="width: 207px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryDivisionExample.png" alt="An Example of Binary Division" width="197" height="453"/><p class="wp-caption-text">Example of Binary Division</p></div>
<p>The pencil-and-paper method of binary division is the same as the pencil-and-paper method of decimal division, except that binary numerals are manipulated instead. As it turns out though, binary division is simpler. There is no need to guess and then check intermediate quotients; they are either 0 are 1, and are easy to determine by sight.</p>
<p><span id="more-368"></span></p>
<h2>Decimal Division</h2>
<p>Pencil-and-paper division, also known as long division, is the hardest of the four arithmetic algorithms. Like the other algorithms, it requires you to solve smaller subproblems of the same type. But unlike the other algorithms, there is no limited set of &ldquo;facts&rdquo; that solve all possible subproblems. Solving these division subproblems requires estimation, guessing, and checking. In addition to these division subproblems, multiplication and subtraction are required as well.</p>
<p>Let&#8217;s review how decimal division is done, so that we can set the stage for how division is done in binary. Here is an example:</p>
<div class="wp-caption aligncenter" style="width: 183px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalDivisionExample.png" alt="An Example of Decimal Division" width="173" height="405"/><p class="wp-caption-text">Example of Decimal Division</p></div>
<p>The algorithm is a series of steps, each step having these four substeps:</p>
<ol>
<li><em>Divide</em>: Divide the working portion of the dividend by the divisor. (The dividend is the number under the line.)</li>
<li><em>Multiply</em>: Multiply the quotient (a single digit) by the divisor.</li>
<li><em>Subtract</em>: Subtract the product from the working portion of the dividend. (I don&#8217;t write down minus signs &#8212; they&#8217;re implied.)</li>
<li><em>Bring down</em>: Copy down the next digit of the dividend to form the new working portion.</li>
</ol>
<p>Here&#8217;s the example again, step-by-step:</p>
<div class="wp-caption aligncenter" style="width: 347px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalDivisionExampleSteps.png" alt="Steps of Decimal Division" width="337" height="541"/><p class="wp-caption-text">Steps of Decimal Division</p></div>
<ul>
<li><strong>Step 0</strong>
<p>Does 88 go into 8? No, because it&#8217;s greater than 8. Does 88 go into 83? No, because it&#8217;s greater than 83. Does 88 go into 831? Yes, because it&#8217;s less than or equal to 831.</p>
<p>(The first step of long division, as commonly practiced, combines several steps and their substeps into one. That&#8217;s why I call this step 0. Technically, 88 goes into 8 zero times, so we should write down a 0, multiply 88 by 0, subtract 0 from 8, and then bring down the 3. Next, we should write down a 0 because 88 goes into 83 zero times, multiply 88 by 0, subtract 0 from 83, and bring down the 1. We&#8217;re just eliminating a bunch of stuff that produces superfluous leading zeros.)</p>
</li>
<li><strong>Step 1</strong>
<ol>
<li><em>Divide</em>: Does 88 go into 831? (Yes, we already know that from step 0.) How many times does it go in? 9 times. (Normally you have to guess the answer and do the multiplication and subtraction to verify you guessed correctly; I&#8217;ll just show the correct answer, to keep things from getting too messy.)</li>
<li><em>Multiply</em>: 9 x 88 = 792. (If 9 were a guess, your first check is to see if 792 is less than 831. It is, so so far, so good.)</li>
<li><em>Subtract</em>: 831 &#8211; 792 = 39. (If 9 were a guess, your second check is to see if 39 is less than 88. It is, so your guess was correct. Actually, in this case, this has to check out, since we can&#8217;t go higher than 9.)</li>
<li><em>Bring down</em>: Bring down the 2 to make 392.</li>
</ol>
</li>
<li><strong>Step 2</strong>
<ol>
<li><em>Divide</em>: Does 88 go into 392? Yes, 4 times.</li>
<li><em>Multiply</em>: 4 x 88 = 352.</li>
<li><em>Subtract</em>: 392 &#8211; 352 = 40.</li>
<li><em>Bring down</em>: Bring down the implied trailing 0 to make 400.</li>
</ol>
</li>
<li><strong>Step 3</strong>
<ol>
<li><em>Divide</em>: Does 88 go into 400? Yes, 4 times.</li>
<li><em>Multiply</em>: 4 x 88 = 352.</li>
<li><em>Subtract</em>: 400 &#8211; 352 = 48.</li>
<li><em>Bring down</em>: Bring down the implied trailing 0 to make 480.</li>
</ol>
</li>
<li><strong>Step 4</strong>
<ol>
<li><em>Divide</em>: Does 88 go into 480? Yes, 5 times.</li>
<li><em>Multiply</em>:  5 x 88 = 440.</li>
<li><em>Subtract</em>: 480 &#8211; 440 = 40.</li>
<li><em>Bring down</em>: Bring down the implied trailing 0 to make 400.</li>
</ol>
</li>
<li><strong>Step 5</strong>
<p>Stop the presses! We tried to divide 400 by 88 before &#8212; two steps ago. That means we have a two-digit cycle (<span style="text-decoration:overline">45</span>) from here on out. The answer is 9.4<span style="text-decoration:overline">45</span>.</p>
</li>
</ul>
<p>The red digits are the carries that occur during the multiplication substeps (the multiplication is done as if the divisor &#8212; the bigger number &#8212; is on top, by convention). Each red digit is crossed out before the next multiplication. To avoid clutter, I have chosen not to mark the borrows that occur during subtraction.</p>
<h3>I Picked The Hardest Type of Example</h3>
<p>My example has a multi-digit divisor, and has an answer with a remainder that I wrote as a repeating decimal. I wanted one example that showed long division to its fullest. I could have picked a problem with a single-digit divisor (which would require no guessing, assuming you know the multiplication facts), or one that produced an integer quotient, or one that produced a quotient with a fractional part that terminated. I could have expressed the fractional part as an integer remainder, or in fraction form.</p>
<h3>Other Cases</h3>
<ul>
<li>If the divisor or dividend is negative, you can remove the signs and apply the appropriate sign to the answer at the end.</li>
<li>If the divisor has a decimal point, shift the decimal point right until the divisor is an integer, and shift the dividend by the same number of places.</li>
<li>If the divisor is greater than the dividend, just proceed with the algorithm as is. Trailing zeros will be brought down to form the appropriate subproblems.</li>
</ul>
<h2>Binary Division</h2>
<p>Let&#8217;s return to the example of the introduction, 1011.11/11. Here it is broken down into steps, following the same algorithm I used for decimal numbers:</p>
<div class="wp-caption aligncenter" style="width: 430px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryDivisionExampleSteps.png" alt="Steps of Binary Division" width="420" height="842"/><p class="wp-caption-text">Steps of Binary Division</p></div>
<ul>
<li><strong>Step 0</strong>
<p>Does 11 go into 1? No, because it&#8217;s greater than 1. Does 11 go into 10? No, because it&#8217;s greater than 10. Does 11 go into 101? Yes, because it&#8217;s less than or equal to 101. (Remember, these are binary numerals; pronounce them &ldquo;one-one&rdquo;, &ldquo;one-zero&rdquo;, &ldquo;one-zero-one&rdquo;, etc.)</p>
</li>
<li><strong>Step 1</strong>
<ol>
<li><em>Divide</em>: Does 11 go into 101? (Yes, we already know that from step 0.) How many times does it go in? One time. There is no guessing. It&#8217;s easy to see 11 is less than 101, so we know it goes in. And if it goes in, it goes in only once.</li>
<li><em>Multiply</em>: 1 x 11 = 11. (Remember <a title="Read Rick Regan's Article &ldquo;Binary Multiplication&rdquo;" href="http://www.exploringbinary.com/binary-multiplication/">how simple it is to &ldquo;multiply&rdquo; a binary number by a single digit</a> &#8212; just copy the number down if that single digit is 1, or write down 0 if that single digit is 0.)</li>
<li><em>Subtract</em>: 101 &#8211; 11 = 10.</li>
<li><em>Bring down</em>: Bring down the 1 to make 101.</li>
</ol>
</li>
<li><strong>Step 2</strong>
<ol>
<li><em>Divide</em>: Does 11 go into 101? Yes, 1 time.</li>
<li><em>Multiply</em>: 1 x 11 = 11.</li>
<li><em>Subtract</em>: 101 &#8211; 11 = 10.</li>
<li><em>Bring down</em>: Bring down the 1 to make 101.</li>
</ol>
</li>
<li><strong>Step 3</strong>
<ol>
<li><em>Divide</em>: Does 11 go into 101? Yes, 1 time.</li>
<li><em>Multiply</em>: 1 x 11 = 11.</li>
<li><em>Subtract</em>: 101 &#8211; 11 = 10.</li>
<li><em>Bring down</em>: Bring down the 1 to make 101.</li>
</ol>
</li>
<li><strong>Step 4</strong>
<ol>
<li><em>Divide</em>: Does 11 go into 101? Yes, 1 time.</li>
<li><em>Multiply</em>: 1 x 11 = 11.</li>
<li><em>Subtract</em>: 101 &#8211; 11 = 10.</li>
<li><em>Bring down</em>: Bring down the 0 to make 100.</li>
</ol>
</li>
<li><strong>Step 5</strong>
<ol>
<li><em>Divide</em>: Does 11 go into 100? Yes, 1 time.</li>
<li><em>Multiply</em>: 1 x 11 = 11.</li>
<li><em>Subtract</em>: 100 &#8211; 11 = 1.</li>
<li><em>Bring down</em>: Bring down the 0 to make 10.</li>
</ol>
</li>
<li><strong>Step 6</strong>
<ol>
<li><em>Divide</em>: Does 11 go into 10? No (write down a 0).</li>
<li><em>Multiply</em>: (We don&#8217;t need to record this step; we&#8217;re just going to get 0.)</li>
<li><em>Subtract</em>: (We don&#8217;t need to record this step; we&#8217;re just going to get 10.)</li>
<li><em>Bring down</em>: Bring down the 0 to make 100.</li>
</ol>
</li>
<li><strong>Step 7</strong>
<p>We stop here, recognizing that we divided 100 by 11 two steps ago. This means we have a two-digit cycle (<span style="text-decoration:overline">10</span>) from here on out. The quotient is 11.11<span style="text-decoration:overline">10</span>.</p>
</li>
</ul>
<h3>Checking the Answer</h3>
<p>When the answer has a repeating fractional part, checking it is not as straightforward as it is for the other arithmetic operations. What we can do is approximate the quotient to a finite number of places and then check that it comes close to the expected answer.</p>
<p>You can check the answer in a few ways. One way is by doing binary multiplication by hand: you verify that the approximated quotient (11.11101011, for example) multiplied by the divisor (11) equals the dividend (1011.11). (I&#8217;ll leave that as an exercise, but the answer is 1011.11000001, which is very close to 1011.11).</p>
<p>Another way to check is to <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">convert the operands to decimal</a>, do decimal division, and then <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">convert the approximate decimal answer to binary</a>. 1011.11 = 11.75, and 11 = 3. 11.75/3 = 3.91<span style="text-decoration:overline">6</span>. Estimating that as 3.91666666666666667, for example, my binary converter says it equals 11.111010101010101010101010101010101010 when truncated to 36 places. That looks like it wants to be 11.11<span style="text-decoration:overline">10</span>, the answer we got using binary division.</p>
<p>You can also check the answer using my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>. It says 1011.11/11 is 11.111010101010 (to 12 places, for example). Again, that looks like 11.11<span style="text-decoration:overline">10</span>.</p>
<p>If you want to verify the repeating part directly, you can use this <a title="An arbitrary-precision, fractional value calculator that shows repeating fractional parts" href="http://www.knowledgedoor.com/2/calculators/convert_a_number_with_a_mixed_fractional_part.html">conversion tool</a>; here&#8217;s what to enter:</p>
<ul>
<li>Initial Base: 2</li>
<li>Integral part: 11</li>
<li>Non-Repeating Fractional Part: 11</li>
<li>Repeating Fractional Part: 10</li>
<li>New Base: 10</li>
</ul>
<p>It gives the decimal answer we expect: 3.91<span style="text-decoration:overline">6</span>.</p>
<p>You can also use this tool to convert in the opposite direction, verifying that 3.91<span style="text-decoration:overline">6</span> converts to 11.11<span style="text-decoration:overline">10</span>.</p>
<h2>Discussion</h2>
<p>Like the other arithmetic algorithms, I described the division algorithm in a base-independent way. I wanted to stress the mechanical procedure, not why it works (in either decimal or binary). </p>
<p>When you do binary long division, you might find yourself doing some of the substeps in your head in decimal (e.g., 101 &#8211; 11 is 5 &#8211; 3 = 2, which is 10 in binary).</p>
<p>Although binary division is easier than decimal division (because there&#8217;s no guessing and effectively no multiplication), you will find that always having the same number (the divisor) as the subtrahend will produce a pattern that will start mesmerizing you; it&#8217;s easy to get lost in that sea of 1s and 0s. (Be thankful my example only had a two-digit repeating cycle!)</p>
<p>If you play around with binary division you&#8217;ll see that it produces more repeating fractional numbers than decimal division does. For example, 2/5 = 0.4, but 10/101 = 0.<span style="text-decoration:overline">0110</span>.</p>
<h2>Further Reading</h2>
<p>There are many explanations of binary division on the Web; one that I like in particular, and that comes closest to what I&#8217;ve explained, is Dr. Math&#8217;s &ldquo;<a title="Read Dr. Math Answer &ldquo;&rdquo;" href="http://mathforum.org/library/drmath/view/55944.html">Long Division in Binary</a>.&rdquo;</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-division/">Binary Division</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/binary-division/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Binary Multiplication</title>
		<link>http://www.exploringbinary.com/binary-multiplication/</link>
		<comments>http://www.exploringbinary.com/binary-multiplication/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 17:28:32 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Binary arithmetic]]></category>
		<category><![CDATA[Binary integers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Convert to decimal]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=366</guid>
		<description><![CDATA[This is the third of a four part series on &#8220;pencil and paper&#8221; binary arithmetic, which I&#8217;m writing as a supplement to my binary calculator. The first article discusses binary addition; the second article discusses binary subtraction; this article discusses binary multiplication. The pencil-and-paper method of binary multiplication is just like the pencil-and-paper method of [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-multiplication/">Binary Multiplication</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>This is the third of a four part series on &ldquo;pencil and paper&rdquo; binary arithmetic, which I&#8217;m writing as a supplement to my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>. The first article discusses <a title="Read Rick Regan's Article &ldquo;Binary Addition&rdquo;" href="http://www.exploringbinary.com/binary-addition/">binary addition</a>; the second article discusses <a title="Read Rick Regan's Article &ldquo;Binary Subtraction&rdquo;" href="http://www.exploringbinary.com/binary-subtraction/">binary subtraction</a>; this article discusses binary multiplication.</em></p>
<div class="wp-caption aligncenter" style="width: 184px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryMultiplicationExample.png" alt="An Example of Binary Multiplication" width="174" height="254"/><p class="wp-caption-text">Example of Binary Multiplication</p></div>
<p>The pencil-and-paper method of binary multiplication is just like the pencil-and-paper method of decimal multiplication; the same algorithm applies, except binary numerals are manipulated instead. The way it works out though, binary multiplication is much simpler. The multiplier contains only 0s and 1s, so each multiplication step produces either zeros or a copy of the multiplicand. So binary multiplication is not multiplication at all &#8212; it&#8217;s just repeated binary addition!</p>
<p><span id="more-366"></span></p>
<h2>Decimal Multiplication</h2>
<p>To multiply two multiple-digit decimal numbers, you first need to know how to multiply two single-digit decimal numbers. This requires the memorization of 100 facts, or 55 facts if you exclude the commutative or “turnaround” facts. These facts are usually represented in a &ldquo;multiplication table,&rdquo; also known as a &ldquo;times table.&rdquo; Example facts are 2 x 9 = 18, 9 x 7 = 63, and 1 x 6 = 6. </p>
<p>A multiplication problem is written with one number on top, called the multiplicand, and one number on the bottom, called the multiplier. The algorithm has two phases: the multiplication phase, where you produce what are called partial products, and the addition phase, where you add the partial products to get the result. </p>
<p>In the multiplication phase, the digits of the multiplier are stepped through one at a time, from right to left. Each digit of the multiplicand is then multiplied, in turn, by the current multiplier digit; taken together, these single-digit multiplications form a partial product. The answer to each single-digit multiplication comes from the multiplication table. Some of these answers are double-digit numbers, in which case the least significant digit is recorded and the most significant digit is carried over to be added to the result of the next single-digit multiplication.</p>
<p>For example, let&#8217;s multiply 3.87 and 5.3:</p>
<div class="wp-caption aligncenter" style="width: 113px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalMultiplicationExample.png" alt="An Example of Decimal Multiplication" width="103" height="197"/><p class="wp-caption-text">Example of Decimal Multiplication</p></div>
<p>There are two digits in the multiplier, so there are two partial products: 1161 and 19350. Each partial product has its own set of carries, which are crossed out before computation of the next partial product. Here is the multiplication phase, broken down into steps:</p>
<div class="wp-caption aligncenter" style="width: 258px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalMultiplicationExampleSteps.png" alt="Steps of Decimal Multiplication (Multiplication Phase Only)" width="248" height="575"/><p class="wp-caption-text">Steps of Decimal Multiplication (Multiplication Phase Only)</p></div>
<p>When the multiplication phase is done, the partial products are added, and the decimal point is placed appropriately. (If there were any minus signs, they would be taken into account at this point as well.) This gives the answer 20.511.</p>
<h2>Binary Multiplication</h2>
<p>Binary multiplication uses the same algorithm, but uses just three order-independent facts: 0 x 0 = 0, 1 x 0 = 0, and 1 x 1 = 1 (these work the same as in decimal). If you perform the multiplication phase with these facts, you&#8217;ll notice two things: there are never any carries, and the partial products will either be zeros or a shifted copy of the multiplicand.</p>
<p>Observing this, you&#8217;ll realize there&#8217;s no need for digit-by-digit multiplication, which means there&#8217;s no need to consult a times table &#8212; which means there&#8217;s no multiplication, period! Instead, you just write down 0 when the current digit of the multiplier is 0, and you write down the multiplicand when the current digit of the multiplier is 1.</p>
<p>In the introduction, I showed this example: 1011.01 x 110.1. I wrote it as if you followed the decimal algorithm to the letter. Here&#8217;s how it looks if you follow the simpler &#8220;write zero or multiplicand&#8221; algorithm (it&#8217;s the same result, but with blanks representing 0s; this matches better conceptually with what we are now doing):</p>
<div class="wp-caption aligncenter" style="width: 209px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryMultiplicationExampleSimplified.png" alt="An Example of Binary Multiplication (Simplified View)" width="199" height="260"/><p class="wp-caption-text">Example of Binary Multiplication (Simplified View)</p></div>
<p>Here&#8217;s what the &#8220;multiplication&#8221; phase looks like, step-by-step:</p>
<div class="wp-caption aligncenter" style="width: 383px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryMultiplicationExampleSteps.png" alt="Steps of Binary Multiplication (Multiplication Phase Only)" width="373" height="368"/><p class="wp-caption-text">Steps of Binary Multiplication (Multiplication Phase Only)</p></div>
<p>Each step is the placement of an entire partial product, unlike in decimal, where each step is a single-digit multiplication (and possible addition of a carry).</p>
<p>In the addition phase, the partial products are added using <a title="Read Rick Regan's Article &ldquo;Binary Addition&rdquo;" href="http://www.exploringbinary.com/binary-addition/">binary addition</a>, and then the radix point is placed appropriately. This gives the answer 1001001.001.</p>
<h3>Checking the Answer</h3>
<p>You can check the answer by <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">converting the operands to decimal</a>, doing decimal multiplication, and then <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">converting the decimal answer to binary</a>. 1011.01 = 11.25, and 110.1 = 6.5. Their product is 73.125, which is 1001001.001, the answer we got using binary multiplication.</p>
<p>You can also check the answer using my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>.</p>
<h2>Discussion</h2>
<p>Computers don&#8217;t multiply in exactly this way, but they do exploit the simplified view of binary multiplication that I&#8217;ve described.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-multiplication/">Binary Multiplication</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/binary-multiplication/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>One Hundred Acorns in Binary</title>
		<link>http://www.exploringbinary.com/one-hundred-acorns-in-binary/</link>
		<comments>http://www.exploringbinary.com/one-hundred-acorns-in-binary/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 13:47:52 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Convert to decimal]]></category>
		<category><![CDATA[Education]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=365</guid>
		<description><![CDATA[Today is the 100th day of school at my son&#8217;s elementary school. I&#8217;ve had my binary influence on prior 100th day projects, and this year was to be no different. But alas, his class is not doing one this year. I didn&#8217;t want to waste the acorn tops we saved though, so I made my [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/one-hundred-acorns-in-binary/">One Hundred Acorns in Binary</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today is the 100th day of school at my son&#8217;s elementary school. I&#8217;ve had my <a title="Read my article “One Hundred Cheerios in Binary”" href="http://www.exploringbinary.com/one-hundred-cheerios-in-binary/">binary influence on prior 100th day projects</a>, and this year was to be no different. But alas, his class is not doing one this year. I didn&#8217;t want to waste the acorn tops we saved though, so I made my own 100th day project (well not quite &#8212; I didn&#8217;t glue them):</p>
<div class="wp-caption aligncenter" style="width: 331px"><img class="centered" title="A Binary Multiplication Problem Expressed With One Hundred Acorn Tops" src="http://www.exploringbinary.com/wp-content/uploads/100th Day Acorns.jpg" alt="A Binary Multiplication Problem Expressed With One Hundred Acorn Tops" width="321" height="242" /><p class="wp-caption-text">A Binary Multiplication Problem Expressed With One Hundred Acorn Tops.</p></div>
<p><span id="more-365"></span></p>
<p>(Notice it&#8217;s a little asymmetric. The zero in the upper right has only 15 acorn tops, not 16 like the rest; I stole one for the times sign.)</p>
<p>There are two ways to solve this problem. The first way is to convert the operands to decimal and then multiply using decimal arithmetic:</p>
<ol>
<li>Binary 1010 = 1 x 2<sup>3</sup> +  0 x 2<sup>2</sup> + 1 x 2<sup>1</sup> + 0 x 2<sup>0</sup> = 2<sup>3</sup> + 2<sup>1</sup> = 8 + 2 = 10 decimal.</li>
<li>10 x 10 = 100.</li>
</ol>
<p>The second way is to <a title="Read Rick Regan's Article &ldquo;Binary Multiplication&rdquo;" href="http://www.exploringbinary.com/binary-multiplication/">multiply using binary arithmetic</a> and then convert the answer to decimal:</p>
<ol>
<li>
<pre class="noborder">
      1010
    x 1010
    ------
      0000
     10100
    000000
   1010000
   -------
   1100100
</pre>
</li>
<li>Binary 1100100 = 1 x 2<sup>6</sup> + 1 x 2<sup>5</sup> + 0 x 2<sup>4</sup> + 0 x 2<sup>3</sup> + 1 x 2<sup>2</sup> + 0 x 2<sup>1</sup> + 0 x 2<sup>0</sup> = 2<sup>6</sup> + 2<sup>5</sup> + 2<sup>2</sup> = 64 + 32 + 4 = 100 decimal.</li>
</ol>
<p>The first way is easier, but it&#8217;s not as much fun <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/one-hundred-acorns-in-binary/">One Hundred Acorns in Binary</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/one-hundred-acorns-in-binary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Subtraction</title>
		<link>http://www.exploringbinary.com/binary-subtraction/</link>
		<comments>http://www.exploringbinary.com/binary-subtraction/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 17:14:28 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Binary arithmetic]]></category>
		<category><![CDATA[Binary integers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Convert to decimal]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=363</guid>
		<description><![CDATA[This is the second of a four part series on &#8220;pencil and paper&#8221; binary arithmetic, which I&#8217;m writing as a supplement to my binary calculator. The first article discusses binary addition; this article discusses binary subtraction. The pencil-and-paper method of binary subtraction is just like the pencil-and-paper method of decimal subtraction you learned in elementary [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-subtraction/">Binary Subtraction</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>This is the second of a four part series on &ldquo;pencil and paper&rdquo; binary arithmetic, which I&#8217;m writing as a supplement to my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>. The first article discusses <a title="Read Rick Regan's Article &ldquo;Binary Addition&rdquo;" href="http://www.exploringbinary.com/binary-addition/">binary addition</a>; this article discusses binary subtraction.</em></p>
<div class="wp-caption aligncenter" style="width: 171px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinarySubtractionExample.png" alt="An Example of Binary Subtraction" width="161" height="146"/><p class="wp-caption-text">Example of Binary Subtraction</p></div>
<p>The pencil-and-paper method of binary subtraction is just like the pencil-and-paper method of decimal subtraction you learned in elementary school. Instead of manipulating decimal numerals, however, you manipulate binary numerals, according to a basic set of rules or &ldquo;facts.&rdquo;</p>
<p><span id="more-363"></span></p>
<h2>Decimal Subtraction</h2>
<p>For decimal subtraction, the basic facts are things like 5 &#8211; 1 = 4, 9 &#8211; 8 = 1, and 18 &#8211; 9 = 9. In each case, the answer is a single-digit, nonnegative integer. Most of the facts are &ldquo;single-digit minus single-digit&rdquo; problems, but some are &ldquo;double-digit minus single-digit&rdquo; problems (the double digits are the numbers 10 through 18). The latter represent cases of <em>borrowing</em>, which is the process by which negative answers are prevented.</p>
<p>Here&#8217;s an example of decimal subtraction:</p>
<div class="wp-caption aligncenter" style="width: 129px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalSubtractionExample.png" alt="An Example of Decimal Subtraction" width="119" height="131"/><p class="wp-caption-text">Example of Decimal Subtraction</p></div>
<p>After the points are aligned, subtraction proceeds from right to left. Red marks indicate borrowing. If a non-zero digit is borrowed from, it is crossed out, one is subtracted from it, and the decremented digit is written above it; a 1 is then placed next to the digit in the borrowing position, making it a two-digit number. If a zero digit is borrowed from, the borrow &ldquo;cascades&rdquo; until a non-zero digit is found.</p>
<p>Here&#8217;s the example again, step-by-step:</p>
<div class="wp-caption aligncenter" style="width: 374px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalSubtractionExampleSteps.png" alt="Steps of Decimal Subtraction" width="364" height="312"/><p class="wp-caption-text">Steps of Decimal Subtraction</p></div>
<ul>
<li><strong>Step 1</strong>: 5 &#8211; 0 = 5.</li>
<li><strong>Step 2</strong>: Borrow to make 12 &#8211; 5 = 7.</li>
<li><strong>Step 3</strong>: Borrow to make 15 &#8211; 7 = 8.</li>
<li><strong>Step 4</strong>: Borrow to make 10 &#8211; 1 = 9.</li>
</ul>
<p>Some people refer to this as the &ldquo;<a title="Wikipedia article on subtraction algorithms" href="http://en.wikipedia.org/wiki/Subtraction#Algorithms_for_subtraction">American method</a>&rdquo; (although this is just one variation of it &#8212; see <a title="Khan Academy Video on Decimal Subtraction" href="http://www.youtube.com/watch?v=0mOH-qNGM7M">Salman Khan&#8217;s video</a>, for example). Whatever your method is though, you can apply it to binary numbers.</p>
<h2>Binary Subtraction</h2>
<p>For binary subtraction, there are <em>four</em> facts instead of one hundred:</p>
<ul>
<li>0 &#8211; 0 = 0</li>
<li>1 &#8211; 0 = 1</li>
<li>1 &#8211; 1 = 0</li>
<li>10 &#8211; 1 = 1</li>
</ul>
<p>The first three are the same as in decimal. The fourth fact is the only new one; it is the borrow case. It applies when the &ldquo;top&rdquo; digit in a column is 0 and the &ldquo;bottom&rdquo; digit is 1. (Remember: in binary, 10 is pronounced &ldquo;one-zero&rdquo; or &ldquo;two.&rdquo;)</p>
<p>Now let&#8217;s subtract 1011.11 from 10101.101, following the same algorithm I used for decimal numbers:</p>
<div class="wp-caption aligncenter" style="width: 338px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinarySubtractionExampleSteps.png" alt="Steps of Binary Subtraction" width="328" height="514"/><p class="wp-caption-text">Steps of Binary Subtraction</p></div>
<ul>
<li><strong>Step 1</strong>: 1 &#8211; 0 = 1.</li>
<li><strong>Step 2</strong>: Borrow to make 10 &#8211; 1 = 1.</li>
<li><strong>Step 3</strong>: Borrow to make 10 &#8211; 1 = 1.</li>
<li><strong>Step 4</strong>: Cascaded borrow to make 10 &#8211; 1 = 1.</li>
<li><strong>Step 5</strong>: 1 &#8211; 1 = 0.</li>
<li><strong>Step 6</strong>: 0 &#8211; 0 = 0.</li>
<li><strong>Step 7</strong>: Borrow to make 10 &#8211; 1 = 1.</li>
</ul>
<p>Since there are lots of 0s in binary numbers, there can be lots of borrows &#8212; and lots of messy looking cross-outs.</p>
<h3>Checking the Answer</h3>
<p>You can check the answer in a few ways. One way is to <a title="Read Rick Regan's Article &ldquo;Binary Addition&rdquo;" href="http://www.exploringbinary.com/binary-addition/">add</a> the result (1001.111) to the subtrahend (1011.11), and check that that answer matches the minuend (10101.101):</p>
<div class="wp-caption aligncenter" style="width: 165px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinarySubtractionExampleCheck.png" alt="Check Binary Subtraction Using Binary Addition" width="155" height="141"/><p class="wp-caption-text">Check Binary Subtraction Using Binary Addition</p></div>
<p>Another way is to <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">convert the operands to decimal</a>, do decimal subtraction, and then <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">convert the decimal answer to binary</a>. 10101.101 =  21.625 and 1011.11 = 11.75, and 21.625 &#8211; 11.75 = 9.875. 9.875 = 1001.111, the answer we got using binary subtraction.</p>
<p>You can also check the answer using my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>.</p>
<h3>Subtracting a Bigger Number From a Smaller Number</h3>
<p>To subtract a bigger number from a smaller number, just swap the numbers, do the subtraction, and negate the result.</p>
<h2>Discussion</h2>
<p>Notice that I didn&#8217;t discuss the number base when describing the algorithm; it is base-independent. Nonetheless, I could have talked about powers of ten and powers of two, and how the process can be visualized by regrouping. My goal was to explain just the mechanized algorithm (presumably you do decimal subtraction mechanically, no longer thinking about why it works).</p>
<h3>Subtracting Using Complements</h3>
<p>Computers don&#8217;t subtract this way; they subtract by <a title="Wikipedia article on &ldquo;Method of Complements&rdquo;" href="http://en.wikipedia.org/wiki/Method_of_complements">adding complements</a>. It&#8217;s more efficient. You can do subtraction by complements with pencil-and-paper, but you won&#8217;t find it more efficient. (In decimal, you would use nine&#8217;s complement or ten’s complement; in binary, you would use ones’ complement or two’s complement.)</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-subtraction/">Binary Subtraction</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/binary-subtraction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring Binary On My Apple II</title>
		<link>http://www.exploringbinary.com/exploring-binary-on-my-apple-ii/</link>
		<comments>http://www.exploringbinary.com/exploring-binary-on-my-apple-ii/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 18:08:26 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[About]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=362</guid>
		<description><![CDATA[I&#8217;m reading Steve Wozniak&#8217;s 2006 book &#8220;iWoz&#8221; and this line got me wondering about my own Apple II: &#8220;In every speech I give, I talk to people who are still running Apple IIs, and they say those machines are still running after this many years.&#8221; So I got it out of the attic and powered [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/exploring-binary-on-my-apple-ii/">Exploring Binary On My Apple II</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m reading Steve Wozniak&#8217;s 2006 book &ldquo;iWoz&rdquo; and this line got me wondering about <a title="Read Rick Regan's Article &ldquo;The Origins of This Site&rdquo;" href="http://www.exploringbinary.com/the-origins-of-this-site/">my own Apple II</a>:</p>
<blockquote><p>&ldquo;In every speech I give, I talk to people who are still running Apple IIs, and they say those machines are still running after this many years.&rdquo;</p></blockquote>
<p>So I got it out of the attic and powered it up. The dozen or so dead keys notwithstanding, it still works &#8212; after 30 years!</p>
<div class="wp-caption aligncenter" style="width: 530px"><img src="http://www.exploringbinary.com/wp-content/uploads/AppleII.Alive.jpg" alt="Some BASIC Commands I Tried On My Keyboard-Challenged But Otherwise Still Working Apple II" width="520" height="384"/><p class="wp-caption-text">Some BASIC Commands I Tried On My Keyboard-Challenged But Otherwise Still Working Apple II</p></div>
<p><span id="more-362"></span></p>
<h2>Getting The Monitor To Work</h2>
<p>I was expecting not to have the cable or connector I needed to hook the computer up to a TV (as I recall, there was a splitter that attached from the video cable to the TV&#8217;s VHF inputs). Also, I was wondering if I had a TV with the right inputs. It turns out I had what I needed.</p>
<p>The video cable was already attached to the computer, inside the case to an RF modulator. Hooking that to the TV&#8217;s video input did not work. After looking online for documentation (I no longer have the manuals), I found a copy of the &ldquo;Apple II Basic Programming Manual&rdquo;; on page 5 I saw this:</p>
<blockquote><p>&ldquo;If you have a color (or black and white) monitor, just connect the appropriate cable from the jack marked &#8220;VIDEO OUT&#8221; (on the rear of the Apple II) to the input of the monitor.</p>
<p>If you have an ordinary TV, you will have to install an RF modulator.&rdquo;</p></blockquote>
<p>I never used a monitor back in the day &#8212; just a TV. So all I did was move the cable to the rear input &#8212; and I got video!</p>
<h2>Getting Into BASIC</h2>
<p>All that appeared on the screen was the &#8220;Apple ][&#8221; logo. My disk drive was attached, and it was spinning (there was no floppy disk in it &#8212; I don&#8217;t have those either). It occurred to me to remove the floppy drive controller card so that the computer would boot into ROM BASIC. I powered up again and, bingo!</p>
<h2>A Partially Working Keyboard</h2>
<p>Unfortunately, many keys did not work (among others: a,  b, i, o, y, 4, -, =, and ,). I opened the bottom of the case to see if there was anything obviously wrong, but I didn&#8217;t see anything (surprisingly though, there was no dust inside the case).</p>
<h2>An Example BASIC Program</h2>
<p>I wanted to make some binary related program using the available keys. Luckily, I had double quotes and the question mark, so I could print stuff. I also had exponentiation (&lsquo;^&rsquo;), addition, and division, which were enough to make the small program pictured above (the picture shows our old TV, before I realized that with correct attachment, it would work on our newer TV):</p>
<ul>
<li>Line 10: Prints &ldquo;Rick Regan exploringbinary.com 2/5/12&rdquo;, or the closest thing to it with missing keys and all caps.</li>
<li>Line 20: Prints 2<sup>32</sup>, which displays in scientific notation, correctly rounded to eight significant digits, as 4.2949673E+09.</li>
<li>Line 30: Prints 2<sup>-32</sup>, which displays in scientific notation, correctly rounded to nine significant digits, as 2.32830644E-10.</li>
<li>Line 50 (remember my &lsquo;4&rsquo; key doesn&#8217;t work): Prints 2<sup>0</sup> +  2<sup>2</sup> +  2<sup>3</sup> +  2<sup>5</sup> +  2<sup>7</sup> +  2<sup>12</sup> +  2<sup>15</sup>, which corresponds to the binary representation of 37037.</li>
<li>Line 60: Prints 2.2250738585072011/10<sup>308</sup> (which is how I had to enter <a title="Read Rick Regan's Article &ldquo;PHP Hangs On Numeric Value 2.2250738585072011e-308&rdquo;" href="http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/">2.2250738585072011e-308</a> without a minus sign). It overflows, but at least it doesn&#8217;t hang! <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<h2>Is the Apple II Really a Computer?</h2>
<p>My kids watched me as I did this. &ldquo;Where&#8217;s the mouse?&rdquo; &ldquo;How do you click on things?&rdquo; &ldquo;Does it play Angry Birds?&rdquo; The problem was I kept calling it a computer, but it wasn&#8217;t like any computer they&#8217;d ever seen.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/exploring-binary-on-my-apple-ii/">Exploring Binary On My Apple II</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/exploring-binary-on-my-apple-ii/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Binary Addition</title>
		<link>http://www.exploringbinary.com/binary-addition/</link>
		<comments>http://www.exploringbinary.com/binary-addition/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:15:19 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Binary arithmetic]]></category>
		<category><![CDATA[Binary integers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Convert to decimal]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=358</guid>
		<description><![CDATA[This is the first of a four part series on &#8220;pencil and paper&#8221; binary arithmetic, which I&#8217;m writing as a supplement to my binary calculator. This article introduces binary arithmetic, and then discusses binary addition. Binary Arithmetic Binary arithmetic is of interest because that&#8217;s how computers do math. When implemented in computers, many things must [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-addition/">Binary Addition</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>This is the first of a four part series on &ldquo;pencil and paper&rdquo; binary arithmetic, which I&#8217;m writing as a supplement to my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>. This article introduces binary arithmetic, and then discusses binary addition.</em></p>
<div class="wp-caption aligncenter" style="width: 203px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryAdditionExample.png" alt="An Example of Binary Addition" width="193" height="159"/><p class="wp-caption-text">Example of Binary Addition</p></div>
<p><span id="more-358"></span></p>
<h2>Binary Arithmetic</h2>
<p>Binary arithmetic is of interest because that&#8217;s how computers do math. When implemented in computers, many things must be taken into account: format (fixed-point, floating-point, etc.), word size (8-bit, 16-bit, 32-bit, etc.), sign representation (sign-magnitude, ones&#8217; complement, two&#8217;s complement, etc.), overflow (when numbers are too big), and underflow (when numbers are too small). Furthermore, there is the design of the circuits to perform the algorithms (full adders, half adders, ripple carry adders, carry-lookahead adders, restoring dividers, non-restoring dividers, etc.). These are not inherent properties of binary arithmetic &#8212; they&#8217;re just implementation issues.</p>
<p>The truth is, in its purest form, binary arithmetic is very simple. The same pencil-and-paper algorithms you learned in grade school work for binary numbers. All you do differently is apply the &ldquo;facts&rdquo; for binary numerals, the (small) set of rules for manipulating 0s and 1s. And binary numbers on paper are written as you&#8217;d expect: without leading zeros, and with a minus sign (&lsquo;-&rsquo;) if negative.</p>
<p>In my series of articles, I will explain the pure forms of the four basic operations of binary arithmetic: addition, <a title="Read Rick Regan's Article &ldquo;Binary Subtraction&rdquo;" href="http://www.exploringbinary.com/binary-subtraction/">subtraction</a>, <a title="Read Rick Regan's Article &ldquo;Binary Multiplication&rdquo;" href="http://www.exploringbinary.com/binary-multiplication/">multiplication</a>, and <a title="Read Rick Regan's Article &ldquo;Binary Division&rdquo;" href="http://www.exploringbinary.com/binary-division/">division</a>. In this first article, I will discuss binary addition.</p>
<h2>Decimal Addition</h2>
<p>To add two multiple-digit decimal numbers, you first need to know how to add two single-digit decimal numbers. This requires the memorization of 100 facts, or 55 facts if you exclude the commutative or &ldquo;turnaround&rdquo; facts. Also, because of carries, you need to know ten additional facts: 10 + 0 = 10, 10 + 1 = 11, &#8230; , 10 + 9 = 19. The latter apply when there&#8217;s a carry (always 1) and the &ldquo;top&rdquo; digit is 9.</p>
<table class="center" border="1" summary="109 Decimal Addition Facts">
<caption><strong>Decimal Addition Facts</strong></caption>
<tbody>
<tr>
<th class="center">+</th>
<th class="center internal_border_bottom">0</th>
<th class="center internal_border_bottom">1</th>
<th class="center internal_border_bottom">2</th>
<th class="center internal_border_bottom">3</th>
<th class="center internal_border_bottom">4</th>
<th class="center internal_border_bottom">5</th>
<th class="center internal_border_bottom">6</th>
<th class="center internal_border_bottom">7</th>
<th class="center internal_border_bottom">8</th>
<th class="center internal_border_bottom">9</th>
<th class="center internal_border_bottom">10</th>
</tr>
<tr>
<td class="center internal_border_right"><strong>0</strong></td>
<td class="center">0</td>
<td class="center grayed">1</td>
<td class="center grayed">2</td>
<td class="center grayed">3</td>
<td class="center grayed">4</td>
<td class="center grayed">5</td>
<td class="center grayed">6</td>
<td class="center grayed">7</td>
<td class="center grayed">8</td>
<td class="center grayed">9</td>
<td class="center grayed">10</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>1</strong></td>
<td class="center">1</td>
<td class="center">2</td>
<td class="center grayed">3</td>
<td class="center grayed">4</td>
<td class="center grayed">5</td>
<td class="center grayed">6</td>
<td class="center grayed">7</td>
<td class="center grayed">8</td>
<td class="center grayed">9</td>
<td class="center grayed">10</td>
<td class="center grayed">11</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>2</strong></td>
<td class="center">2</td>
<td class="center">3</td>
<td class="center">4</td>
<td class="center grayed">5</td>
<td class="center grayed">6</td>
<td class="center grayed">7</td>
<td class="center grayed">8</td>
<td class="center grayed">9</td>
<td class="center grayed">10</td>
<td class="center grayed">11</td>
<td class="center grayed">12</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>3</strong></td>
<td class="center">3</td>
<td class="center">4</td>
<td class="center">5</td>
<td class="center">6</td>
<td class="center grayed">7</td>
<td class="center grayed">8</td>
<td class="center grayed">9</td>
<td class="center grayed">10</td>
<td class="center grayed">11</td>
<td class="center grayed">12</td>
<td class="center grayed">13</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>4</strong></td>
<td class="center">4</td>
<td class="center">5</td>
<td class="center">6</td>
<td class="center">7</td>
<td class="center">8</td>
<td class="center grayed">9</td>
<td class="center grayed">10</td>
<td class="center grayed">11</td>
<td class="center grayed">12</td>
<td class="center grayed">13</td>
<td class="center grayed">14</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>5</strong></td>
<td class="center">5</td>
<td class="center">6</td>
<td class="center">7</td>
<td class="center">8</td>
<td class="center">9</td>
<td class="center">10</td>
<td class="center grayed">11</td>
<td class="center grayed">12</td>
<td class="center grayed">13</td>
<td class="center grayed">14</td>
<td class="center grayed">15</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>6</strong></td>
<td class="center">6</td>
<td class="center">7</td>
<td class="center">8</td>
<td class="center">9</td>
<td class="center">10</td>
<td class="center">11</td>
<td class="center">12</td>
<td class="center grayed">13</td>
<td class="center grayed">14</td>
<td class="center grayed">15</td>
<td class="center grayed">16</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>7</strong></td>
<td class="center">7</td>
<td class="center">8</td>
<td class="center">9</td>
<td class="center">10</td>
<td class="center">11</td>
<td class="center">12</td>
<td class="center">13</td>
<td class="center">14</td>
<td class="center grayed">15</td>
<td class="center grayed">16</td>
<td class="center grayed">17</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>8</strong></td>
<td class="center">8</td>
<td class="center">9</td>
<td class="center">10</td>
<td class="center">11</td>
<td class="center">12</td>
<td class="center">13</td>
<td class="center">14</td>
<td class="center">15</td>
<td class="center">16</td>
<td class="center grayed">17</td>
<td class="center grayed">18</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>9</strong></td>
<td class="center">9</td>
<td class="center">10</td>
<td class="center">11</td>
<td class="center">12</td>
<td class="center">13</td>
<td class="center">14</td>
<td class="center">15</td>
<td class="center">16</td>
<td class="center">17</td>
<td class="center">18</td>
<td class="center grayed">19</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>10</strong></td>
<td class="center">10</td>
<td class="center">11</td>
<td class="center">12</td>
<td class="center">13</td>
<td class="center">14</td>
<td class="center">15</td>
<td class="center">16</td>
<td class="center">17</td>
<td class="center">18</td>
<td class="center">19</td>
<td class="center">-</td>
</tr>
</tbody>
</table>
<p>For example, let&#8217;s add 19.7 and 12.8:</p>
<div class="wp-caption aligncenter" style="width: 127px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalAdditionExample.png" alt="An Example of Decimal Addition" width="117" height="137"/><p class="wp-caption-text">Example of Decimal Addition</p></div>
<p>Let&#8217;s think about what a carry does. It turns our neat &#8220;add two single-digit numbers per column&#8221; problem into &#8220;add <em>three</em> single-digit numbers per column.&#8221; But addition is a binary (in a different sense) operation, meaning it operates on two numbers at a time. So what we do is add the carry to the &ldquo;top&rdquo; digit, and then add that result to the &ldquo;bottom&rdquo; digit. The first addition may result in a double digit answer (10), which means we&#8217;d have to use one of the ten extra facts to do the second addition.</p>
<p>In our example, the carry is added to 9, giving 10, and then 10 is added to 2, giving 12. The second addition used the extra &#8220;10 + 2 = 12&#8243; fact.</p>
<div class="wp-caption aligncenter" style="width: 139px"><img src="http://www.exploringbinary.com/wp-content/uploads/DecimalAdditionExampleExtra.png" alt="Decimal Addition Showing Intermediate Additions" width="129" height="151"/><p class="wp-caption-text">Decimal Addition Showing Intermediate Additions</p></div>
<h2>Binary Addition</h2>
<p>Binary addition works the same way as decimal addition, except it uses a different &#8212; and much smaller &#8212; set of facts. There are only four single-digit facts, or three if you exclude the commutative fact. To handle carries, you need to know two additional facts, 10 + 0 = 10 and 10 + 1 = 11. (<a title="Read Rick Regan's Article &ldquo;There Are 10 Types of People …&rdquo;" href="http://www.exploringbinary.com/there-are-10-types-of-people/">Resist the urge to pronounce 10 as ten</a> and 11 as eleven; either call them one-zero and one-one, or two and three.) The latter apply when there’s a carry (always 1) and the “top” digit is 1.</p>
<table class="center" border="1" summary="8 Binary Addition Facts">
<caption><strong>Binary Addition Facts</strong></caption>
<tbody>
<tr>
<th class="center">+</th>
<th class="center internal_border_bottom">0</th>
<th class="center internal_border_bottom">1</th>
<th class="center internal_border_bottom">10</th>
</tr>
<tr>
<td class="center internal_border_right"><strong>0</strong></td>
<td class="center">0</td>
<td class="center grayed">1</td>
<td class="center grayed">10</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>1</strong></td>
<td class="center">1</td>
<td class="center">10</td>
<td class="center grayed">11</td>
</tr>
<tr>
<td class="center internal_border_right"><strong>10</strong></td>
<td class="center">10</td>
<td class="center">11</td>
<td class="center">-</td>
</tr>
</tbody>
</table>
<p>Of the five order-independent facts, two are the same as in decimal (0 + 0 = 0 and 1 + 0 = 1), and one is trivial since it just adds zero (10 + 0 = 10). <strong>This leaves only two facts to memorize: 1 + 1 = 10 and 10 + 1 = 11!</strong></p>
<p>To add two binary numbers, proceed as in decimal addition: </p>
<ul>
<li>If one or both numbers has a fractional part, line up the <a title="Wikipedia article on radix point" href="http://en.wikipedia.org/wiki/Radix_point">radix points</a>.</li>
<li>Proceeding from right to left, add the digits in each &ldquo;column,&rdquo; according to the facts table.</li>
<li>If the result has two-digits, write down the least significant digit; carry the most significant digit to the next column.</li>
</ul>
<p>Let&#8217;s return to the example in the introduction, 1011.01 + 11.011, this time showing it step-by-step:</p>
<div class="wp-caption aligncenter" style="width: 354px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryAdditionExampleSteps.png" alt="Steps of Binary Addition" width="344" height="644"/><p class="wp-caption-text">Steps of Binary Addition</p></div>
<p>Eventually you won&#8217;t need to write down the intermediate additions, and you might even start doing &lsquo;1 + 1 + 1&rsquo; as one step.</p>
<h3>Checking the Answer</h3>
<p>You can verify the answer by <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">converting the operands to decimal</a>, doing decimal addition, and then <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">converting the decimal answer to binary</a> (of course you can do it that way period, avoiding binary arithmetic entirely!). 1011.01 = 11.25, and 11.011 = 3.375. They sum to 14.625, which is 1110.101, the answer we got using binary addition.</p>
<p>You can also check the answer using my <a title="Rick Regan's &ldquo;Binary Calculator&rdquo;" href="http://www.exploringbinary.com/binary-calculator/">binary calculator</a>.</p>
<h2>Discussion</h2>
<p>We didn&#8217;t need to know the place value of columns, or that a carry represents a power of the base; the algorithm is base-independent. We <em>did</em> however need a base-dependent set of facts, which allowed us to manipulate binary numerals according to the basic rules of binary addition.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-addition/">Binary Addition</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/binary-addition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bigcomp: Deciding Truncated, Near Halfway Conversions</title>
		<link>http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/</link>
		<comments>http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 15:40:16 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Numbers in computers]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Convert to decimal]]></category>
		<category><![CDATA[Decimals]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Floating-point]]></category>
		<category><![CDATA[Fractions]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=353</guid>
		<description><![CDATA[In my article &#8220;Using Integers to Check a Floating-Point Approximation,&#8221; I briefly mentioned &#8220;bigcomp,&#8221; an optimization strtod() uses to reduce big integer overhead when checking long decimal inputs. bigcomp does a floating-point to decimal conversion &#8212; right in the middle of a decimal to floating-point conversion mind you &#8212; to generate the decimal expansion of [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/">Bigcomp: Deciding Truncated, Near Halfway Conversions</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In my article &ldquo;<a title="Read Rick Regan's article &ldquo;Using Integers to Check a Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">Using Integers to Check a Floating-Point Approximation</a>,&rdquo; I briefly mentioned &ldquo;bigcomp,&rdquo; an optimization strtod() uses to reduce big integer overhead when checking long decimal inputs. bigcomp does a floating-point to decimal conversion &#8212; right in the middle of a decimal to floating-point conversion mind you &#8212; to generate the decimal expansion of the number halfway between two target floating-point numbers. This decimal expansion is compared to the input decimal string, and the result of the comparison dictates which of the two target numbers is the correctly rounded result.</p>
<p>In this article, I&#8217;ll explain how bigcomp works, and when it applies. Also, I&#8217;ll talk briefly about its performance; my informal testing shows that, under the default setting, bigcomp actually <em>worsens</em> performance for some inputs.</p>
<p><span id="more-353"></span></p>
<h2>When Bigcomp Applies</h2>
<p>Bigcomp is enabled by default in strtod() (it can be disabled by &ldquo;&#35;defining&rdquo; the variable &ldquo;NO_STRTOD_BIGCOMP&rdquo;). It applies to decimal strings longer than STRTOD_DIGLIM significant digits, which by default is &ldquo;&#35;defined&rdquo; to 40. For decimal inputs <em>dStr</em> of 40 significant digits or less, the <a title="Read Rick Regan's article &ldquo;Using Integers to Check a Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">normal approximation check</a> is done, using all significant digits of <em>dStr</em> for its big integer representation <em>d</em>; for inputs longer than 40 digits, only the first 18 digits are used for <em>d</em>. This truncation of <em>d</em> leads to smaller big integers, but it can lead to cases where the approximation check cannot decide between adjacent floating-point values. When that happens, the bigcomp procedure kicks in, through a call to the bigcomp() function.</p>
<p>Specifically, bigcomp() is called when the approximation check cannot decide between the approximation <em>b</em> and its successor, <em>b + u</em>. (As with all my articles in this series, I will only be discussing how strtod() works for round-to-nearest/round-half-to-even rounding.) This happens when the truncated value of <em>d</em> falls between <em>b</em> and <em>b + h</em>; that is, between <em>b</em> and one-half ULP above <em>b</em>.</p>
<p>(In this article, I distinguish between the input <em>dStr</em> and its big integer representation <em>d</em>, which may or may not be a truncation of <em>dStr</em>.) </p>
<h2>Overview of the Algorithm</h2>
<p>bigcomp() compares the significant digits of <em>dStr</em> to a string representation of the significant digits of <em>b + h</em>. It goes left-to-right, digit by significant digit, stopping when the two strings differ. (In the bulk of cases, the difference occurs between the 16th and 18th digits, although in the worst case <a title="Read Rick Regan's article &ldquo;A Bug in the Bigcomp Function of David Gay&rsquo;s strtod()&rdquo;" href="http://www.exploringbinary.com/a-bug-in-the-bigcomp-function-of-david-gays-strtod/">the difference can occur at the 54th digit</a>; it depends on how close the decimal number is to <em>b + h</em>.) If <em>dStr</em> is <em>less</em> than <em>b + h</em>, bigcomp() returns <em>b</em>; if <em>dStr</em> is <em>greater</em> than <em>b + h</em>, bigcomp() returns <em>b + u</em>;  if <em>dStr</em> equals <em>b + h</em>, either <em>b</em> or <em>b + u</em> is chosen, according to round-half-to-even rounding.</p>
<p>bigcomp() generates the significant digits of <em>b + h</em> using essentially the same algorithm as the dtoa() function, the floating-point to decimal conversion routine co-located in <a title="David Gay's dtoa.c" href="http://www.netlib.org/fp/dtoa.c">David Gay&#8217;s dtoa.c</a>. It uses big integer arithmetic, including big integer <em>division</em>.</p>
<p>bigcomp() starts by creating a fraction that represents the significant digits of <em>b + h</em> (not <em>b + h</em> itself). In this form, it generates a digit by multiplying the numerator of the fraction by ten, and then using integer division to form a quotient and remainder. The quotient represents the next digit; the remainder represents the remaining digits, which are accessed one at a time by repeating this process. Digits are generated until there is a mismatch between <em>dStr</em> and <em>b + h</em>.</p>
<h2>The Algorithm, By Example</h2>
<p>I&#8217;ll break the algorithm into five steps, and illustrate it with two examples. I set STRTOD_DIGLIM to 19 so I could generate shorter examples (both are 20 digits).</p>
<h3>Example 1: Check 1.3694713649464322631e-11</h3>
<p>In this example, we want to check if <em>dStr</em> = 1.3694713649464322631e-11 is closer to <em>b</em> = 0&#120;1.e1d703bb5749cp-37 or <em>b + u</em> = 0&#120;1.e1d703bb5749dp-37. We do this by comparing <em>dStr</em> to <em>b + h</em> =  0&#120;1.e1d703bb5749c8p-37. (I describe <em>b + h</em> using a <a title="Read Rick Regan's article &ldquo;Hexadecimal Floating-Point Constants&rdquo;" href="http://www.exploringbinary.com/hexadecimal-floating-point-constants/">hexadecimal floating-point constant</a>, even though it won&#8217;t be represented as an IEEE floating-point number. This number has 54 bits, with bit 54 being a 1. It is <em>b</em> with a hexadecimal &lsquo;8&rsquo; tacked on at the end.)</p>
<h4>1. Represent <em>b + h</em> as an integer times a power of two</h4>
<p>We start by representing <em>b</em> as a 53-bit integer times a power of two, as we do in the <a title="Read Rick Regan's article &ldquo;Using Integers to Check a Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">basic approximation check</a>:</p>
<pre class="noborder">
<em>b</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup>
  = 8476617176609948 x 2<sup>-89</sup>
</pre>
<p>Since <em>bInt</em> is 53-bit integer, <em>u</em> = 2<sup><em>bExp</em></sup>, and <em>h</em> is 2<sup><em>bExp</em>-1</sup>. Therefore,</p>
<pre class="noborder">
<em>b + h</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup> + 2<sup><em>bExp</em>-1</sup>
      = (<em>bInt</em> x 2 + 1) x 2<sup><em>bExp</em>-1</sup>
      = (8476617176609948 x 2 + 1) x 2<sup>-90</sup>
      = 16953234353219897 x 2<sup>-90</sup>
</pre>
<p><em>In code</em>: We create <em>bInt</em> and <em>bExp</em> from <em>b</em>&rsquo;s <a title="Read Rick Regan's article &ldquo;Displaying the Raw Fields of a Floating-Point Number&rdquo;" href="http://www.exploringbinary.com/displaying-the-raw-fields-of-a-floating-point-number/">underlying floating-point representation</a>: we treat its significand as a 53-bit integer, and subtract 52 from its power of two exponent. We create a big integer from <em>bInt</em>, and then we make it the integer portion of <em>b + h</em> by shifting it left and &lsquo;ORing&rsquo; a 1 into its least significant bit. We maintain the exponent of the power of two as a native integer, setting it to <em>bExp</em>-1.</p>
<h4>2. Scale <em>b + h</em> so its first significant digit is just to the left of the decimal point</h4>
<p>If you were to compute <em>b + h</em> from the expression above, you&#8217;d see its value is</p>
<p>0.0000000000136947136494643226044416541235590733258456475063269408565<br />
24743139743804931640625</p>
<p>(We&#8217;re not going to do that; I just wanted to show you where we&#8217;re headed. By the way, I computed it using <a title="Read Rick Regan's article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;" href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/">PARI/GP</a>.)</p>
<p>What we want to do is scale <em>b + h</em> so that it looks like</p>
<p>1.3694713649464322604441654123559073325845647506326940856524743139743<br />
804931640625</p>
<p>In other words, we want to multiply it by a power of ten so that, when viewed as a decimal number, its significant digits look normalized; 10<sup>11</sup> does the trick, effectively shifting <em>b + h</em> <em>left</em> by 11 decimal places:</p>
<pre class="noborder">
Scaled <em>b + h</em> = 16953234353219897 x 2<sup>-90</sup> x 10<sup>11</sup>
</pre>
<p>This &ldquo;normalization&rdquo; sets up the digit generation algorithm.</p>
<p><em>In code</em>: A full-blown floating-point to decimal conversion would need to use logarithms to compute the power of ten; but in this case, we can obtain the power of ten exponent directly from <em>dStr</em>.</p>
<h4>3. Combine powers of two</h4>
<p>If we decompose the power of ten into a power of two and a power of five, we get</p>
<pre class="noborder">
Scaled <em>b + h</em> = 16953234353219897 x 2<sup>-90</sup> x 2<sup>11</sup> x 5<sup>11</sup>
</pre>
<p>We can now <a title="Read Rick Regan's Article &ldquo;Composing Powers of Two Using The Laws of Exponents&rdquo;" href="http://www.exploringbinary.com/composing-powers-of-two-using-the-laws-of-exponents/">combine powers of two</a> to reduce the size of the resulting big integers.</p>
<pre class="noborder">
Scaled <em>b + h</em> = 16953234353219897 x 2<sup>-79</sup> x 5<sup>11</sup>
</pre>
<p><em>In code</em>: The code is simple: just add the exponents of the power of two from <em>b + h</em> and the power of two from the power of ten scaling factor.</p>
<h4>4. Express scaled <em>b + h</em> as a fraction</h4>
<p>The negative power of two, 2<sup>-79</sup>, makes this a fraction:</p>
<pre class="noborder">
Scaled <em>b + h</em> = (16953234353219897 x 5<sup>11</sup>)/2<sup>79</sup>
             = 827794646153315283203125/604462909807314587353088
</pre>
<p><em>In code</em>: Compute the powers as big integers, using the absolute values of the exponents; then, multiply the terms out, as appropriate.</p>
<h4>5. Generate and check digits</h4>
<p>Scaled <em>b + h</em> has a numerator <em>num</em> and denominator <em>den</em>:</p>
<pre class="noborder">
<em>num</em> = 827794646153315283203125
<em>den</em> = 604462909807314587353088
</pre>
<p>The significant digits are generated from these integers using the &ldquo;repeated multiplication by ten&rdquo; algorithm; this is what&#8217;s done at each iteration (<em>dig</em> stands for &lsquo;digit&rsquo;, and <em>rem</em> stands for &lsquo;remainder&rsquo;):</p>
<ol>
<li><em>dig</em> = <em>num</em>/<em>den</em> (just the integer part of the quotient)</li>
<li><em>rem</em> = <em>num</em> &#8211; <em>dig</em> x <em>den</em></li>
<li><em>num</em> = <em>rem</em> x 10</li>
</ol>
<p>In other words, at each iteration, a digit is peeled off and the rest of the number is shifted left one decimal place. This step is repeated until a corresponding digit from <em>dStr</em> and <em>b + h</em> don&#8217;t match; here are the first three iterations:</p>
<pre class="noborder">
1. 827794646153315283203125/<em>den</em>  = 1 <em>rem</em> 223331736346000695850037
2. 2233317363460006958500370/<em>den</em> = 3 <em>rem</em> 419928634038063196441106
3. 4199286340380631964411060/<em>den</em> = 6 <em>rem</em> 572508881536744440292532
4. ...
</pre>
<p>This stops after step 19; that is, <em>dStr</em> and <em>b + h</em> differ at the 19th digit. Here are the digits of <em>dStr</em> and <em>b + h</em> aligned, to show where they differ:</p>
<pre class="noborder">
Digits of <em>dStr</em>  = 136947136494643226<span class="highlight_gray4">3</span>1
Digits of <em>b + h</em> = 136947136494643226<span class="highlight_gray4">0</span>444165412355907332584564750632
6940856524743139743804931640625
</pre>
<p>Since 3 is greater than 0, <em>dStr</em> is closer to <em>b + u</em> than it is to <em>b</em>, so <em>b + u</em> is the answer.</p>
<h3>Example 2: Check 9.3170532238714134438e+16</h3>
<p>In this example, we want to check if <em>dStr</em> = 9.3170532238714134438e+16 is closer to <em>b</em> = 0&#120;1.4b021afd9f651p+56 or <em>b + u</em> = 0&#120;1.4b021afd9f652p+56. We do this by comparing <em>dStr</em> to <em>b + h</em> =  0&#120;1.4b021afd9f6518p+56.</p>
<h4>1. Represent <em>b + h</em> as an integer times a power of two</h4>
<pre class="noborder">
<em>b</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup>
  = 5823158264919633 x 2<sup>4</sup>

<em>b + h</em> = (<em>bInt</em> x 2 + 1) x 2<sup><em>bExp</em>-1</sup>
      = (5823158264919633 x 2 + 1) x 2<sup>3</sup>
      = 11646316529839267 x 2<sup>3</sup>
</pre>
<h4>2. Scale <em>b + h</em> so its first significant digit is just to the left of the decimal point</h4>
<p>If you were to compute <em>b + h</em> from the expression above, you&#8217;d see its value is</p>
<p>93170532238714136</p>
<p>We want to shift it <em>right</em> 16 decimal places so it looks like</p>
<p>9.3170532238714136</p>
<pre class="noborder">
Scaled <em>b + h</em> = 11646316529839267 x 2<sup>3</sup> x 10<sup>-16</sup>
</pre>
<h4>3. Combine powers of two</h4>
<p>If we decompose the power of ten into a power of two and a power of five, we get</p>
<pre class="noborder">
Scaled <em>b + h</em> = 11646316529839267 x 2<sup>3</sup> x 2<sup>-16</sup> x 5<sup>-16</sup>
</pre>
<p>Combining powers of two we get</p>
<pre class="noborder">
Scaled <em>b + h</em> = 11646316529839267 x 2<sup>-13</sup> x 5<sup>-16</sup>
</pre>
<h4>4. Express scaled <em>b + h</em> as a fraction</h4>
<p>The negative powers of two and five make this a fraction:</p>
<pre class="noborder">
Scaled <em>b + h</em> = 11646316529839267/(2<sup>13</sup> x 5<sup>16</sup>)
             = 11646316529839267/1250000000000000
</pre>
<h4>5. Generate and check digits</h4>
<p>Scaled <em>b + h</em> has a numerator <em>num</em> and denominator <em>den</em>:</p>
<pre class="noborder">
<em>num</em> = 11646316529839267
<em>den</em> = 1250000000000000
</pre>
<p>Here are the first three iterations of the repeated multiplication by ten algorithm:</p>
<pre class="noborder">
1. 11646316529839267/<em>den</em> = 9 <em>rem</em> 396316529839267
2. 3963165298392670/<em>den</em>  = 3 <em>rem</em> 213165298392670
3. 2131652983926700/<em>den</em>  = 1 <em>rem</em> 881652983926700
4. ...
</pre>
<p>This stops after step 17; that is, <em>dStr</em> and <em>b + h</em> differ at the 17th digit. Here are the digits of <em>dStr</em> and <em>b + h</em> aligned, to show where they differ:</p>
<pre class="noborder">
Digits of <em>dStr</em>  = 9317053223871413<span class="highlight_gray4">4</span>438
Digits of <em>b + h</em> = 9317053223871413<span class="highlight_gray4">6</span>
</pre>
<p>Since 4 is less than 6, <em>dStr</em> is closer to <em>b</em> than it is to <em>b + u</em>, so <em>b</em> is the answer.</p>
<h3>Discussion</h3>
<p>strtod() has an extra step, which I&#8217;ve not described. It scales the numerator and denominator by a common power of two, which is necessary for its &lsquo;quorem()&rsquo; function to compute the quotient and remainder properly. The power of two exponent is determined by a function called &lsquo;dshift().&rsquo;</p>
<p>If <em>dStr</em> represents a fractional value, step 2 (scale by power of ten) and step 3 (combine powers of two) are technically unnecessary. Without them, step 5 (generate digits) would generate leading zeros, which could be ignored. (Of course, the pre-scaling is more elegant, as it reduces everything to one form.)</p>
<h2>The 40-Digit Cutoff Is Too Low</h2>
<p>bigcomp() does a lot of work with big integers, which made me wonder why it performed better than the basic approximation check. I ran some tests, and found that it <em>doesn&#8217;t</em> perform better &#8212; not at the default STRTOD_DIGLIM value of 40! My tests indicate that bigcomp() only performs better starting at somewhere between 80 and 83 digits. (I don&#8217;t know why 40 was chosen as the default.) bigcomp() outperforms the basic check for really long inputs; for example, the difference in performance is great at 500 digits, and substantial at 1000 digits.</p>
<p>Let&#8217;s look at a 1000-digit input to see the difference in the big integers involved:</p>
<pre class="noborder">
<em>dStr</em> = 5.39393324717760434968487301635560570642059608762708229108908
24537754965408622951477281124864324524316631524646435056101989215441
13243584680626358335990970896315873327586087827333915843584231417344
18113410301502801068079096892041478634628161830670245841932386407800
12745803879948980583567722824049594060350336856210415679895612943123
50098271611832693583464221850190462604938332826437767915224673375308
74570398083131101089815004834148500912320646940469604931721018872877
13566715387976682236017863055187831755129573243931855002598816011501
73130862094578091926124182802567489337171671008075773833668718543916
59331687129078431660248055444327878892028873933579030052796118406453
59419797286387142281725295340184928613290361074947899276429780250293
85401861377815243939607988608126354001619571808095966555370152710852
57007714188421395421027623995592231545518458559072989336658831420578
08271261804364513313549623924640140003927894134813865873451090057841
21193123838848522389829029095139051820951352412966707477e+16
</pre>
<p>Without bigcomp, using the <a title="Read Rick Regan's article &ldquo;Using Integers to Check a Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">basic approximation check</a>, these are the variables involved:</p>
<pre class="noborder">
<em>dS</em> = 539393324717760434968487301635560570642059608762708229108908245
37754965408622951477281124864324524316631524646435056101989215441132
43584680626358335990970896315873327586087827333915843584231417344181
13410301502801068079096892041478634628161830670245841932386407800127
45803879948980583567722824049594060350336856210415679895612943123500
98271611832693583464221850190462604938332826437767915224673375308745
70398083131101089815004834148500912320646940469604931721018872877135
66715387976682236017863055187831755129573243931855002598816011501731
30862094578091926124182802567489337171671008075773833668718543916593
31687129078431660248055444327878892028873933579030052796118406453594
19797286387142281725295340184928613290361074947899276429780250293854
01861377815243939607988608126354001619571808095966555370152710852570
07714188421395421027623995592231545518458559072989336658831420578082
71261804364513313549623924640140003927894134813865873451090057841211
93123838848522389829029095139051820951352412966707477

<em>bS</em> = 539393324717760400000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000

<em>hS</em> = 400000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000

&#124;<em>dS-bS</em>&#124; = 3496848730163556057064205960876270822910890824537754965408
62295147728112486432452431663152464643505610198921544113243584680626
35833599097089631587332758608782733391584358423141734418113410301502
80106807909689204147863462816183067024584193238640780012745803879948
98058356772282404959406035033685621041567989561294312350098271611832
69358346422185019046260493833282643776791522467337530874570398083131
10108981500483414850091232064694046960493172101887287713566715387976
68223601786305518783175512957324393185500259881601150173130862094578
09192612418280256748933717167100807577383366871854391659331687129078
43166024805544432787889202887393357903005279611840645359419797286387
14228172529534018492861329036107494789927642978025029385401861377815
24393960798860812635400161957180809596655537015271085257007714188421
39542102762399559223154551845855907298933665883142057808271261804364
51331354962392464014000392789413481386587345109005784121193123838848
522389829029095139051820951352412966707477
</pre>
<p>&#124;<em>dS-bS</em>&#124; is less than <em>hS</em>, so <em>b</em> is chosen.</p>
<p><em>With</em> bigcomp, there are two sets of big integers involved: one set for the basic approximation check (on the truncated value), and one set for bigcomp. Here are the big integers used for the basic approximation check:</p>
<pre class="noborder">
<em>dS</em> = 539393324717760434
<em>bS</em> = 539393324717760400
<em>hS</em> = 40
&#124;<em>dS-bS</em>&#124; = 34
</pre>
<p>Here are the big integers used for bigcomp (not shown is the extra factor of 2<sup>8</sup> in the numerator and denominator due to dshift()):</p>
<pre class="noborder">
<em>num</em> = 13484833117944011
<em>den</em> = 2500000000000000
</pre>
<p>bigcomp() finds that digit 17 of <em>dStr</em> (3) is less than digit 17 of <em>b + h</em> (4), so it chooses <em>b</em>.</p>
<h2>Acknowledgement</h2>
<p>Thanks to Mark Dickinson for his commentary on bigcomp, both in <a title="Python dtoa.c" href="http://svn.python.org/view/python/branches/py3k-dtoa/Python/dtoa.c?revision=83813&#038;view=markup">Python&#8217;s version of David Gay&#8217;s dtoa.c</a>, and in private communication.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/">Bigcomp: Deciding Truncated, Near Halfway Conversions</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring Binary: One Million Views</title>
		<link>http://www.exploringbinary.com/exploring-binary-one-million-views/</link>
		<comments>http://www.exploringbinary.com/exploring-binary-one-million-views/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 04:43:43 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[About]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=351</guid>
		<description><![CDATA[Today (November 7, 2011), exploringbinary.com received its one millionth view, according to WordPress.com Stats: This site went live on November 14, 2008; that works out to an average of over 920 views per day. My decimal/binary converter (an HTML form with an accompanying article) accounts for nearly half of those views (445,700+). Not counting my [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/exploring-binary-one-million-views/">Exploring Binary: One Million Views</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today (November 7, 2011), exploringbinary.com received its one millionth view, according to WordPress.com Stats:</p>
<div class="wp-caption aligncenter" style="width: 325px"><img src="http://www.exploringbinary.com/wp-content/uploads/WPstats.oneMillionViews.png" alt="WordPress.com Stats Reports One Million Views" width="315" height="149"/><p class="wp-caption-text">WordPress.com Stats Reports One Million Views</p></div>
<p><span id="more-351"></span></p>
<p>This site went live on November 14, 2008; that works out to an average of over 920 views per day. My <a title="Rick Regan's Decimal/Binary Converter" href="http://www.exploringbinary.com/binary-converter/">decimal/binary converter</a> (an HTML form with an accompanying article) accounts for nearly half of those views (445,700+).  Not counting my converter, these are my top ten articles:</p>
<ol>
<li><a title="Read Rick Regan's Article &ldquo;Java Hangs When Converting 2.2250738585072012e-308&rdquo;" href="http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/">Java Hangs When Converting 2.2250738585072012e-308</a> (134,500+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;PHP Hangs On Numeric Value 2.2250738585072011e-308&rdquo;" href="http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/">PHP Hangs On Numeric Value 2.2250738585072011e-308</a> (93,600+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;Binary Dates in 2010 and 2011&rdquo;" href="http://www.exploringbinary.com/binary-dates-in-2010-and-2011/">Binary Dates in 2010 and 2011</a> (45,600+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;Ten Ways to Check if an Integer Is a Power Of Two in C&rdquo;" href="http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/">Ten Ways to Check if an Integer Is a Power Of Two in C</a> (37,400+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;Converting Floating-Point Numbers to Binary Strings in C&rdquo;" href="http://www.exploringbinary.com/converting-floating-point-numbers-to-binary-strings-in-c/">Converting Floating-Point Numbers to Binary Strings in C</a> (15,200+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;Why &ldquo;Volatile&rdquo; Fixes the 2.2250738585072011e-308 Bug&rdquo;" href="http://www.exploringbinary.com/why-volatile-fixes-the-2-2250738585072011e-308-bug/">Why &ldquo;Volatile&rdquo; Fixes the 2.2250738585072011e-308 Bug</a> (12,200+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;FPUpdater Fixes the Java 2.2250738585072012e-308 Bug&rdquo;" href="http://www.exploringbinary.com/fpupdater-fixes-the-java-2-2250738585072012e-308-bug/">FPUpdater Fixes the Java 2.2250738585072012e-308 Bug</a> (10,600+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;How I Taught Third Graders Binary Numbers&rdquo;" href="http://www.exploringbinary.com/how-i-taught-third-graders-binary-numbers/">How I Taught Third Graders Binary Numbers</a> (9,800+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;The Answer is One (Unless You Use Floating-Point)&rdquo;" href="http://www.exploringbinary.com/the-answer-is-one-unless-you-use-floating-point/">The Answer is One (Unless You Use Floating-Point)</a> (9,700+ views)</li>
<li><a title="Read Rick Regan's Article &ldquo;A Closer Look at the Java 2.2250738585072012e-308 Bug&rdquo;" href="http://www.exploringbinary.com/a-closer-look-at-the-java-2-2250738585072012e-308-bug/">A Closer Look at the Java 2.2250738585072012e-308 Bug</a> (9,200+ views)</li>
</ol>
<p>Five of my top ten articles are related to the PHP and Java floating-point conversion bugs; together they total 260,200+ views.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/exploring-binary-one-million-views/">Exploring Binary: One Million Views</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/exploring-binary-one-million-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Integers to Check a Floating-Point Approximation</title>
		<link>http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/</link>
		<comments>http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 14:49:32 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Numbers in computers]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Decimals]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Floating-point]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=349</guid>
		<description><![CDATA[For decimal inputs that don&#8217;t qualify for fast path conversion, David Gay&#8217;s strtod() function does three things: first, it uses IEEE double-precision floating-point arithmetic to calculate an approximation to the correct result; next, it uses arbitrary-precision integer arithmetic (AKA big integers) to check if the approximation is correct; finally, it adjusts the approximation, if necessary. [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">Using Integers to Check a Floating-Point Approximation</a></p>
]]></description>
			<content:encoded><![CDATA[<p>For decimal inputs that don&#8217;t qualify for <a title="Read Rick Regan's article &ldquo;Fast Path Decimal to Floating-Point Conversion&rdquo;" href="http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/">fast path conversion</a>, David Gay&#8217;s strtod() function does three things: first, it uses IEEE double-precision floating-point arithmetic to <a title="Read Rick Regan's article &ldquo;strtod()&rsquo;s Initial Decimal to Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/">calculate an approximation</a> to the correct result; next, it uses arbitrary-precision integer arithmetic (AKA big integers) to check if the approximation is correct; finally, it adjusts the approximation, if necessary. In this article, I&#8217;ll explain the second step &#8212; how the check of the approximation is done.</p>
<p><span id="more-349"></span></p>
<h2>Overview</h2>
<p>The goal of David Gay&#8217;s strtod() is to perform efficient, correctly rounded decimal to floating point conversion. To that end, it minimizes the overhead due to arbitrary-precision arithmetic, which is unavoidable for some conversions. In particular, strtod() avoids <em>division</em> of big integers, as would be required by a <a title="Read Rick Regan's article &ldquo;Correct Decimal To Floating-Point Using Big Integers&rdquo;" href="http://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/">full-blown big integer algorithm</a>. Instead of dividing, it multiplies, adds, subtracts, and compares.</p>
<p>The key to the algorithm is the floating-point approximation. It can be compared to the original decimal input to see if it&#8217;s too small, correct, or too big (<a title="Read Rick Regan's article &ldquo;strtod()&rsquo;s Initial Decimal to Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/">it will be within 10 ULPs or so of correct</a>). The approximation can be adjusted accordingly.</p>
<p>(My discussion will deal only with normal double-precision numbers; strtod() handles subnormal numbers as well.)</p>
<h2>Correctly Rounded Conversions</h2>
<p>For a floating-point number <em>b</em> to be considered the correctly rounded conversion of a decimal number <em>d</em>, it must be the <em>closest</em> floating-point number to <em>d</em>. That definition sounds simple enough, but there&#8217;s a problem: the meaning of &ldquo;closest&rdquo; can depend on the value of <em>b</em>. In any case, the tests for closeness are all based on a measure called a ULP &#8212; a binary (for our purposes) <em>unit in the last place</em>. A ULP is the gap between adjacent floating-point numbers.</p>
<p>I&#8217;ll present the tests as three separate cases, although in code they must be considered together.</p>
<h3>Case 1: <em>d</em> is less than one-half ULP away from <em>b</em></h3>
<p>If <em>d</em> is less than one-half of a ULP from <em>b</em>, then <em>b</em> is the correctly rounded conversion of <em>d</em> (this test is ambiguous when <em>b</em> is a power of two &#8212; I&#8217;ll explain below). Mathematically, this is stated as</p>
<p>b &#8211; <em>h</em> &lt; d &lt; b + <em>h</em> ,</p>
<p>where <em>h</em> = 1/2&middot;<em>u</em>, and where <em>u</em> equals one ULP.</p>
<p>Here&#8217;s what the relationship looks like:</p>
<div class="wp-caption aligncenter" style="width: 494px"><img src="http://www.exploringbinary.com/wp-content/uploads/decBinInterval.png" alt="Correct rounding when d is less than one-half ULP away from b" width="484" height="166"/><p class="wp-caption-text">Correct rounding when <em>d</em> is less than one-half ULP away from <em>b</em></p></div>
<p>Equivalently, this relationship can be written as</p>
<p>-<em>h</em> &lt; <em>d</em> &#8211; <em>b</em> &lt; <em>h</em> ,</p>
<p>or</p>
<p>&#124;<em>d</em> &#8211; <em>b</em>&#124; &lt; <em>h</em> .</p>
<h3>Case 2: <em>d</em> is one-half ULP away from <em>b</em></h3>
<p>If <em>d</em> is one-half of a ULP away from <em>b</em>, then <em>b</em> is the correctly rounded conversion of <em>d</em> only if <em>b</em>&rsquo;s least significant bit is 0  (this test is ambiguous when <em>b</em> is a power of two &#8212; I&#8217;ll explain below). This is called <a title="Wikipedia article definition of &ldquo;round half to even&rdquo; rounding mode" href="http://en.wikipedia.org/wiki/Rounding#Round_half_to_even">round-half-to-even</a> rounding.</p>
<p>The relationship is written as</p>
<p>&#124;<em>d</em> &#8211; <em>b</em>&#124; = <em>h</em></p>
<div class="wp-caption aligncenter" style="width: 495px"><img src="http://www.exploringbinary.com/wp-content/uploads/decBinInterval.halfway.png" alt="Correct rounding when d is one-half ULP away from b" width="485" height="197"/><p class="wp-caption-text">Correct rounding when <em>d</em> is one-half ULP away from <em>b</em></p></div>
<h3>Case 3: <em>b</em> is a power of two</h3>
<p>If <em>b</em> is a power of two, two different ULP values come into play, making our half ULP test ambiguous. One ULP <em>below</em> <em>b</em> is half as much as one ULP <em>above</em> <em>b</em>, since a ULP doubles at each increasing power of two boundary. This means that if <em>b</em> is a power of two, it is the correctly rounded conversion of <em>d</em> only if <em>d</em> is no more than one-half ULP above <em>b</em> or no more than one-quarter of that same (upper) ULP below <em>b</em>; here is the inequality:</p>
<p>-<em>h</em>/2 &le; <em>d</em> &#8211; <em>b</em> &le; <em>h</em> .</p>
<p>This is what it looks like:</p>
<div class="wp-caption aligncenter" style="width: 499px"><img src="http://www.exploringbinary.com/wp-content/uploads/decBinInterval.PO2.png" alt="Correct rounding when b is a power of two" width="489" height="175"/><p class="wp-caption-text">Correct rounding when <em>b</em> is a power of two</p></div>
<p>We use the &ldquo;&le;&rdquo; signs in the inequality because <em>b</em> is the correctly rounded result even if <em>d</em> = <em>b</em> &#8211; <em>h</em>/2 or <em>d</em> = <em>b</em> + <em>h</em>. This is because the significands of (normal) powers of two are zero.</p>
<h2>Comparing Using Big Integers</h2>
<p>To explain how the algorithm works, I will use this example: <em>d</em> = 1.7864e-45 and <em>b</em> = 0&#120;1.465a72e467d88p-149 (the latter is expressed as a <a title="Read Rick Regan's article &ldquo;Hexadecimal Floating-Point Constants&rdquo;" href="http://www.exploringbinary.com/hexadecimal-floating-point-constants/">hexadecimal floating-point constant</a>).  Here&#8217;s how <em>d</em> and <em>b</em> are related:</p>
<div class="wp-caption aligncenter" style="width: 502px"><img src="http://www.exploringbinary.com/wp-content/uploads/decBinExample.png" alt="1.7864 x 10^-45 In Relation to Its Binary Floating-Point Approximation 0&#120;1.465a72e467d88p-149" width="492" height="215"/><p class="wp-caption-text">1.7864 x 10<sup>-45</sup> In Relation to Its Binary Floating-Point Approximation, 0&#120;1.465a72e467d88p-149</p></div>
<p>The diagram shows that <em>b</em> is the closest floating-point number to <em>d</em> &#8212; but how do we check that programmatically, using binary arithmetic? We need to apply the above test: &#124;<em>d</em> &#8211; <em>b</em>&#124; &lt; <em>h</em>. (I know a priori this is not a special case &#8212; I wanted to keep my example simple.) But there&#8217;s a problem: how do we represent <em>d</em>? Answer: as a big integer. If we also represent <em>b</em> and <em>h</em> as big integers, such that they are proportional to <em>d</em>, we can apply the test given by the inequality.</p>
<p>I&#8217;ve broken the process into four steps:</p>
<h3>Step 1: Represent the significant digits of <em>d</em> and <em>b</em> as integers</h3>
<p>The first step is to represent <em>d</em> as an integer times a power of ten, and <em>b</em> as an integer times a power of two:</p>
<p><em>d</em> = <em>dInt</em> x 10<sup><em>dExp</em></sup><br />
<em>b</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup></p>
<p><em>d</em> was already put in this form when the <a title="Read Rick Regan's article &ldquo;strtod()’s Initial Decimal to Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/">floating point approximation <em>b</em> was computed</a> (but for the purposes of the comparison, <em>all</em> digits of <em>d</em> are used, not just the first 16). <em>b</em> can be put in this form simply, by <a title="Read Rick Regan's article &ldquo;Displaying the Raw Fields of a Floating-Point Number&rdquo;" href="http://www.exploringbinary.com/displaying-the-raw-fields-of-a-floating-point-number/">accessing its underlying floating-point representation</a>: we &ldquo;unnormalize&rdquo; its significand into an integer by decreasing its power of two exponent. We represent the significant digits as a <a title="Read Rick Regan's article &ldquo;Correct Decimal To Floating-Point Using Big Integers&rdquo;" href="http://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/">53-bit integer</a>, which means we subtract 52 from its power of two exponent.</p>
<p>For our example, <em>d</em> = 1.7864e-45 is represented as 17864 x 10<sup>-49</sup>, and <em>b</em> = 0&#120;1.465a72e467d88p-149, which in <a title="Read Rick Regan's article &ldquo;Displaying IEEE Doubles in Binary Scientific Notation&rdquo;" href="http://www.exploringbinary.com/displaying-ieee-doubles-in-binary-scientific-notation/">binary scientific notation</a> is</p>
<p>1.0100011001011010011100101110010001100111110110001 x 2<sup>-149</sup> ,</p>
<p>is represented (in decimal numerals) as 5741268244528520 x 2<sup>-201</sup>. </p>
<h3>Step 2: Represent <em>h</em> as a power of two</h3>
<p>Since <em>bInt</em> is 53-bit integer, <em>u</em> = 2<sup><em>bExp</em></sup>, and <em>h</em> is half that: 2<sup><em>bExp</em>-1</sup>. For consistency in my discussion, I&#8217;ll substitute the variable <em>hExp</em> for <em>bExp</em>-1, giving</p>
<p><em>h</em> = 2<sup><em>hExp</em></sup> .</p>
<h3>Step 3: Scale the inequality</h3>
<p>To complete the conversion to integers, we need to get rid of any negative powers; we do this by scaling both sides of the inequality. Let&#8217;s look at the inequality again:</p>
<p>&#124;<em>d</em> &#8211; <em>b</em>&#124; &lt; <em>h</em> .</p>
<p>Here are the three values as we expressed them above:</p>
<p><em>d</em> = <em>dInt</em> x 10<sup><em>dExp</em></sup><br />
<em>b</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup><br />
<em>h</em> = 2<sup><em>hExp</em></sup></p>
<p>For efficiency, the scaling process will factor out common powers of two from the three powers; to facilitate this, we rewrite 10<sup><em>dExp</em></sup> as 2<sup><em>dExp</em></sup> x 5<sup><em>dExp</em></sup>; now we have</p>
<p><em>d</em> = <em>dInt</em> x 2<sup><em>dExp</em></sup> x 5<sup><em>dExp</em></sup><br />
<em>b</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup><br />
<em>h</em> = 2<sup><em>hExp</em></sup></p>
<p>If any of the exponents <em>dExp</em>, <em>bExp</em>, or <em>hExp</em> are negative, we make them nonnegative by scaling. To reflect any scaling, let&#8217;s rename <em>d</em>, <em>b</em>, and <em>h</em> to <em>dS</em>, <em>bS</em>, and <em>hS</em>, respectively. The scaled inequality is now written</p>
<p>&#124;<em>dS</em> &#8211; <em>bS</em>&#124; &lt; <em>hS</em> ,</p>
<p>and the initial values of <em>dS</em>, <em>bS</em>, and <em>hS</em> are</p>
<p><em>dS</em> = <em>dInt</em> x 2<sup><em>dExp</em></sup> x 5<sup><em>dExp</em></sup><br />
<em>bS</em> = <em>bInt</em> x 2<sup><em>bExp</em></sup><br />
<em>hS</em> = 2<sup><em>hExp</em></sup></p>
<p>To show how scaling works, let&#8217;s continue with our example above; here are the initial values of the variables:</p>
<p><em>dS</em> = 17864  x  2<sup>-49</sup> x 5<sup>-49</sup><br />
<em>bS</em> = 5741268244528520 x 2<sup>-201</sup><br />
<em>hS</em> = 2<sup>-202</sup></p>
<p>In this example, all three exponents &#8212; <em>dExp</em>, <em>bExp</em>, and <em>hExp</em>  &#8212; are negative; we scale the values with a common factor <em>S</em> that makes them all nonnegative. The least common factor that does this is <em>S</em> = 2<sup>202</sup> x 5<sup>49</sup>; our variables then become</p>
<p><em>dS</em> = 17864  x  2<sup>153</sup><br />
<em>bS</em> = 5741268244528520 x 2<sup>1</sup> x 5<sup>49</sup><br />
<em>hS</em> = 5<sup>49</sup></p>
<p>Multiplied out, these are</p>
<p><em>dS</em> = 203970822259994138521801764465966248930731085529088<br />
<em>bS</em> = 203970822259994122305215569213032722473144531250000<br />
<em>hS</em> = 17763568394002504646778106689453125</p>
<h3>Step 4: Apply the test</h3>
<p>Now all we have to do is check the inequality, &#124;<em>dS</em> &#8211; <em>bS</em>&#124; &lt; <em>hS</em>:</p>
<p>&#124;<em>dS</em> &#8211; <em>bS</em>&#124; = 16216586195252933526457586554279088, which is less than <em>hS</em> = 17763568394002504646778106689453125. This proves that <em>b</em> is the correct conversion of <em>d</em>.</p>
<p>(In the diagram for this example above, I&#8217;ve drawn the relationship of <em>d</em> and <em>b</em> proportionally: (<em>dS</em> &#8211; <em>bS</em>)/<em>hS</em> is approximately 0.91, which means that <em>d</em> is about  0.91<em>h</em> above <em>b</em>.)</p>
<h2>Computing the Scaling Exponents In Code</h2>
<p>In code, the scaling is done by operating on native integer variables representing the exponents of the powers of two and five in each of the three scaled values. After the exponents are created, common factors of two are removed to reduce the size of the resulting big integers. Here is how I coded it in C (this is essentially how it&#8217;s done in David Gay&#8217;s strtod() and Java’s FloatingDecimal class; I&#8217;ve renamed the variables and structured it a bit differently to make it clearer):</p>
<pre>
//Initialize scaling exponents
dS_Exp2 = 0;
dS_Exp5 = 0;
bS_Exp2 = 0;
bS_Exp5 = 0;
hS_Exp2 = 0;
hS_Exp5 = 0;

//Adjust for decimal exponent
if (dExp >= 0) {
   dS_Exp2 += dExp;
   dS_Exp5 += dExp;
   }
else {
   bS_Exp2 -= dExp;
   bS_Exp5 -= dExp;
   hS_Exp2 -= dExp;
   hS_Exp5 -= dExp;
   }

//Adjust for binary exponent
if (bExp >= 0) {
    bS_Exp2 += bExp;
   }
else {
   dS_Exp2 -= bExp;
   hS_Exp2 -= bExp;
   }

//Adjust for half ulp exponent
if (hExp >= 0) {
   hS_Exp2 += hExp;
   }
else {
   dS_Exp2 -= hExp;
   bS_Exp2 -= hExp;
   }

//Remove common power of two factor from all three scaled values
common_Exp2 = min(dS_Exp2, min(bS_Exp2, hS_Exp2));
dS_Exp2 -= common_Exp2;
bS_Exp2 -= common_Exp2;
hS_Exp2 -= common_Exp2;
</pre>
<p>Here is how the code executes for our example (I&#8217;ll show the state of the exponent variables after adjusting for each exponent):</p>
<pre class="indented">
//After initializing scaling exponents
dS_Exp2 = 0;
dS_Exp5 = 0;
bS_Exp2 = 0;
bS_Exp5 = 0;
hS_Exp2 = 0;
hS_Exp5 = 0;

//After adjusting for decimal exponent (dExp = -49)
dS_Exp2 = 0;
dS_Exp5 = 0;
bS_Exp2 = 49;
bS_Exp5 = 49;
hS_Exp2 = 49;
hS_Exp5 = 49;

//After adjusting for binary exponent (bExp = -201)
dS_Exp2 = 201;
dS_Exp5 = 0;
bS_Exp2 = 49;
bS_Exp5 = 49;
hS_Exp2 = 250;
hS_Exp5 = 49;

//After adjusting for half ulp exponent (hExp = -202)
dS_Exp2 = 403;
dS_Exp5 = 0;
bS_Exp2 = 251;
bS_Exp5 = 49;
hS_Exp2 = 250;
hS_Exp5 = 49;

//After removing common power of two factor of 2^250
dS_Exp2 = 153;
dS_Exp5 = 0;
bS_Exp2 = 1;
bS_Exp5 = 49;
hS_Exp2 = 0;
hS_Exp5 = 49;
</pre>
<p>After this code executes, the powers are multiplied out as big integers and used to create big integers <em>dS</em>, <em>bS</em>, and <em>hS</em>.</p>
<h3>When <em>d</em>, <em>b</em>, and <em>h</em> are already integers</h3>
<p>When all three exponents are nonnegative, <em>d</em>, <em>b</em>, and <em>h</em> start out as integer values. In this case, they are scaled <em>down</em>, by their common power of two factor.</p>
<h2>Testing the Inequality In Code</h2>
<p>In strtod(), the checking centers around a comparison of &#124;<em>d</em> &#8211; <em>b</em>&#124; (actually, &#124;<em>b</em> &#8211; <em>d</em>&#124;, which is equivalent) to <em>h</em>, which results in three outcomes: &#124;<em>d</em> &#8211; <em>b</em>&#124; &lt; <em>h</em>, &#124;<em>d</em> &#8211; <em>b</em>&#124; = <em>h</em>, or &#124;<em>d</em> &#8211; <em>b</em>&#124; &gt; <em>h</em>. The special cases are worked into the first two outcomes. (Java’s FloatingDecimal class does similar checks, but it is structured differently.)</p>
<p>Regarding the required check at a power of two boundary (comparing <em>d</em> &#8211; <em>b</em> to -<em>h</em>/2), strtod() scales <em>d</em> &#8211; <em>b</em> by two &#8212; to ensure that all values remain integers. This gives the equivalent comparison of 2(<em>d</em> &#8211; <em>b</em>) to -<em>h</em>. (In many cases, <em>h</em> is divisible by two; strtod() could just as easily divide <em>h</em> by two instead of multiplying <em>d</em> &#8211; <em>b</em> by two. Java’s FloatingDecimal class does the former.)</p>
<h3>Adjusting <em>b</em></h3>
<p>strtod() makes theses checks in a loop, and adjusts <em>b</em> as necessary (that is beyond the scope of this article).</p>
<h2>strtod()&rsquo;s Optimization Called &ldquo;Bigcomp&rdquo;</h2>
<p>strtod() has an optimization called &ldquo;<a title="Read Rick Regan's Article &ldquo;Bigcomp: Deciding Truncated, Near Halfway Conversions&rdquo;" href="http://www.exploringbinary.com/bigcomp-deciding-truncated-near-halfway-conversions/">bigcomp</a>&rdquo;, which limits the big integer value of <em>d</em> to 40 decimal digits. For decimal inputs of 40 significant digits or less, all 40 are used for <em>d</em>; for decimal inputs greater than 40 digits, only the first 18 are used. Compensating for this truncation of <em>d</em> leads to a more complicated test procedure. (This optimization is on by default; you have to &#35;define variable &ldquo;NO_STRTOD_BIGCOMP&rdquo; to turn it off.)</p>
<h2>References</h2>
<ul>
<li>David Gay&#8217;s <a title="David Gay's dtoa.c" href="http://www.netlib.org/fp/dtoa.c">dtoa.c</a> (see the strtod() function).</li>
<li>David Gay&#8217;s Technical Report <a title="David Gay's Technical Report &ldquo;Correctly Rounded Binary-Decimal and Decimal-Binary Conversions&rdquo;" href="http://www.ampl.com/REFS/rounding.pdf">&ldquo;Correctly Rounded Binary-Decimal and Decimal-Binary Conversions&rdquo;</a>.</li>
<li>William D. Clinger&#8217;s paper <a title="Read William D. Clinger's paper &ldquo;How to Read Floating Point Numbers Accurately&rdquo;" href="http://www.cesura17.net/~will/Professional/Research/Papers/howtoread.pdf">&ldquo;How to Read Floating Point Numbers Accurately&rdquo;</a> (see &lsquo;algorithmR&rsquo;).</li>
<li><a title="FloatingDecimal.java" href="http://www.docjar.com/html/api/sun/misc/FloatingDecimal.java.html">FloatingDecimal.java</a> (see the doubleValue() method).</li>
<li><a title="Python dtoa.c" href="http://svn.python.org/view/python/branches/py3k-dtoa/Python/dtoa.c?revision=83813&#038;view=markup">Python&#8217;s version of David Gay&#8217;s dtoa.c</a>.</li>
</ul>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">Using Integers to Check a Floating-Point Approximation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>strtod()’s Initial Decimal to Floating-Point Approximation</title>
		<link>http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/</link>
		<comments>http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 15:13:05 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Numbers in computers]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Convert to binary]]></category>
		<category><![CDATA[Decimals]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Floating-point]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=347</guid>
		<description><![CDATA[David Gay&#8217;s strtod() function does decimal to floating-point conversion using both IEEE double-precision floating-point arithmetic and arbitrary-precision integer arithmetic. For some inputs, a simple IEEE floating-point calculation suffices to produce the correct result; for other inputs, a combination of IEEE arithmetic and arbitrary-precision arithmetic is required. In the latter case, IEEE arithmetic is used to [...]<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/">strtod()’s Initial Decimal to Floating-Point Approximation</a></p>
]]></description>
			<content:encoded><![CDATA[<p>David Gay&#8217;s strtod() function does decimal to floating-point conversion using both IEEE double-precision floating-point arithmetic and arbitrary-precision integer arithmetic. For some inputs, <a title="Read Rick Regan's article &ldquo;Fast Path Decimal to Floating-Point Conversion&rdquo;" href="http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/">a simple IEEE floating-point calculation suffices</a> to produce the correct result; for other inputs, a combination of IEEE arithmetic and arbitrary-precision arithmetic is required. In the latter case, IEEE arithmetic is used to calculate an <em>approximation</em> to the correct result, which is then refined using arbitrary-precision arithmetic. In this article, I&#8217;ll describe the approximation calculation, which is based on a form of binary exponentiation.</p>
<p><span id="more-347"></span></p>
<h2>Overview of the Approximation Calculation</h2>
<p>Conceptually, the approximation calculation involves two floating-point values: the significant digits of the decimal number, expressed as an integer, and a corresponding power of ten. For example, for the decimal string 163.118762e+109, the calculation is 163118762 x 10<sup>103</sup>; for the decimal string 8.453127e-67, the calculation is 8453127 x 10<sup>-73</sup>. If there are more than 16 significant digits, only the first 16 are used. The decimal string 6.2187331579177550499956283e+100, for example, would be treated as 6.218733157917755e+100, making the calculation 6218733157917755 x 10<sup>85</sup>.</p>
<p>The powers of ten can be very small or very large. For double-precision floating-point (I&#8217;m considering normal numbers only), they range from 10<sup>-323</sup> (e.g., for DBL_MIN = 2.2250738585072014e-308) to 10<sup>308</sup> (e.g., for 1e+308). The goal is to compute them efficiently.</p>
<h2>Right-to-Left Binary Exponentiation</h2>
<p>An efficient way to compute powers of ten is using <a title="Read Dr. Math's description of binary exponentiation" href="http://mathforum.org/library/drmath/view/55603.html">right-to-left binary exponentiation</a>. Conceptually, the process is this: given a power b<sup>n</sup> to compute, write <em>n</em> as a sum of powers of two; then, using the <a href="http://www.exploringbinary.com/the-laws-of-exponents/" title="Read Rick Regan's Article &ldquo;The Laws of Exponents&rdquo;">laws of exponents</a>, rewrite b<sup>n</sup> as a product of <em>b</em> raised to each power of two. For example:</p>
<p>10<sup>103</sup> = 10<sup>1 + 2 + 4 + 32 + 64</sup> = 10<sup>1</sup> * 10<sup>2</sup> * 10<sup>4</sup> * 10<sup>32</sup> * 10<sup>64</sup> .</p>
<p>Evaluating this expression requires only ten multiplications &#8212; six to compute the powers of ten 10<sup>2</sup> through 10<sup>64</sup> (by successive squaring), and four to multiply the five required powers together. If 10<sup>103</sup> were computed naively, it would require 102 multiplications.</p>
<p>As an example for computing negative exponents, consider </p>
<p>10<sup>-73</sup> = 10<sup>-(1 + 8 + 64)</sup> = 10<sup>-1</sup> * 10<sup>-8</sup> * 10<sup>-64</sup> .</p>
<p>This requires eight multiplications &#8212; six to compute the powers of ten 10<sup>-2</sup> through 10<sup>-64</sup>, and two to multiply the three required powers together. The naive calculation would do 72 multiplications.</p>
<h3>Required Set of Powers</h3>
<p>For the range of exponents of double-precision values, the required set of &ldquo;binary powers&rdquo; (powers of ten raised to powers of two) for right-to-left binary exponentiation are</p>
<ul>
<li>{10<sup>-1</sup>, 10<sup>-2</sup>, 10<sup>-4</sup>, 10<sup>-8</sup>, 10<sup>-16</sup>, 10<sup>-32</sup>, 10<sup>-64</sup>, 10<sup>-128</sup>, 10<sup>-256</sup>} for negative exponents, and</li>
<li>{10<sup>1</sup>, 10<sup>2</sup>, 10<sup>4</sup>, 10<sup>8</sup>, 10<sup>16</sup>, 10<sup>32</sup>, 10<sup>64</sup>, 10<sup>128</sup>, 10<sup>256</sup>} for positive exponents.</li>
</ul>
<p>Instead of computing these powers for each conversion, they can be computed once and stored in a table. This reduces the number of multiplications further. In the case of our examples, 10<sup>103</sup> would now require only four multiplications, and 10<sup>-73</sup> would require only two.</p>
<h2>The Approximation Calculation in strtod()</h2>
<p>The approximation calculation in strtod() is based on the calculations I just described, but there are a few differences. The significant digits and power of ten are not built in two variables; one variable is initialized with the significant digits, and then the power of ten is factored in incrementally. This allows for a two-phased calculation: the first phase is a <a title="Read Rick Regan's article &ldquo;Fast Path Decimal to Floating-Point Conversion&rdquo;" href="http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/">fast-path</a> like calculation that handles any component power of ten between 10<sup>-15</sup> and 10<sup>15</sup>; the second phase uses binary exponentiation to finish the computation of the power of ten, for component binary powers 10<sup>-16</sup> and smaller and 10<sup>16</sup> and greater. </p>
<p>In the first phase of the calculation, only positive powers of ten, 10<sup>1</sup> to 10<sup>15</sup>, are used. When negative powers of ten 10<sup>-1</sup> to 10<sup>-15</sup> are called for, the calculation is changed to the equivalent calculation that <em>divides</em> the significant digits by the corresponding <em>positive</em> power of ten. A separate table contains each of the powers of ten from 10<sup>1</sup> to 10<sup>15</sup>, which are looked up directly. The one division or multiplication replaces the up to three multiplications that might be needed for binary exponentiation (10<sup>1</sup> * 10<sup>2</sup> * 10<sup>4</sup> * 10<sup>8</sup>). This not only is potentially more efficient, but reduces the number of possibly inexact operations (operations in which the result must be rounded).</p>
<p>Here are two examples: 163.118762e+109, which is parsed as 163118762 x 10<sup>103</sup>, is calculated as 163118762 * 10<sup>7</sup> * 10<sup>32</sup> * 10<sup>64</sup>; 8.453127e-67, which is parsed as 8453127 x 10<sup>-73</sup>, is calculated as 8453127/10<sup>9</sup> * 10<sup>-64</sup>.</p>
<h3>The Calculation In Code</h3>
<p>I&#8217;ve taken <a title="David Gay's dtoa.c" href="http://www.netlib.org/fp/dtoa.c">strtod()</a>&#8216;s approximation code and distilled it into my own C program. The main difference between my code and strtod() is that strtod() handles overflow (for values near DBL_MAX) and underflow (for values that are near DBL_MIN or are subnormal). I&#8217;ve also renamed a few variables and made a few other small changes to make the code more readable.</p>
<pre>
double strtod_approx(char* decimal)
{
 int exponent, sign, i;
 long long sigDigits16;

 static const double fastPosTens[] = {
   1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
   1e10, 1e11, 1e12, 1e13, 1e14, 1e15};
 static const double binaryPosTens[] = {
   1e16, 1e32, 1e64, 1e128, 1e256};
 static const double binaryNegTens[] = {
   1e-16, 1e-32, 1e-64, 1e-128, 1e-256};

 double d;

 <span class="grayed">parseDecimalApprox(decimal,&amp;sign,&amp;sigDigits16,&amp;exponent);</span>

 <strong>d = sigDigits16;</strong>
 if (sign)
   d = -d;

 if (exponent &gt; 0)
   {
    if (i = exponent &amp; 0xF)
      <strong>d *= fastPosTens[i];</strong>
    if (exponent &gt;&gt;= 4)
      for(i = 0; exponent &gt; 0; i++, exponent &gt;&gt;= 1)
        if (exponent &amp; 1)
          <strong>d *= binaryPosTens[i];</strong>
   }
 else
   if (exponent &lt; 0)
     {
      exponent = -exponent;
      if (i = exponent &amp; 0xF)
        <strong>d /= fastPosTens[i];</strong>
      if (exponent &gt;&gt;= 4)
        for(i = 0; exponent &gt; 0; i++, exponent &gt;&gt;= 1)
          if (exponent &amp; 1)
            <strong>d *= binaryNegTens[i];</strong>
     }

 return d;
}
</pre>
<h4>Notes</h4>
<ul>
<li>This function calls an auxiliary function I wrote called parseDecimalApprox(), which I&#8217;ve not shown. It returns the sign of the number, the value of its first 16 significant digits, and its power of ten exponent.</li>
<li>There are three tables of powers of ten, all precomputed at compile time:
<ul>
<li><strong>fastPosTens</strong>: This holds the 15 <a title="Read Rick Regan's article &ldquo;Why Powers of Ten Up to 10^22 Are Exact As Doubles&rdquo;" href="http://www.exploringbinary.com/why-powers-of-ten-up-to-10-to-the-22-are-exact-as-doubles/">exactly representable powers of ten</a> needed for the first phase of the calculation (1e0 is not used &#8212; it&#8217;s just a placeholder).</li>
<li><strong>binaryPosTens</strong>: This holds the five positive powers of ten used for binary exponentiation.</li>
<li><strong>binaryNegTens</strong>: This holds the five negative powers of ten used for binary exponentiation.</li>
</ul>
</li>
<li>If the exponent is zero, no power of ten is factored in (it would be 10<sup>0</sup> = 1).</li>
</ul>
<p>Realized in code, you can see why the algorithm to compute the power of ten is called right-to-left binary exponentiation. Converting the exponent to a sum of powers of two is the same as converting it to binary; in code, the exponent is a binary integer. The bits of the exponent are accessed from right to left &#8212; least significant to most significant &#8212; using the right shift operator; a 1-bit means the corresponding power of two is part of the exponent. The exponent will be a maximum of 9 bits long.</p>
<h3>Analysis</h3>
<p>There are at most five floating-point multiplications and divisions in this calculation: three multiplications plus one multiplication or division for the worst case powers of ten, and one multiplication to combine the significant digits and the power of ten. If the significant digits start out inexact &#8212; either because they are truncated to 16 digits, or they are 16 digits (truncated or otherwise) but represent a value greater than 2<sup>53</sup> &#8211; 1 &#8212; that&#8217;s a maximum of six inexact operations.</p>
<p>Using my code above, I measured approximations as far as 10 ULPs off; 1.00431469722921494e-140 is one example. (This is consistent with my <a title="Read Rick Regan's article &ldquo;Properties of the Correction Loop in David Gay’s strtod()&rdquo;" href="http://www.exploringbinary.com/properties-of-the-correction-loop-in-david-gays-strtod/">analysis of David Gay&#8217;s code</a>, where the maximum difference I found was 11 ULPs. Why the difference? It could be due to the code that handles underflow and overflow, or it could be because I didn&#8217;t run the same exact tests in both cases.)</p>
<p>Contrast this with my <a title="Read Rick Regan's article &ldquo;Quick and Dirty Decimal to Floating-Point Conversion&rdquo;" href="http://www.exploringbinary.com/quick-and-dirty-decimal-to-floating-point-conversion/">quick and dirty decimal to floating-point conversion program</a>, which does a multiplication or division for every digit. Using that program, I found an example that was 14 ULPs off.</p>
<h2>Discussion</h2>
<h3>A Program That Converts Decimal Values Uses Decimal Values?</h3>
<p>The code converts decimal numbers to floating-point, and yet it itself needs decimal numbers converted to floating-point &#8212; isn&#8217;t that circular? Not really. The compiler&#8217;s implementation would have to be different, or at least modified so that the decimal constants are specified in some other way &#8212; say as <a title="Read Rick Regan's article &ldquo;Hexadecimal Floating-Point Constants&rdquo;" href="http://www.exploringbinary.com/hexadecimal-floating-point-constants/">hexadecimal floating-point constants</a>.</p>
<p>This raises another issue: what if the <a title="Read Rick Regan's article &ldquo;Incorrectly Rounded Conversions in Visual C++&rdquo;" href="http://www.exploringbinary.com/incorrectly-rounded-conversions-in-visual-c-plus-plus/">compiler converts these decimal literals incorrectly</a>? After all, this is part of a larger conversion routine whose goal is correct conversion. My guess is that it does not matter. The worst that can happen is that the approximation will be a little less accurate; it will ultimately be corrected &#8212; by the <a title="Read Rick Regan's Article &ldquo;Using Integers to Check a Floating-Point Approximation&rdquo;" href="http://www.exploringbinary.com/using-integers-to-check-a-floating-point-approximation/">arbitrary-precision based reconciliation process in strtod()</a>. (For what it&#8217;s worth, I checked &#8212; Visual C++ got all of the conversions right.)</p>
<h3>Java’s FloatingDecimal Class</h3>
<p>The same approximation calculation is done in the doubleValue() method of Java’s FloatingDecimal Class, which is modeled on David Gay’s strtod().</p>
<h3>Using Division For the Negative Exponent Case</h3>
<p>For the negative exponent case of the binary exponentiation calculation, I tried using division (by positive powers of ten) instead of multiplication (by negative powers of ten). The division-based calculation ran a little slower, which was expected. But interestingly, the approximations were a little less accurate, on average. I don&#8217;t know why.</p>
<p>By Rick Regan (Copyright &copy; 2008-2012  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/">strtod()’s Initial Decimal to Floating-Point Approximation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/strtod-initial-decimal-to-floating-point-approximation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

