tag:blogger.com,1999:blog-11151112110091502872024-02-07T20:32:52.136-08:00Xerbitronacute blog in an obtuse worldTim Dowtyhttp://www.blogger.com/profile/06055094071786589921noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-1115111211009150287.post-68743704658806308442007-07-20T18:03:00.000-07:002007-07-20T18:09:22.592-07:00x == y for very large values of x and y - part deuxOK, continuing from the previous post, why does eq equal true when comparing numbers that are obviously not equivalent?<br /><br /><br />Well... looking at the numbers that are used, you might take a hint from their magnitude: they are really big numbers. The equivalent script using decimal numbers would look like this:<br /><br /><br /><blockquote style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><p><br /><span style="font-family:Courier New;color:#663333;">var n = 18014398509481984;</span></p><p><span style="font-family:Courier New;color:#663333;">var m = 18014398509481986;</span></p><p><span style="font-family:Courier New;color:#663333;">var eq = (m == n);</span></p><p></p><p></p><p><em><span style="color:#cc9933;">The puzzle script with decimal-encoded numbers. Yes... <span style="font-family:Courier New;">eq == true</span> what's up?</span></em></p></blockquote><p>Yes, the numbers are big... but not so big they won't fit in a 64-bit integer. Looking back at the previous post's script, notice that the bigger of the two numbers is <span style="font-family:Courier New;">0x0040000000000002</span><span style="font-family:Verdana;">; the largest 64-bit integer would be expressed as all sixteen hex digits with a value of <span style="font-family:Courier New;">F</span>. It's not a sign issue, since the MSB of isn't set for either number.</span></p><p><span style="font-family:Verdana;font-size:130%;"><strong>VARIANTs</strong></span></p><p></p><p>So JScript must be storing the numbers internally using something other than a 64-bit integer to store the values. In fact, it uses the <a title="VARIANT" href="http://msdn2.microsoft.com/en-us/library/ms221627.aspx" target="_blank">VARIANT</a> data type. A VARIANT is a discriminated union that can hold just about any common data type--very useful for 'typeless' languages like JScript. A modern (<em>post-Windows 2000</em>) VARIANT can hold a 64-bit integer (a.k.a long long, or VT_I8 in VARIANT-speak), but JScript 5.6 doesn't use it, probably for legacy compatibility reasons.</p><p></p><p><span style="font-family:verdana;font-size:130%;"><strong>Today's Puzzle</strong></span></p><p>So now, another puzzle: what VARIANT type does JScript use to hold these large integers? (hint: it's not VT_I4!)</p>Tim Dowtyhttp://www.blogger.com/profile/06055094071786589921noreply@blogger.com1tag:blogger.com,1999:blog-1115111211009150287.post-2898587159157440852007-07-17T15:57:00.000-07:002007-07-20T18:06:08.396-07:00x == y for very large values of x and y<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtff-8iekDK1NIdQKSPjgaeI4Xa9ehltCBweeJs96T5sk1-p8d4PMdagoPV5UQdy0YIbXdNZXinAdYCE6QXTOBwe6-gOJQvRG40Dz52TOopiLGFtCWz5Asj8spXFfKITfxSdN3KfG60dU/s1600-h/giantNumber.JPG"><img id="BLOGGER_PHOTO_ID_5088313837241015362" style="FLOAT: right; MARGIN: 0px 0px 10px 10px; CURSOR: hand" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtff-8iekDK1NIdQKSPjgaeI4Xa9ehltCBweeJs96T5sk1-p8d4PMdagoPV5UQdy0YIbXdNZXinAdYCE6QXTOBwe6-gOJQvRG40Dz52TOopiLGFtCWz5Asj8spXFfKITfxSdN3KfG60dU/s320/giantNumber.JPG" border="0" /></a><br /><div>Lately, I've been doing some work that has required me to look very closely at the way scripting languages represent scalar values internally and how they are represented when passed to and from a COM server.<br /><br />The results are interesting, but the process of collection was not interesting at all, so if you need this information too, maybe I can save you some pain. </div><br /><div></div><br /><div>OK, what am I talking about? </div><br /><div></div><br /><div>Here's a little JScript puzzle to illustrate:</div><br /><div></div><br /><div><span style="font-family:verdana;font-size:130%;"><strong>The Puzzle</strong><br /></span></div><br /><div><table border="1"><tbody><tr><td><br /><p><span style="font-family:courier new;font-size:85%;">var n = 0x0040000000000000;</span></p><br /><p><span style="font-family:Courier New;font-size:85%;">var m = 0x0040000000000002;</span></p><br /><p><span style="font-family:Courier New;font-size:85%;">var eq = (m == n);</span></p></td></tr></tbody></table></div><br /><p>If you run the code, what will the value of <span style="font-family:courier new;">eq</span> be?</p><br /><p><span style="font-family:verdana;font-size:130%;"><strong>The Answer</strong></span></p><br /><p>What's your answer?</p><br /><p>You probably smell a rat here and answered "true." And you were right! But why were you right? Stay tuned for the details...</p><br /><p></p>Tim Dowtyhttp://www.blogger.com/profile/06055094071786589921noreply@blogger.com0