<?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>Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx</link><description>A reader also asked the other day why it is that in VBScript, CSng(0.1) = CDbl(0.1) is False . Forget about binary floating point for a moment. Suppose that we had two fixed-point decimal systems, say one with five digits after the decimal place and one</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#355450</link><pubDate>Tue, 18 Jan 2005 21:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:355450</guid><dc:creator>Dave Bacher</dc:creator><description>In general, if you're dealing with money, the correct way is to use an integer multilpied by a scaling factor, instead of an IEEE float of any variety (single or double).&lt;br&gt;&lt;br&gt;The most common case where you see this problem is when someone's dealing with currency -- it hits double precision inaccuracies a lot, and the runtime tries to compensate for it.&lt;br&gt;&lt;br&gt;By using fixed point math (multiply the values by 1000, for example, then divide and convert into a float/double once your done), you eliminate any rounding errors, etc. at the expense of a small increase in the amount of code that you have to write.&lt;br&gt;&lt;br&gt;SQL Server, Oracle and ISO C++ provide mechanisms for handling this.  I believe the .NET runtime has support for it as well.</description></item><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#355451</link><pubDate>Tue, 18 Jan 2005 21:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:355451</guid><dc:creator>Dave Bacher</dc:creator><description>Oh and that goes for any time you care about significance in any computer math -- never use a single or a double if you care about how many digits come after the decimal.</description></item><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#355478</link><pubDate>Tue, 18 Jan 2005 21:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:355478</guid><dc:creator>Eric Lippert</dc:creator><description>Yes, the &amp;quot;decimal&amp;quot; type in .NET is a base-ten floating point number.  It's a 96 bit integer scaled by 10^exp, where exp is any value from 0 to -28.&lt;br&gt;</description></item><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#355624</link><pubDate>Wed, 19 Jan 2005 00:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:355624</guid><dc:creator>James Mastros (theorbtwo)</dc:creator><description>As a matter of fact, when dealing with money, it's often an error to carry any more information that the smallest unit in the currency you're dealing with (for the USD, EUR, and GBP, that's 1/100, but it is 1/1000, or 1/1 in some currencies).  For example, even if the formulae for a morgtage say that the lendee should pay $1.001 per month, that isn't possible.  Instead, they would pay exactly $1.00.  That sort of error, when compounded over 40 years, can become noticible.&lt;br&gt;&lt;br&gt;I once, a long time ago, wrote a customer tracking system that did 6% tax calculations, and automatically assessed late fees.  It was not until it was in production and started assessing late fees to people who showed up as owing &amp;quot;$0.00&amp;quot; and a significant amount of head-scratching did we figure out that some people ended up owing fractional cents.  (Of course, if I were writing the system today, I would have done more tests before putting it into production.)</description></item><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#355690</link><pubDate>Wed, 19 Jan 2005 02:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:355690</guid><dc:creator>Norman Diamond</dc:creator><description>&amp;gt; The Visual Basic designers decided that loss&lt;br&gt;&amp;gt; of information is worse than manufacturing&lt;br&gt;&amp;gt; new information and applied that rule&lt;br&gt;&amp;gt; consistently to the variant arithmetic logic.&lt;br&gt;&lt;br&gt;Except when printing.  See your previous blog entry  ^_^&lt;br&gt;&lt;br&gt;1/18/2005 4:29 PM James Mastros (theorbtwo)&lt;br&gt;&lt;br&gt;&amp;gt; when dealing with money, it's often an error&lt;br&gt;&amp;gt; to carry any more information that the&lt;br&gt;&amp;gt; smallest unit in the currency&lt;br&gt;&lt;br&gt;Surely not.  If interest is compounded daily and every 6 months you're supposed to pay the amount of interest that has accrued during those 6 months, you shouldn't compute each day's interest as zero and add up all those zeroes.  You need to compute with enough extra digits to make the final result accurate within one of those smallest units, and then round once.  (And of course you probably shouldn't be using floating point for any part of this.)</description></item><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#551358</link><pubDate>Tue, 14 Mar 2006 20:51:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:551358</guid><dc:creator>Joe</dc:creator><description> &amp;nbsp; &amp;nbsp;public static double roundnum(double num, int place)&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double n;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;n = num * Math.Pow(10, place);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;n = Math.Sign(n) * Math.Abs(Math.Floor(n + .5));&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return n / Math.Pow(10, place);&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;didn't write this....but looking for comments on it</description></item><item><title>re: Fun with Floating Point Arithmetic, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/18/fun-with-floating-point-arithmetic-part-four.aspx#2286937</link><pubDate>Thu, 26 Apr 2007 17:24:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2286937</guid><dc:creator>Sandeep</dc:creator><description>&lt;p&gt;This is for Dave...&lt;/p&gt;
&lt;p&gt; A BIG THANKS!!&lt;/p&gt;
&lt;p&gt;i just multiplied by 100 ( i had only 2 decimal places) and it solved my issue.&lt;/p&gt;
&lt;p&gt;my code was something like this:&lt;/p&gt;
&lt;p&gt;If pctTotal &amp;gt; 100 then&lt;/p&gt;
&lt;p&gt;... &lt;/p&gt;
&lt;p&gt;Response.Write(&amp;quot;Percentage total cannot be greater than 100. Your percentage total is &amp;quot; &amp;amp; pctTotal )&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;End If&lt;/p&gt;
&lt;p&gt;I was stumped when I got the message like this:&lt;/p&gt;
&lt;p&gt;Percentage total cannot be greater than 100. Your percentage total is 100&lt;/p&gt;
&lt;p&gt;i just changed into If &amp;nbsp;CInt(pctTotal*100))&amp;gt;10000&lt;/p&gt;
&lt;p&gt;Now its all fixed. Thanks guys!&lt;/p&gt;
</description></item></channel></rss>