{"id":352,"date":"2011-05-30T10:00:25","date_gmt":"2011-05-30T09:00:25","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=352"},"modified":"2011-05-29T13:46:27","modified_gmt":"2011-05-29T12:46:27","slug":"rectangle-class-equals-method-returns-incorrect-value","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2011\/05\/30\/rectangle-class-equals-method-returns-incorrect-value\/","title":{"rendered":"Rectangle class, equals method returns incorrect value"},"content":{"rendered":"<p>Hi,<\/p>\n<p>recently I&#8217;ve used Rectangle&#8217;s equals() method and found that it incorrectly returns false when both rectangles are the same. Problem occurs when any of the properties of the Rectangle are NaN (not a number).<\/p>\n<p><!--more--><br \/>\nSee the example:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar a:Rectangle = new Rectangle();\r\nvar b:Rectangle = new Rectangle();\r\nvar c:Rectangle = new Rectangle(0, 0, NaN, 0);\r\nvar d:Rectangle = new Rectangle(0, 0, NaN, 0);\r\nvar e:Rectangle = d;\r\ntrace(&quot;a.equals(b) = &quot; + a.equals(b));\/\/a.equals(b) = true\r\ntrace(&quot;c.equals(d) = &quot; + c.equals(d));\/\/c.equals(d) = false\r\ntrace(&quot;c.equals(d) = &quot; + d.equals(e));\/\/c.equals(d) = false\r\ntrace(&quot;e.equals(a) = &quot; + e.equals(a));\/\/e.equals(a) = false\r\n<\/pre>\n<p>According to <a href=\"http:\/\/help.adobe.com\/en_US\/FlashPlatform\/reference\/actionscript\/3\/flash\/geom\/Rectangle.html#equals%28%29\">flash<\/a> help, the equals() method:<\/p>\n<blockquote><p>Determines whether the object specified in the toCompare parameter is equal to this Rectangle object. This method compares the x, y, width, and height properties of an object against the same properties of this Rectangle object. <\/p><\/blockquote>\n<p>But because comparison of NaN always returns false:<\/p>\n<p>following code<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">trace(NaN == NaN);<\/pre>\n<p>will produce following error<\/p>\n<blockquote><p>Warning: Illogical comparison with NaN.  This statement always evaluates to false.<\/p><\/blockquote>\n<p>this method can&#8217;t correctly compare it, the solution would be to use custom equals() method (or fixing the Rectangle implementation):<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\r\n\t\t\/**\r\n\t\t * Compares properties (x,y,width,height) of given Rectangle objects and returns true when both match. This method includes comparing NaN values.\r\n\t\t * @param\ta\r\n\t\t * @param\tb\r\n\t\t * @return\r\n\t\t *\/\r\n\t\tprotected function compareRect(a:Rectangle, b:Rectangle):Boolean\r\n\t\t{\r\n\t\t\tif (a === b) return true;\r\n\r\n\t\t\tif (isNaN(a.x) != isNaN(b.x)) return false;\r\n\t\t\tif (!isNaN(a.x) &amp;&amp; !isNaN(b.x) &amp;&amp; a.x != b.x) return false;\r\n\t\t\tif (isNaN(a.y) != isNaN(b.y)) return false;\r\n\t\t\tif (!isNaN(a.y) &amp;&amp; !isNaN(b.y) &amp;&amp; a.y != b.y) return false;\r\n\t\t\tif (isNaN(a.width) != isNaN(b.width)) return false;\r\n\t\t\tif (!isNaN(a.width) &amp;&amp; !isNaN(b.width) &amp;&amp; a.width != b.width) return false;\r\n\t\t\tif (isNaN(a.height) != isNaN(b.height)) return false;\r\n\t\t\tif (!isNaN(a.height) &amp;&amp; !isNaN(b.height) &amp;&amp; a.height != b.height) return false;\r\n\t\t\t\r\n\t\t\treturn true;\r\n\t\t}\r\n<\/pre>\n<p>and the same example again using our custom method:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar a:Rectangle = new Rectangle();\r\nvar b:Rectangle = new Rectangle();\r\nvar c:Rectangle = new Rectangle(0, 0, NaN, 0);\r\nvar d:Rectangle = new Rectangle(0, 0, NaN, 0);\r\nvar e:Rectangle = d;\r\n\r\ntrace(&quot;compareRect(a,b) = &quot; + compareRect(a,b));\/\/compareRect(a,b) = true\r\ntrace(&quot;compareRect(c,d) = &quot; + compareRect(c,d));\/\/compareRect(c,d) = true\r\ntrace(&quot;compareRect(d,e) = &quot; + compareRect(d,e));\/\/compareRect(d,e) = true\r\ntrace(&quot;compareRect(a,e) = &quot; + compareRect(a, e));\/\/compareRect(a,e) = false\r\n<\/pre>\n<p>Happy Coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, recently I&#8217;ve used Rectangle&#8217;s equals() method and found that it incorrectly returns false when both rectangles are the same. Problem occurs when any of the properties of the Rectangle are NaN (not a number).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[17,72,9],"tags":[12,95,96,35,34],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/352"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=352"}],"version-history":[{"count":5,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/352\/revisions"}],"predecessor-version":[{"id":357,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/352\/revisions\/357"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}