<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren't_Real&amp;action=history&amp;feed=atom</id>
		<title>Floating-point Numbers Aren't Real - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren't_Real&amp;action=history&amp;feed=atom"/>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;action=history"/>
		<updated>2013-06-19T19:13:09Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.11.0</generator>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=25205&amp;oldid=prev</id>
		<title>Harmonious at 04:48, 8 August 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=25205&amp;oldid=prev"/>
				<updated>2009-08-08T04:48:38Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 04:48, 8 August 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It should go without saying that you shouldn't use floating-point numbers for financial &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;applic&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It should go without saying that you shouldn't use floating-point numbers for financial &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;appliction's — that's what decimal classes in languages like Python and C# are for. Floating-point numbers are intended for efficient scientific computation. But efficiency is worthless without accuracy, so remember the source of rounding errors and code accordingly!&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;By Chuck Allison&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:25204:newid:25205 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=25204&amp;oldid=prev</id>
		<title>Harmonious at 04:45, 8 August 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=25204&amp;oldid=prev"/>
				<updated>2009-08-08T04:45:25Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 04:45, 8 August 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Floating-point numbers are not &amp;quot;real numbers&amp;quot; in the mathematical sense, even though they are called ''real'' in some programming languages, such as Pascal and Fortran. Real numbers have infinite precision and are therefore continuous and non-lossy; floating-point numbers have limited precision, so they are finite, and they resemble &amp;quot;badly-behaved&amp;quot; integers, because they're not evenly spaced throughout &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;the &lt;/del&gt;range.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Floating-point numbers are not &amp;quot;real numbers&amp;quot; in the mathematical sense, even though they are called ''real'' in some programming languages, such as Pascal and Fortran. Real numbers have infinite precision and are therefore continuous and non-lossy; floating-point numbers have limited precision, so they are finite, and they resemble &amp;quot;badly-behaved&amp;quot; integers, because they're not evenly spaced throughout &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;their &lt;/ins&gt;range.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;To illustrate, assign 2147483647 (the largest signed 32-bit integer) to a 32-bit &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; variable (&amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, say), and print it. You'll see 2147483648. Now print &amp;lt;code&amp;gt;x - 64&amp;lt;/code&amp;gt;. Still 2147483648. Now print &amp;lt;code&amp;gt;x-65&amp;lt;/code&amp;gt; and you'll get 2147483520! Why? Because the spacing between adjacent floats in that range is 128, and floating-point operations round to the nearest floating-point number.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;To illustrate, assign 2147483647 (the largest signed 32-bit integer) to a 32-bit &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; variable (&amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, say), and print it. You'll see 2147483648. Now print &amp;lt;code&amp;gt;x - 64&amp;lt;/code&amp;gt;. Still 2147483648. Now print &amp;lt;code&amp;gt;x-65&amp;lt;/code&amp;gt; and you'll get 2147483520! Why? Because the spacing between adjacent floats in that range is 128, and floating-point operations round to the nearest floating-point number.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It should go without saying that you shouldn't use floating-point numbers for financial &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;application's &amp;amp;mdash; that's what decimal classes in languages like Python and C# are for. Floating-point numbers are intended for efficient scientific computation. But efficiency is worthless without accuracy, so remember the source of rounding errors and code accordingly!&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It should go without saying that you shouldn't use floating-point numbers for financial &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;applic&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;By [[Chuck Allison]]&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;This work is licensed under a [http://creativecommons.org/licenses/by/3.0/us/ Creative Commons Attribution 3] &lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;Back to [[97 Things Every Programmer Should Know]] home page&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:23428:newid:25204 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=23428&amp;oldid=prev</id>
		<title>Kevlin at 22:06, 16 February 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=23428&amp;oldid=prev"/>
				<updated>2009-02-16T22:06:09Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:06, 16 February 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;goes &lt;/del&gt;without saying that you shouldn't use floating-point numbers for financial application&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;should go &lt;/ins&gt;without saying that you shouldn't use floating-point numbers for financial application&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;'&lt;/ins&gt;s &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;amp;mdash; &lt;/ins&gt;that's what decimal classes in languages like Python and C# are for. Floating-point numbers are intended for efficient scientific computation. But efficiency is worthless without accuracy, so remember the source of rounding errors and code accordingly!&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;s &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;-- &lt;/del&gt;that's what decimal classes in languages like Python and C# are for. Floating-point numbers are intended for efficient scientific computation. But efficiency is worthless without accuracy, so remember the source of rounding errors and code accordingly!&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;By [[Chuck Allison]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;By [[Chuck Allison]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:23425:newid:23428 --&gt;
&lt;/table&gt;</summary>
		<author><name>Kevlin</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=23425&amp;oldid=prev</id>
		<title>Harmonious at 21:09, 16 February 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=23425&amp;oldid=prev"/>
				<updated>2009-02-16T21:09:25Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 21:09, 16 February 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 10:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 10:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;'' by the formula ''1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...''. This works fine for positive ''x'', but consider what happens when ''x'' is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative ''x'', compute ''e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;It goes without saying that you shouldn't use floating-point numbers for financial application&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;s -- that's what decimal classes in languages like Python and C# are for. Floating-point numbers are intended for efficient scientific computation. But efficiency is worthless without accuracy, so remember the source of rounding errors and code accordingly!&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;By [[Chuck Allison]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;By [[Chuck Allison]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:23248:newid:23425 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=23248&amp;oldid=prev</id>
		<title>Kevlin at 11:45, 3 February 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=23248&amp;oldid=prev"/>
				<updated>2009-02-03T11:45:32Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 11:45, 3 February 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Knowing the spacing in the neighborhood of a floating-point number can help you avoid classic numerical blunders. For example, if you're performing an iterative calculation, such as searching for the root of an equation, there's no sense in asking for greater precision than the number system can give in the neighborhood of the answer. Make sure that the tolerance you request is no smaller than the spacing there; otherwise you'll loop forever.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Knowing the spacing in the neighborhood of a floating-point number can help you avoid classic numerical blunders. For example, if you're performing an iterative calculation, such as searching for the root of an equation, there's no sense in asking for greater precision than the number system can give in the neighborhood of the answer. Make sure that the tolerance you request is no smaller than the spacing there; otherwise you'll loop forever.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt; &lt;/del&gt;with the quadratic formula. Since the operands in the expression &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt; &lt;/del&gt;are nearly equal in magnitude, you can instead compute the root &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;&lt;/del&gt;, and then obtain &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;&lt;/del&gt;, since for any quadratic equation, &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;&lt;/del&gt;, the roots satisfy &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1 &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;= 0'' &lt;/ins&gt;with the quadratic formula. Since the operands in the expression &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;'' &lt;/ins&gt;are nearly equal in magnitude, you can instead compute the root &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;, and then obtain &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;, since for any quadratic equation, &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;, the roots satisfy &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt; &lt;/del&gt;by the formula &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt; &lt;/del&gt;This works fine for positive &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;x&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;&lt;/del&gt;, but consider what happens when &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;x&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt; &lt;/del&gt;is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;x&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;&lt;/del&gt;, compute &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&amp;gt;&lt;/del&gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;&lt;/del&gt;/&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;code&amp;gt;&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;'' &lt;/ins&gt;by the formula &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''. &lt;/ins&gt;This works fine for positive &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;x&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;, but consider what happens when &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;x&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;'' &lt;/ins&gt;is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;x&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;, compute &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''&lt;/ins&gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;''.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;By [[Chuck Allison]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;This work is licensed under a [http:&lt;/ins&gt;/&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;/creativecommons&lt;/ins&gt;.&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;org/licenses/by/3.0/us/ Creative Commons Attribution 3] &lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;Back to [[97 Things Every Programmer Should Know]] home page&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:22924:newid:23248 --&gt;
&lt;/table&gt;</summary>
		<author><name>Kevlin</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22924&amp;oldid=prev</id>
		<title>Harmonious at 20:05, 2 January 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22924&amp;oldid=prev"/>
				<updated>2009-01-02T20:05:57Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 20:05, 2 January 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/code&amp;gt;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:22923:newid:22924 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22923&amp;oldid=prev</id>
		<title>Harmonious at 20:05, 2 January 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22923&amp;oldid=prev"/>
				<updated>2009-01-02T20:05:28Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 20:05, 2 January 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/co&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;de&amp;gt;.&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:22922:newid:22923 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22922&amp;oldid=prev</id>
		<title>Harmonious at 20:04, 2 January 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22922&amp;oldid=prev"/>
				<updated>2009-01-02T20:04:28Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 20:04, 2 January 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&amp;lt;/&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;c&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&amp;lt;/&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;co&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;de&amp;gt;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:22921:newid:22922 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22921&amp;oldid=prev</id>
		<title>Harmonious at 20:03, 2 January 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22921&amp;oldid=prev"/>
				<updated>2009-01-02T20:03:24Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 20:03, 2 January 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt;This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;! &lt;/del&gt;The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. Suppose a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt; This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;. &lt;/ins&gt;The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&amp;lt;/&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;c&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&amp;lt;/&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;co&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;de&amp;gt;.&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;By [[Chuck Allison]]&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;This work is licensed under a [http://creativecommons.org/licenses/by/3.0/us/ Creative Commons Attribution 3] &lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;Back to [[97 Things Every Programmer Should Know]] home page&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:22919:newid:22921 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22919&amp;oldid=prev</id>
		<title>Harmonious at 20:02, 2 January 2009</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Floating-point_Numbers_Aren%27t_Real&amp;diff=22919&amp;oldid=prev"/>
				<updated>2009-01-02T20:02:07Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 20:02, 2 January 2009&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 9:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such ''catastrophic cancellation''. To illustrate, consider solving the equation &amp;lt;code&amp;gt;x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 100000x + 1&amp;lt;/code&amp;gt; with the quadratic formula. Since the operands in the expression &amp;lt;code&amp;gt;-b + sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup?&amp;gt; - 4)&amp;lt;/code&amp;gt; are nearly equal in magnitude, you can instead compute the root &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt; = -b - sqrt(b&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; - 4)&amp;lt;/code&amp;gt;, and then obtain &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = 1/r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt;, since for any quadratic equation, &amp;lt;code&amp;gt;ax&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; + bx + c = 0&amp;lt;/code&amp;gt;, the roots satisfy &amp;lt;code&amp;gt;r&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;r&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; = c/a&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;Consider what happens if &lt;/del&gt;a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt;This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer! The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Smearing can occur in even more subtle ways. &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;Suppose &lt;/ins&gt;a library naively computes &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt; by the formula &amp;lt;code&amp;gt;1 + x + x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;/2 + x&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;/3! + ...&amp;lt;/code&amp;gt;This works fine for positive &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, but consider what happens when &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered magnitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer! The answer diverges toward positive infinity! The solution here is also simple: for negative &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;, compute &amp;lt;code&amp;gt;e&amp;lt;sup&amp;gt;x&amp;lt;/sup&amp;gt; = 1/e&amp;lt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&amp;lt;/&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;code&lt;/del&gt;&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;sup&amp;gt;|x|&amp;lt;/sup&amp;gt;&amp;lt;/&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;co&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;de&lt;/ins&gt;&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;By [[Chuck Allison]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;By [[Chuck Allison]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:22918:newid:22919 --&gt;
&lt;/table&gt;</summary>
		<author><name>Harmonious</name></author>	</entry>

	</feed>