<?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>Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx</link><description>Without compiling and running this program. Can you tell me what the output would be? class MainProgram { static void Main( string [] args) { double a = Convert .ToDouble( "1.170404" ); double b = Convert .ToDouble( "2.170404" ); double c = Convert .ToDouble(</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593408</link><pubDate>Tue, 09 May 2006 09:23:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593408</guid><dc:creator>Tommy Carlier</dc:creator><description>No, I can't. But I have a feeling that it will say 'false' for all 4 the cases. I know that doubles and == don't like each other very much.</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593533</link><pubDate>Tue, 09 May 2006 14:23:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593533</guid><dc:creator>Bjørn Reppen</dc:creator><description>I wouldn't want to answer this question... ;)</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593562</link><pubDate>Tue, 09 May 2006 15:27:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593562</guid><dc:creator>Simon</dc:creator><description>I'd expect there to be a number of 'False' strings output. Possibly even 4 of them....</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593570</link><pubDate>Tue, 09 May 2006 15:49:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593570</guid><dc:creator>Raymond Lewalen</dc:creator><description>My guess is all true, but I don't think you'd be posing the question if that were the case, but I can't think of why any of them wouldn't be true.</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593571</link><pubDate>Tue, 09 May 2006 15:53:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593571</guid><dc:creator>Philip Hoppe</dc:creator><description>Floating point comparisons are notoriously unreliable. This just proves it.</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593573</link><pubDate>Tue, 09 May 2006 15:56:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593573</guid><dc:creator>Simon</dc:creator><description>OK, only one was. Why that one?</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593600</link><pubDate>Tue, 09 May 2006 16:51:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593600</guid><dc:creator>John Meyer</dc:creator><description>Interesting... a bug in Convert.ToDouble(String)
&lt;br&gt;
&lt;br&gt;changing the code to
&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double a = Convert.ToDouble(1.170404D);
&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double b = Convert.ToDouble(2.170404D);
&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double c = Convert.ToDouble(3.170404D);
&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double d = Convert.ToDouble(4.170404D);
&lt;br&gt;
&lt;br&gt;outputs True 4 times (so does the same code without the Ds)... we should be guaranteed that
&lt;br&gt;
&lt;br&gt;Convert.ToDouble(&amp;quot;1.170404&amp;quot;)==Convert.ToDouble(1.170404)
&lt;br&gt;
&lt;br&gt;is true for any and all numeric values, but we're not! &amp;nbsp;WTF??? &amp;nbsp;:-S</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593618</link><pubDate>Tue, 09 May 2006 17:19:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593618</guid><dc:creator>Kyle</dc:creator><description>Okay, I thought I knew the answer and then I ran the code and I was wrong. &amp;nbsp;Want to explain how it works?</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593627</link><pubDate>Tue, 09 May 2006 17:22:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593627</guid><dc:creator>Jeff Parker</dc:creator><description>hmm well if I had to guess without running it I would guess all of them would write out false. &lt;br&gt;&lt;br&gt;Doubles are big floats, 64 BIT I believe, which when converted like that usually will never maintain the precision very well in a conversion. I would imagine you would get similar results as well if you were converting Int32 to a double that there would be precision loss. &lt;br&gt;&lt;br&gt;The only thing I know of that would maintain the precision in that way could be doing it this way&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double a = 1.170404D;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double b = 2.170404D;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double c = 3.170404D;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double d = 4.170404D;&lt;br&gt;&lt;br&gt;Using Convert here would be a waste since it will just return the original.&lt;br&gt;&lt;br&gt;But that doesn't help you in converting a string to a double. Thats an interesting problem. One of those why I typically avoid floats. If I have to deal with decimal points and need precision I typically use a decimal. Floats are ok if you are going to round them off in display later and not do a whole lot with them. I am going to have to run it now, to see what I get but I am guessing what ever I get will be wierd and not make much sense as dealing with floats don't usually. I am going to have to look in the spec and see what it says as well. I have never researched the rhyme nor reason on floats and their precisions.</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593714</link><pubDate>Tue, 09 May 2006 19:04:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593714</guid><dc:creator>JamesCurran</dc:creator><description>Well, after make a semi-logical guess, I ran the code to discover that I was 75% correct (wrong about b). &amp;nbsp;Of course, a even naive guess would also get you 75% right (but wrong about d instead)&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#593920</link><pubDate>Tue, 09 May 2006 23:37:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:593920</guid><dc:creator>KathyKam</dc:creator><description>I'm going to post answer/explaination in 2 weeks (or around then.. :))&lt;br&gt;</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#594073</link><pubDate>Wed, 10 May 2006 02:47:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:594073</guid><dc:creator>Eber Irigoyen</dc:creator><description>this might have something to do with it...
&lt;br&gt;and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; esp,0FFFFFFF8h</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#594844</link><pubDate>Wed, 10 May 2006 23:37:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:594844</guid><dc:creator>Jeff Parker</dc:creator><description>Hah, well why playing with this to see if I could get the answer I found some other interesting things. &lt;br&gt;&lt;br&gt;Try the folloing code as well: &lt;br&gt;Will it run, or Divide by 0 exception.&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine((1.0D / 0.0D) == (-1.0D / 0.0D));&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(1.0D / 0.0D);</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#595879</link><pubDate>Fri, 12 May 2006 09:17:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:595879</guid><dc:creator>Shailen Sukul</dc:creator><description>The weird behaviour observed by variable d is quite disconcerting and indeed, a larger value of d = 4.180404 will return true ie it will not lose precision in the conversion.
&lt;br&gt;
&lt;br&gt;The question is to ask is: why does double lose precision for the particular values of 4.170404, 5.170404, 6.170404, etc.
&lt;br&gt;
&lt;br&gt;well done kathy, on this excellent puzzle.
&lt;br&gt;I am looking forward to the answer.</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#602168</link><pubDate>Sat, 20 May 2006 00:37:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:602168</guid><dc:creator>Murali</dc:creator><description>I believe that you can attribute that to the IEEE roundoff error inhernent in the double precision floating point variables.&lt;br&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br&gt;#include &amp;lt;math.h&amp;gt;&lt;br&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br&gt;#include &amp;lt;float.h&amp;gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;int main(int argc, char **argv)&lt;br&gt;{&lt;br&gt; &amp;nbsp;double a,d;&lt;br&gt; &amp;nbsp;a = atof(&amp;quot;1.170404&amp;quot;);&lt;br&gt; &amp;nbsp;d = atof(&amp;quot;4.170404&amp;quot;);&lt;br&gt; &amp;nbsp;printf(&amp;quot;a=%.*g, d=%.*g\n&amp;quot;,DBL_DIG,a,DBL_DIG,d);&lt;br&gt;&lt;br&gt;// Reproduces the results&lt;br&gt; &amp;nbsp;printf(&amp;quot;test a: %s\n&amp;quot;,(a == 1.170404?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt; &amp;nbsp;printf(&amp;quot;test d: %s\n&amp;quot;,(d == 4.170404?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt;&lt;br&gt;// A better test when working with floating point numbers&lt;br&gt; &amp;nbsp;printf(&amp;quot;test a: %s\n&amp;quot;,(fabs(a - 1.170404) &amp;lt;= sqrt(DBL_EPSILON)?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt; &amp;nbsp;printf(&amp;quot;test d: %s\n&amp;quot;,(fabs(d - 4.170404) &amp;lt;= sqrt(DBL_EPSILON)?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt; &amp;nbsp;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Notice the the sqrt(DBL_EPSILON) is a better equality test with double precision floating point numbers. == alone for floating point numbers can be risky.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#603310</link><pubDate>Sun, 21 May 2006 20:01:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:603310</guid><dc:creator>Murali</dc:creator><description>I believe that you can attribute that to the IEEE roundoff error inhernent in the double precision floating point variables.&lt;br&gt;I better test would be as follows (shown in C++)&lt;br&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br&gt;#include &amp;lt;math.h&amp;gt;&lt;br&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br&gt;#include &amp;lt;float.h&amp;gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;int main(int argc, char **argv)&lt;br&gt;{&lt;br&gt; &amp;nbsp;double a,d;&lt;br&gt; &amp;nbsp;a = atof(&amp;quot;1.170404&amp;quot;);&lt;br&gt; &amp;nbsp;d = atof(&amp;quot;4.170404&amp;quot;);&lt;br&gt; &amp;nbsp;printf(&amp;quot;a=%.*g, d=%.*g\n&amp;quot;,DBL_DIG,a,DBL_DIG,d);&lt;br&gt;&lt;br&gt;// Reproduces your results&lt;br&gt; &amp;nbsp;printf(&amp;quot;test a: %s\n&amp;quot;,(a == 1.170404?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt; &amp;nbsp;printf(&amp;quot;test d: %s\n&amp;quot;,(d == 4.170404?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt;&lt;br&gt;// A better test when working with floating point numbers&lt;br&gt; &amp;nbsp;printf(&amp;quot;test a: %s\n&amp;quot;,(fabs(a - 1.170404) &amp;lt;= sqrt(DBL_EPSILON)?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt; &amp;nbsp;printf(&amp;quot;test d: %s\n&amp;quot;,(fabs(d - 4.170404) &amp;lt;= sqrt(DBL_EPSILON)?&amp;quot;true&amp;quot;:&amp;quot;false&amp;quot;));&lt;br&gt; &amp;nbsp;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Notice the the sqrt(DBL_EPSILON) is a better equality test with double precision floating point numbers. == alone for floating point numbers can be risky.&lt;br&gt;&lt;br&gt;-Murali&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#610215</link><pubDate>Mon, 29 May 2006 22:02:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:610215</guid><dc:creator>neolee</dc:creator><description>interesting....这看起来是一个浮点数的精度问题,我从对比逻辑上猜想一下: &lt;br&gt;a,b,c,d的四浮点数唯一的区别在于整数位转换成二进制的时候,他们的位数在内存中表示时是不一样的,我认为这是一个关键,a = 1,b=10,c=11,d=100,如果在处理浮点数时(0.170404),最大长度64位刚好除到62位(假设而已),加上整数的2位(好像在内存中的表示时会去掉最前面的&amp;quot;1&amp;quot;),则不存在浮点数的精度问题,可是如果整数位 &amp;gt; 3的话,就会出现浮点数的精度问题,我用程序侧面证明试了一下,所有大于3的数都是false,基本上符合我的猜想.&lt;br&gt;</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#610225</link><pubDate>Mon, 29 May 2006 22:25:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:610225</guid><dc:creator>neolee</dc:creator><description>interesting....&lt;br&gt;this 4 FloatingPoint's integer part chang to binary system,&lt;br&gt;a = 1,&lt;br&gt;b=10,&lt;br&gt;c=11,&lt;br&gt;d=100&lt;br&gt;I think this is a key.&lt;br&gt;If 0.170404 chang to binary system of lenth equal to 62bit(guess),and adding to integer part 2 bit, it's not out of Floating Point , &lt;br&gt;If integer part &amp;nbsp;exceed 3 bit, &amp;nbsp;it's out of Floating Point&lt;br&gt;It's my guess of all.Soryy,my english is very poor,I'm from china. &amp;nbsp;&lt;br&gt;</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#612821</link><pubDate>Thu, 01 Jun 2006 14:13:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:612821</guid><dc:creator>Colin Han</dc:creator><description>0010101110011111100110001011011100011011100010101010000000000000&lt;br&gt;+123456789+123456789+123456789+123456789+123456789+123456789+123&lt;br&gt;&lt;br&gt;This is binary value for 0.170404. it has 51 bit virtual value. &lt;br&gt;Double has 53 bit virtual value. </description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#634325</link><pubDate>Fri, 16 Jun 2006 20:20:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:634325</guid><dc:creator>Jeffrey Sax</dc:creator><description>Excellent find, Kathy! It looks to me like this is due to the different way the C# compiler and the .NET framework parse real numbers.&lt;br&gt;&lt;br&gt;Notice all the zeros at the end of the binary expansion in Colin Han's comment above. The rotor code contains an unnecessary optimization based on the faulty assumption that a round-off error in the 64th bit will not propagate to the final result. My guess is the same error is present in the actual .NET code. See &lt;a rel="nofollow" target="_new" href="http://blogs.extremeoptimization.com/jeffrey/archive/2006/06/16/16647.aspx"&gt;http://blogs.extremeoptimization.com/jeffrey/archive/2006/06/16/16647.aspx&lt;/a&gt; for details.&lt;br&gt;&lt;br&gt;More examples of &amp;quot;floating-point wierdness&amp;quot; can be found in this article: &lt;a rel="nofollow" target="_new" href="http://www.extremeoptimization.com/Resources/Articles/FPDotNetConceptsAndFormats.aspx"&gt;http://www.extremeoptimization.com/Resources/Articles/FPDotNetConceptsAndFormats.aspx&lt;/a&gt;.</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#683085</link><pubDate>Sun, 30 Jul 2006 11:15:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:683085</guid><dc:creator>Dah</dc:creator><description>I am a computer science student..I don't know about &amp;quot;floating point internal expression in computer&amp;quot;..But after I read the article Jeffrey Sax said(&lt;a rel="nofollow" target="_new" href="http://www.extremeoptimization.com/Resources/Articles/FPDotNetConceptsAndFormats.aspx"&gt;http://www.extremeoptimization.com/Resources/Articles/FPDotNetConceptsAndFormats.aspx&lt;/a&gt;)..I think I know the reason..3 in binary format is &amp;quot;11&amp;quot; while 4 is &amp;quot;100&amp;quot;..there's a &amp;quot;overflow&amp;quot; in the computer's internal operation.. maybe that's the reason...</description></item><item><title>Handling Floating Point Numbers</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#703047</link><pubDate>Thu, 17 Aug 2006 00:40:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:703047</guid><dc:creator>Math.NET Project News</dc:creator><description>Did you know, that: a double can represent 0xFF D FFFFFFFFFFFFF = 18437736874454810623 distinct numbers?</description></item><item><title>re: Quiz of the Month: Double Trouble</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#1807789</link><pubDate>Mon, 05 Mar 2007 10:48:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1807789</guid><dc:creator>mona</dc:creator><description>&lt;p&gt;which sum is greater Colum A or Colum B?&lt;/p&gt;</description></item><item><title> Kathy Kam Quiz of the Month Double Trouble | debt settlement program</title><link>http://blogs.msdn.com/kathykam/archive/2006/05/08/592888.aspx#9754102</link><pubDate>Mon, 15 Jun 2009 20:24:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9754102</guid><dc:creator> Kathy Kam Quiz of the Month Double Trouble | debt settlement program</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://edebtsettlementprogram.info/story.php?id=22782"&gt;http://edebtsettlementprogram.info/story.php?id=22782&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>