<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Developing for Developers : Other</title><link>http://blogs.msdn.com/devdev/archive/tags/Other/default.aspx</link><description>Tags: Other</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Floating-point arithmetic</title><link>http://blogs.msdn.com/devdev/archive/2005/08/24/456001.aspx</link><pubDate>Thu, 25 Aug 2005 03:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:456001</guid><dc:creator>dcoetzee</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/devdev/comments/456001.aspx</comments><wfw:commentRss>http://blogs.msdn.com/devdev/commentrss.aspx?PostID=456001</wfw:commentRss><description>&lt;P&gt;What's got a mantissa field, an exponent field, is included in nearly every major programming language, yet relatively few programmers really understand? The answer is floating-point arithmetic. Most programmers realize that floating-point computation is "not exact" and that error can accumulate over time, but they have little conception of which operations cause how much error to build up how quickly, and how to avoid or repair these kinds of problems.&lt;/P&gt;
&lt;P&gt;The applied field of mathematics which studies floating-point arithmetic (among other things) is &lt;EM&gt;numerical analysis&lt;/EM&gt;. The algorithms created by this discipline go directly&amp;nbsp;into advanced numerical libraries used in scientific computing such as &lt;A href="http://www.netlib.org/lapack/"&gt;LAPACK&lt;/A&gt;&amp;nbsp;and are carefully crafted to be numerically stable as possible (you would be surprised how horribly wrong the answers produced by a naive &lt;A href="http://en.wikipedia.org/wiki/Gaussian_elimination"&gt;Gaussian elimination&lt;/A&gt; algorithm or &lt;A href="http://en.wikipedia.org/wiki/Newton%27s_method"&gt;Newton's method&lt;/A&gt; can be in some cases). One of the best sources I know for a crash course in the basics of floating-point theory, including rounding error and common sources of error, the IEEE format and operations, and compiler optimization of floating-point computations, is David Goldberg's &lt;A href="http://docs.sun.com/source/806-3568/ncg_goldberg.html"&gt;&lt;EM&gt;What Every Computer Scientist Should Know About Floating-Point Arithmetic&lt;/EM&gt;&lt;/A&gt;&amp;nbsp;(&lt;A href="http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf"&gt;PDF here&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;If you don't feel like reading a detailed paper on the topic, then let me sum things up in one simple rule: &lt;STRONG&gt;use doubles&lt;/STRONG&gt;. Your first line of defense against roundoff error should always be to add precision - it's easy and it works. Don't think that just because you only "need" 3 to 7 digits of accuracy that floats are fine - you need more accuracy in the intermediate results to ensure that you get those 3 to 7 digits. In some cases the performance impact is unacceptable, or the error is still too high, and you must design (or find) clever numerical algorithms to get the most out of your bits, but to do so up-front is simply premature optimization. In fact, I'd go so far as to say that you should always use the highest-precision floating-point type available to you, and only think about lowering it when a performance problem manifests. This strategy will save you a lot of headaches in the end.&lt;/P&gt;&lt;IMG src="http://moonflare.com/blogfiles/devdev/Log_one_plus_x_relative_error.png" align=right&gt; 
&lt;P&gt;But is precision really a substitute for good algorithms? Surprisingly, in many cases, yes it is. I wrote a short C program computing log(1 + x) for small x using floats in three ways:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The direct, obvious way, using floats. Mean relative error: 0.0157 
&lt;LI&gt;A&amp;nbsp;more numerically stable&amp;nbsp;algorithm given by &lt;EM&gt;x&lt;/EM&gt;log(1 + &lt;EM&gt;x&lt;/EM&gt;)/((1 + &lt;EM&gt;x&lt;/EM&gt;) − 1), using floats. Mean relative error: 3.2 × 10&lt;SUP&gt;-8&lt;/SUP&gt; 
&lt;LI&gt;The direct, obvious way, using doubles. Mean relative error: 3.1 × 10&lt;SUP&gt;-11&lt;/SUP&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Relative error was roughly constant in each case across the trial values. As you can see, a good algorithm helps a lot, but there are many times when precision really can make up for a stupid algorithm.&lt;/P&gt;&lt;P&lt; P&gt;
&lt;P&gt;Derrick Coetzee&lt;BR&gt;&lt;I&gt;&lt;FONT size=2&gt;This posting is provided "AS IS" with no warranties, and confers no rights.&lt;/FONT&gt;&lt;/I&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=456001" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/devdev/archive/tags/Other/default.aspx">Other</category></item><item><title>Welcome!</title><link>http://blogs.msdn.com/devdev/archive/2005/08/16/452421.aspx</link><pubDate>Wed, 17 Aug 2005 02:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:452421</guid><dc:creator>dcoetzee</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/devdev/comments/452421.aspx</comments><wfw:commentRss>http://blogs.msdn.com/devdev/commentrss.aspx?PostID=452421</wfw:commentRss><description>&lt;P&gt;Hi everybody. My name is Derrick Coetzee. I'm totally new to the blog scene - I've barely read any, much less written any - so you'll have to forgive me if (or rather when) I do anything ridiculous. I created this blog to discuss some of the technologies and techniques that interest me most and help expose them to more of the developers out there in the world, as well as to get in touch with people who can tell me more. Because I want to be able to talk about a number of related areas, I had trouble finding a blog description to cover them all, but in the end I settled on "Tools, techniques, and theory for measuring and improving the power and performance of developers and their code." Now, that's a mouthful, but it includes, among other things:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Programming language technology and features, both established and nascent 
&lt;LI&gt;Compilers, compiler optimizations, and some of the limits of compilers 
&lt;LI&gt;Static checking tools for detecting errors such as Lint, FxCop, Spec#, and many others 
&lt;LI&gt;Scalable algorithms and data structures for efficiently solving large problems, including cache-optimized data structures 
&lt;LI&gt;Debugging tools and techniques such as gdb, Windbg, Visual Studio, instrumenting, and custom tools 
&lt;LI&gt;Coding and quality enforcement techniques 
&lt;LI&gt;Theoretical limitations of and equivalencies among real-world problems and how this helps us solve them 
&lt;LI&gt;Occasionally, something completely random&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I intend to address one subject at a time and go into some detail about each one and how it can help you. I hope everyone - including me - will learn something from this blog. Welcome!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=452421" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/devdev/archive/tags/Other/default.aspx">Other</category></item></channel></rss>