<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Spot the Bug</title>
	<atom:link href="http://cafe.elharo.com/programming/spot-the-bug/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/programming/spot-the-bug/</link>
	<description>Longer than a blog; shorter than a book</description>
	<lastBuildDate>Wed, 08 Feb 2012 21:45:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Monetary calculations in CL &#124; keyongtech</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-339086</link>
		<dc:creator>Monetary calculations in CL &#124; keyongtech</dc:creator>
		<pubDate>Sun, 18 Jan 2009 17:04:18 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-339086</guid>
		<description>[...] calculations in CL     This was posted on reddit -- http://cafe.elharo.com/programming/spot-the-bug/  Printed representations aside (that can be taken care of with ~$), how do you perform monetary [...]</description>
		<content:encoded><![CDATA[<p>[...] calculations in CL     This was posted on reddit &#8212; <a href="http://cafe.elharo.com/programming/spot-the-bug/" rel="nofollow">http://cafe.elharo.com/programming/spot-the-bug/</a>  Printed representations aside (that can be taken care of with ~$), how do you perform monetary [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Madden</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-219591</link>
		<dc:creator>David Madden</dc:creator>
		<pubDate>Tue, 22 Apr 2008 21:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-219591</guid>
		<description>Take a look at this post: http://jug.org.ua/wiki/display/JavaAlmanac/The+Need+for+BigDecimal+-+Core+Java+Technology+Tech+Tips</description>
		<content:encoded><![CDATA[<p>Take a look at this post: <a href="http://jug.org.ua/wiki/display/JavaAlmanac/The+Need+for+BigDecimal+-+Core+Java+Technology+Tech+Tips" rel="nofollow">http://jug.org.ua/wiki/display/JavaAlmanac/The+Need+for+BigDecimal+-+Core+Java+Technology+Tech+Tips</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Cowan</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-202903</link>
		<dc:creator>John Cowan</dc:creator>
		<pubDate>Mon, 10 Mar 2008 03:58:13 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-202903</guid>
		<description>Alan Little:  Cobol does indeed allow you to say DECIMAL POINT IS COMMA, and you can insert punctuation editing characters wherever you like -- you aren&#039;t restricted to putting one every three places.   In any case, Indian numbers aren&#039;t every four places:  it&#039;s one thousand = 1,000; one lakh (10^5) = 1,00,000, and one crore (10^7) = 1,00,00,000.  It&#039;s Archimede&#039;s &lt;i&gt;Sand Reckoner&lt;/i&gt; that worked in multiples of one myriad (10^4).

Zack:  If MzScheme indeed prints that, it is violating the Scheme standard, which requires that all inexact numbers be printed using the nearest exact numeral.  Python is doing the right thing.</description>
		<content:encoded><![CDATA[<p>Alan Little:  Cobol does indeed allow you to say DECIMAL POINT IS COMMA, and you can insert punctuation editing characters wherever you like &#8212; you aren&#8217;t restricted to putting one every three places.   In any case, Indian numbers aren&#8217;t every four places:  it&#8217;s one thousand = 1,000; one lakh (10^5) = 1,00,000, and one crore (10^7) = 1,00,00,000.  It&#8217;s Archimede&#8217;s <i>Sand Reckoner</i> that worked in multiples of one myriad (10^4).</p>
<p>Zack:  If MzScheme indeed prints that, it is violating the Scheme standard, which requires that all inexact numbers be printed using the nearest exact numeral.  Python is doing the right thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MHMD</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-200684</link>
		<dc:creator>MHMD</dc:creator>
		<pubDate>Wed, 05 Mar 2008 07:38:26 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-200684</guid>
		<description>Hi,

 My perspective about analyzing the above code in java would be,

 BUG:
  Total:9.879999999999999
Expected:
  Total:9.88
Problem description:
  In Java floating points are expressed as fractions in binary formats , so using double as data type for these would have caused the problem.

Solution:
Quick Fix:

  1.Use float instead of double that would give the exact answer need for this case.
  2.In case if the double is needed in anyways then use NumberFormat class to format the output

Original Solution:
  1.Use BigDecimal and NumberFormat class to format the output.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p> My perspective about analyzing the above code in java would be,</p>
<p> BUG:<br />
  Total:9.879999999999999<br />
Expected:<br />
  Total:9.88<br />
Problem description:<br />
  In Java floating points are expressed as fractions in binary formats , so using double as data type for these would have caused the problem.</p>
<p>Solution:<br />
Quick Fix:</p>
<p>  1.Use float instead of double that would give the exact answer need for this case.<br />
  2.In case if the double is needed in anyways then use NumberFormat class to format the output</p>
<p>Original Solution:<br />
  1.Use BigDecimal and NumberFormat class to format the output.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pat Farrell</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-189931</link>
		<dc:creator>Pat Farrell</dc:creator>
		<pubDate>Thu, 14 Feb 2008 03:38:13 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-189931</guid>
		<description>There may be more, but the criminal sin is using floating point for money. 

Any language used by folks who might use money to do anything, needs a Money or Currency intrinsic type so that rookie programmers don&#039;t shoot themselves in the feet using floating point.

I&#039;m not asking for Euro to Pounds Sterling to Dollars, just a Money datatype.</description>
		<content:encoded><![CDATA[<p>There may be more, but the criminal sin is using floating point for money. </p>
<p>Any language used by folks who might use money to do anything, needs a Money or Currency intrinsic type so that rookie programmers don&#8217;t shoot themselves in the feet using floating point.</p>
<p>I&#8217;m not asking for Euro to Pounds Sterling to Dollars, just a Money datatype.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: duncan</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-188934</link>
		<dc:creator>duncan</dc:creator>
		<pubDate>Mon, 11 Feb 2008 23:32:39 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-188934</guid>
		<description>The upstream commenters who point out that some code will have to deal with amounts less than one cent are of course correct, but if you know you will only be dealing with US currency and that you will not have to deal with fractions of a penny, handling currency in pennies makes a lot of sense as a defensive measure. It&#039;s not always appropriate, but where it is I don&#039;t see a problem with it.</description>
		<content:encoded><![CDATA[<p>The upstream commenters who point out that some code will have to deal with amounts less than one cent are of course correct, but if you know you will only be dealing with US currency and that you will not have to deal with fractions of a penny, handling currency in pennies makes a lot of sense as a defensive measure. It&#8217;s not always appropriate, but where it is I don&#8217;t see a problem with it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aadis</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-188472</link>
		<dc:creator>aadis</dc:creator>
		<pubDate>Mon, 11 Feb 2008 03:56:52 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-188472</guid>
		<description>And the shipping URL page should have the order number string pre-appended (like &quot;shipping.jsp?order=#####&quot;). Another less semantic load :)</description>
		<content:encoded><![CDATA[<p>And the shipping URL page should have the order number string pre-appended (like &#8220;shipping.jsp?order=#####&#8221;). Another less semantic load <img src='http://cafe.elharo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramkumar.E.V.</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-188467</link>
		<dc:creator>Ramkumar.E.V.</dc:creator>
		<pubDate>Mon, 11 Feb 2008 03:45:28 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-188467</guid>
		<description>Hi,

I see this from an user perspective.If you see realistic currencies,you can maximum pay to someone for 2 decimal points,you can pay for figures like 12.3456 and all .. you can max pay for 12.34

So the best deal would be to use double for calculations since it takes care of decimal issues by default or use explicit type conversion to float or use BigDecimal whatever but keep it rounded to 2 decimal points at the end when the results are shown to the user.

Cheers
Ram</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I see this from an user perspective.If you see realistic currencies,you can maximum pay to someone for 2 decimal points,you can pay for figures like 12.3456 and all .. you can max pay for 12.34</p>
<p>So the best deal would be to use double for calculations since it takes care of decimal issues by default or use explicit type conversion to float or use BigDecimal whatever but keep it rounded to 2 decimal points at the end when the results are shown to the user.</p>
<p>Cheers<br />
Ram</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garren</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-188462</link>
		<dc:creator>Garren</dc:creator>
		<pubDate>Mon, 11 Feb 2008 03:36:36 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-188462</guid>
		<description>Yeah, Java. Just in case the url &quot;...shipping.jsp&quot; didn&#039;t give it away. 
It&#039;s not &quot;faulty float addition&quot; nor is it a &quot;broken language&quot; (debatable). It&#039;s just good ol&#039;sloppy coding. 
A more interesting game might be, &quot;Fix the bug&quot;. 

class phew {
    public static void main( String[] args ) {
        double price = 5.89, tax = 3.99;
        System.out.println(  price + &quot; + &quot; + tax + &quot; = &quot; + 
        (price + tax) );
//  ( java.lang.Math.round( 100 * (price + tax) ) / 100.00 )
    }
}

5.89 + 3.99 = 9.879999999999999</description>
		<content:encoded><![CDATA[<p>Yeah, Java. Just in case the url &#8220;&#8230;shipping.jsp&#8221; didn&#8217;t give it away.<br />
It&#8217;s not &#8220;faulty float addition&#8221; nor is it a &#8220;broken language&#8221; (debatable). It&#8217;s just good ol&#8217;sloppy coding.<br />
A more interesting game might be, &#8220;Fix the bug&#8221;. </p>
<p>class phew {<br />
    public static void main( String[] args ) {<br />
        double price = 5.89, tax = 3.99;<br />
        System.out.println(  price + &#8221; + &#8221; + tax + &#8221; = &#8221; +<br />
        (price + tax) );<br />
//  ( java.lang.Math.round( 100 * (price + tax) ) / 100.00 )<br />
    }<br />
}</p>
<p>5.89 + 3.99 = 9.879999999999999</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adrian</title>
		<link>http://cafe.elharo.com/programming/spot-the-bug/comment-page-1/#comment-188369</link>
		<dc:creator>Adrian</dc:creator>
		<pubDate>Sun, 10 Feb 2008 23:20:58 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/programming/spot-the-bug/#comment-188369</guid>
		<description>John,

&gt;(9.879~) == (9.88)
&gt;Not approximately - exactly.

Ok fair point. I&#039;d already forgotten &lt;a href=&quot;http://en.wikipedia.org/wiki/0.999...&quot; rel=&quot;nofollow&quot;&gt;http://en.wikipedia.org/wiki/0.999...&lt;/a&gt; after reading it a few months ago. 

I&#039;ll have to fall back on arguing that 9.88 is a better representation for the users in this case.</description>
		<content:encoded><![CDATA[<p>John,</p>
<p>&gt;(9.879~) == (9.88)<br />
&gt;Not approximately &#8211; exactly.</p>
<p>Ok fair point. I&#8217;d already forgotten <a href="http://en.wikipedia.org/wiki/0.999..." rel="nofollow">http://en.wikipedia.org/wiki/0.999&#8230;</a> after reading it a few months ago. </p>
<p>I&#8217;ll have to fall back on arguing that 9.88 is a better representation for the users in this case.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

