<?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: When You Can&#8217;t Count On Your Numbers</title>
	<atom:link href="http://www.yuiblog.com/blog/index.php/2009/03/10/when-you-cant-count-on-your-numbers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/</link>
	<description>The official blog of the YUI Project.</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:46:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
	<item>
		<title>By: robert</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-584687</link>
		<dc:creator>robert</dc:creator>
		<pubDate>Sat, 19 Sep 2009 17:08:33 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-584687</guid>
		<description>I Second the Goldberg reference. 
 http://docs.sun.com/source/806-3568/ncg_goldberg.html

This same issue happens with every single language that has floating point numbers. The IEEE standard means that everyone does these calculations in the same way with the same (very good) set of rules. But it&#039;s impossible to remove the issue and still use binary floating point.

One common workaround is to round to the nearest 1/100th or 1/10000th, this is common in financial software. (More common than storing values in cents)

Another is to use vulgar fractions, either with the denominator as a power of 10 or the general form with any denominator. (This is common in BIG NUMBER packages)

But none of these (including the IEEE 754) is a simple solution; that&#039;s one of the reasons why IEEE 754 is as common as it is, there&#039;s a lot of work gone into that standard.</description>
		<content:encoded><![CDATA[<p>I Second the Goldberg reference.<br />
 <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" rel="nofollow">http://docs.sun.com/source/806-3568/ncg_goldberg.html</a></p>
<p>This same issue happens with every single language that has floating point numbers. The IEEE standard means that everyone does these calculations in the same way with the same (very good) set of rules. But it&#8217;s impossible to remove the issue and still use binary floating point.</p>
<p>One common workaround is to round to the nearest 1/100th or 1/10000th, this is common in financial software. (More common than storing values in cents)</p>
<p>Another is to use vulgar fractions, either with the denominator as a power of 10 or the general form with any denominator. (This is common in BIG NUMBER packages)</p>
<p>But none of these (including the IEEE 754) is a simple solution; that&#8217;s one of the reasons why IEEE 754 is as common as it is, there&#8217;s a lot of work gone into that standard.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Höltje</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-579392</link>
		<dc:creator>Christian Höltje</dc:creator>
		<pubDate>Sun, 22 Mar 2009 17:27:40 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-579392</guid>
		<description>Ah-hah! I discovered why Lua looks like it gets it wrong and Python and JavaScript look like they don&#039;t.

They are all using floats underneath.  But lua is using a different sprintf string to represent the numbers.  This is potentially confusing, but it is also nice in other ways.

Example:
lua&gt; print(0.1 + 0.2)
0.3
lua&gt; print((0.1 + 0.2) == 0.3)
false

This is because Lua is using the sprintf format: &quot;%.14g&quot;

It seems python and JavaScript use &quot;%.17f&quot; or something similar.

Now I understand.  Comment to myself when I was coloring myself confused; floats are binary exponents, not decimal.

I think that probably replacing JavaScript numbers with something like perl&#039;s arbitrary precision number would be best.

Code using workarounds for floats, such as (0.1 + 0.2 - 0.3 &lt; 0.00001), would still work.  And naive code would work correctly.

Ciao!</description>
		<content:encoded><![CDATA[<p>Ah-hah! I discovered why Lua looks like it gets it wrong and Python and JavaScript look like they don&#8217;t.</p>
<p>They are all using floats underneath.  But lua is using a different sprintf string to represent the numbers.  This is potentially confusing, but it is also nice in other ways.</p>
<p>Example:<br />
lua&gt; print(0.1 + 0.2)<br />
0.3<br />
lua&gt; print((0.1 + 0.2) == 0.3)<br />
false</p>
<p>This is because Lua is using the sprintf format: &#8220;%.14g&#8221;</p>
<p>It seems python and JavaScript use &#8220;%.17f&#8221; or something similar.</p>
<p>Now I understand.  Comment to myself when I was coloring myself confused; floats are binary exponents, not decimal.</p>
<p>I think that probably replacing JavaScript numbers with something like perl&#8217;s arbitrary precision number would be best.</p>
<p>Code using workarounds for floats, such as (0.1 + 0.2 &#8211; 0.3 &lt; 0.00001), would still work.  And naive code would work correctly.</p>
<p>Ciao!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Höltje</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-579237</link>
		<dc:creator>Christian Höltje</dc:creator>
		<pubDate>Fri, 20 Mar 2009 22:15:27 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-579237</guid>
		<description>I just did and it still doesn&#039;t explain why it works in Lua and not in JavaScript and Python.

I&#039;ve looked in the Lua code and I don&#039;t see why it would be different, though I haven&#039;t exhausted the lua source yet.

Did you read the Lua article?

Ciao!</description>
		<content:encoded><![CDATA[<p>I just did and it still doesn&#8217;t explain why it works in Lua and not in JavaScript and Python.</p>
<p>I&#8217;ve looked in the Lua code and I don&#8217;t see why it would be different, though I haven&#8217;t exhausted the lua source yet.</p>
<p>Did you read the Lua article?</p>
<p>Ciao!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Douglas Crockford</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-579229</link>
		<dc:creator>Douglas Crockford</dc:creator>
		<pubDate>Fri, 20 Mar 2009 21:14:08 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-579229</guid>
		<description>Christian, I highly recommend that you read the document whose url you posted.</description>
		<content:encoded><![CDATA[<p>Christian, I highly recommend that you read the document whose url you posted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Höltje</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-579217</link>
		<dc:creator>Christian Höltje</dc:creator>
		<pubDate>Fri, 20 Mar 2009 17:54:36 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-579217</guid>
		<description>Color me confused.  Why should &lt;code&gt;0.1 + 0.2&lt;/code&gt; ever not equal &lt;code&gt;0.3&lt;/code&gt;?

floats are usually stored as: &lt;code&gt;NNNN x 10^^POW
&lt;/code&gt;

For the above example it should be something like:

&lt;code&gt;1 x10^-1 + 2 x10^-1 = 3 x10^-1&lt;/code&gt;

How on earth is it being stored that there is &lt;em&gt;noise&lt;/em&gt; like that?

Lua, for example, uses only floating point[1] and it can handle &lt;code&gt;0.1 + 0.2 = 0.3&lt;/code&gt; just fine.

Ciao!

[1] http://lua-users.org/wiki/FloatingPoint</description>
		<content:encoded><![CDATA[<p>Color me confused.  Why should <code>0.1 + 0.2</code> ever not equal <code>0.3</code>?</p>
<p>floats are usually stored as: <code>NNNN x 10^^POW<br />
</code></p>
<p>For the above example it should be something like:</p>
<p><code>1 x10^-1 + 2 x10^-1 = 3 x10^-1</code></p>
<p>How on earth is it being stored that there is <em>noise</em> like that?</p>
<p>Lua, for example, uses only floating point[1] and it can handle <code>0.1 + 0.2 = 0.3</code> just fine.</p>
<p>Ciao!</p>
<p>[1] <a href="http://lua-users.org/wiki/FloatingPoint" rel="nofollow">http://lua-users.org/wiki/FloatingPoint</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trav</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-578943</link>
		<dc:creator>Trav</dc:creator>
		<pubDate>Tue, 17 Mar 2009 15:48:01 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-578943</guid>
		<description>No programmer should be using equality tests on floating-point numbers, although I admit I don&#039;t hear that kind of advice tossed around much anymore. You don&#039;t ask if 0.1 + 0.2 === 0.3 .

Instead, you ask if 0.1 + 0.2 - 0.3 &lt; 0.00001 (or some other suitably small number). This works for all real-world cases where you&#039;d use a float. It isn’t that big a deal. Please do not flame me with complaints that it increases download size.</description>
		<content:encoded><![CDATA[<p>No programmer should be using equality tests on floating-point numbers, although I admit I don&#8217;t hear that kind of advice tossed around much anymore. You don&#8217;t ask if 0.1 + 0.2 === 0.3 .</p>
<p>Instead, you ask if 0.1 + 0.2 &#8211; 0.3 &lt; 0.00001 (or some other suitably small number). This works for all real-world cases where you&#8217;d use a float. It isn’t that big a deal. Please do not flame me with complaints that it increases download size.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bertrand Le Roy</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-578927</link>
		<dc:creator>Bertrand Le Roy</dc:creator>
		<pubDate>Mon, 16 Mar 2009 17:58:52 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-578927</guid>
		<description>The real problem is that JavaScript painted itself into a corner by trying to look cute. While on the surface it does seem like having one numeric type is &quot;simplifying and stabilizing&quot;, the amount of confusion it caused users and the complexity of the things people have to do to work around it should be the deciding factor to update the spec. I have no idea what a right way to do that would be though. Oh well.</description>
		<content:encoded><![CDATA[<p>The real problem is that JavaScript painted itself into a corner by trying to look cute. While on the surface it does seem like having one numeric type is &#8220;simplifying and stabilizing&#8221;, the amount of confusion it caused users and the complexity of the things people have to do to work around it should be the deciding factor to update the spec. I have no idea what a right way to do that would be though. Oh well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-578923</link>
		<dc:creator>Christian</dc:creator>
		<pubDate>Mon, 16 Mar 2009 13:55:24 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-578923</guid>
		<description>Hi, you may try this library &quot;...allow calculations with nearly arbitrary precision&quot;:

http://stz-ida.de/html/oss/js_bigdecimal.html.en  

Source code

http://stz-ida.de/download/oss/js_bigdecimal.tgz</description>
		<content:encoded><![CDATA[<p>Hi, you may try this library &#8220;&#8230;allow calculations with nearly arbitrary precision&#8221;:</p>
<p><a href="http://stz-ida.de/html/oss/js_bigdecimal.html.en" rel="nofollow">http://stz-ida.de/html/oss/js_bigdecimal.html.en</a>  </p>
<p>Source code</p>
<p><a href="http://stz-ida.de/download/oss/js_bigdecimal.tgz" rel="nofollow">http://stz-ida.de/download/oss/js_bigdecimal.tgz</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Hart</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-578890</link>
		<dc:creator>Daniel Hart</dc:creator>
		<pubDate>Sat, 14 Mar 2009 11:27:04 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-578890</guid>
		<description>A minor change to your add function will fix the problem:

&lt;code&gt;
var add = function (a, b) {
    return Math.round((a + b) * 100)/100;
};
&lt;/code&gt;

array.reduce(add, 0) now produces 100 instead of 100.00000000001425</description>
		<content:encoded><![CDATA[<p>A minor change to your add function will fix the problem:</p>
<p><code><br />
var add = function (a, b) {<br />
    return Math.round((a + b) * 100)/100;<br />
};<br />
</code></p>
<p>array.reduce(add, 0) now produces 100 instead of 100.00000000001425</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arthur Blake</title>
		<link>http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/comment-page-1/#comment-578869</link>
		<dc:creator>Arthur Blake</dc:creator>
		<pubDate>Wed, 11 Mar 2009 15:08:02 +0000</pubDate>
		<guid isPermaLink="false">http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/#comment-578869</guid>
		<description>Would love to hear more about the binary number system and why it&#039;s not going to happen...

The link points to another bibliography link that is broken...

With JavaScript gaining more and more importance these days, it seems to me that the real solution is to update to a more intelligent yet backwardly compatible numbering system.</description>
		<content:encoded><![CDATA[<p>Would love to hear more about the binary number system and why it&#8217;s not going to happen&#8230;</p>
<p>The link points to another bibliography link that is broken&#8230;</p>
<p>With JavaScript gaining more and more importance these days, it seems to me that the real solution is to update to a more intelligent yet backwardly compatible numbering system.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

