{"id":1199,"date":"2022-08-30T15:14:02","date_gmt":"2022-08-30T15:14:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/an-operator-riddle-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:02","modified_gmt":"2022-08-30T15:14:02","slug":"an-operator-riddle-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/an-operator-riddle-collection-of-common-programming-errors\/","title":{"rendered":"An operator riddle-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<p>Because Python uses a slightly interpretation of your input:<\/p>\n<blockquote>\n<p>Formally, if <code>a, b, c, ..., y, z<\/code> are expressions and <code>op1, op2, ..., opN<\/code> are comparison operators, then <code>a op1 b op2 c ... y opN z<\/code> is equivalent to <code>a op1 b and b op2 c and ... y opN z<\/code>, except that each expression is evaluated at most once.<\/p>\n<\/blockquote>\n<p>This means your lines will be interpreted as<\/p>\n<pre><code>a &lt; a &lt; a = a &lt; a and a &lt; a \/\/ returns false\nc &lt; b &lt; a = c &lt; b and b &lt; a \/\/ returns false\nc &gt; b &gt; a = c &gt; b and b &gt; a \/\/ returns true\na &lt; c &gt; b = a &lt; c and c &gt; b \/\/ returns true\n<\/code><\/pre>\n<p>In C-style languages, an comparison expression will evaluate to either <code>false<\/code> (integer value 0) or <code>true<\/code> (integer value 1). So in C it will behave like<\/p>\n<pre><code>a &lt; a &lt; a = (a &lt; a) &lt; a = 0 &lt; a  \/\/ returns true\nc &lt; b &lt; a = (c &lt; b) &lt; a = 0 &lt; a  \/\/ returns true\nc &gt; b &gt; a = (c &gt; b) &gt; a = 1 &gt; a \/\/ returns false\na &lt; c &gt; b = (a &lt; c) &gt; b = 0 &gt; b \/\/ returns false\n<\/code><\/pre>\n<p>Note that almost all languages define operators with a boolean return value, but since boolean values can be implicit converted to zero or one the proposition above is still valid:<\/p>\n<pre><code>\/\/ C++ example\nstruct myComparableObject{\n    int data;\n    bool operator<\/code><\/pre>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:09:00. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Because Python uses a slightly interpretation of your input: Formally, if a, b, c, &#8230;, y, z are expressions and op1, op2, &#8230;, opN are comparison operators, then a op1 b op2 c &#8230; y opN z is equivalent to a op1 b and b op2 c and &#8230; y opN z, except that each [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1199","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1199"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1199\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}