{"id":5088,"date":"2018-11-14T09:27:56","date_gmt":"2018-11-14T09:27:56","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=5088"},"modified":"2019-06-12T13:02:12","modified_gmt":"2019-06-12T13:02:12","slug":"tolerated-comparison-part-1","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2018\/11\/tolerated-comparison-part-1\/","title":{"rendered":"Tolerated Comparison, Part 1"},"content":{"rendered":"<p><em>Warning: The formula for &#8220;tolerated&#8221; comparison in this post is not correct for actual floating-point numbers. Do not use it! <a href=\"https:\/\/www.dyalog.com\/blog\/2019\/06\/tolerated-comparison-part-2\/\">The follow-up post<\/a> discusses how to compute the required value precisely in the presence of floating-point error.<\/em><\/p>\n<p>Tolerant comparison is an important feature in making APL usable in the real world. But it&#8217;s also a lot slower than exact comparison. We think this tradeoff is well worth it (and of course, if you don&#8217;t want to make it, just set <code class=\"language-apl\">\u2395CT\u21900<\/code>). But once in a while we can completely get rid of the slowdown! Added in Dyalog 17.0, along with vectorised comparison code so fast you might not even notice this change, is a cool technique that I call &#8220;tolerated comparison&#8221;. It works by turning a tolerant comparison with one argument fixed into an intolerant comparison by adjusting that fixed argument. So if you&#8217;re comparing one floating-point number to many of them, your code will run 30-40% faster than it would without this improvement.<\/p>\n<h2>Why tolerance?<\/h2>\n<p>Let&#8217;s take a trip to a slightly different APL.<\/p>\n<pre><code class=\"language-apl\">      \u2395CT\u21900<\/code><\/pre>\n<p>A world in which tolerant comparison hasn&#8217;t been invented. Two things are equal when they&#8217;re <em>the same<\/em>.<\/p>\n<pre><code class=\"language-apl\">      0.1 = 0.3-0.2\r\n0\r\n      {(0.1\u00d7\u2375) = \u2375\u00f710} \u23738\r\n1 1 0 1 1 0 0 1<\/code><\/pre>\n<p>Okay, that&#8217;s a scary place. What happened?<\/p>\n<p>The floating-point format can&#8217;t possibly represent every real number. Instead, there&#8217;s a carefully chosen set containing just shy of <code class=\"language-apl\">2*64<\/code> numbers which can be expressed. For other numbers, we pick the closest available floating-point number. Usually this is a pretty accurate approximation. But once we perform arithmetic with a few of these values, we might go from &#8220;closest possible approximation&#8221; to &#8220;second-closest approximation&#8221; or even further away. The differences are still tiny, but when the same value\u2014mathematically speaking\u2014is computed in two different ways the results might be different numbers. Just barely different, but in a world without tolerant comparison that means they won&#8217;t be equal any more.<\/p>\n<p>Tolerant comparison is an attempt to fix this. It&#8217;s not perfect, but it&#8217;s better in a lot of real-world situations. It makes <code class=\"language-apl\">=<\/code> a useful function. The rule is that two values are equal when their difference is at most <code class=\"language-apl\">\u2395CT<\/code> times the larger of their magnitudes. This is often called a &#8220;relative epsilon comparison&#8221; outside of the APL world.<\/p>\n<p><strong>Notation<\/strong>: To avoid confusion in the following text, I have used the APL symbols <code class=\"language-apl\">&lt;\u2264=\u2265&gt;\u2260<\/code> for <em>intolerant<\/em> comparisons, and the non-APL symbols <code class=\"language-apl\">\u22e6\u2272\u2242\u2273\u22e7\u2249<\/code> to refer to APL&#8217;s <em>tolerant<\/em> comparisons which depend on <code class=\"language-apl\">\u2395CT<\/code>. Watch out for a few quirks due to the scarcity of symbols to use: the actual APL primitives are denoted by non-APL characters, and the simpler symbol <code class=\"language-apl\">&lt;<\/code> &#8220;less-than&#8221; corresponds to the more complicated <code class=\"language-apl\">\u22e6<\/code> &#8220;less-than and not approximately equal to&#8221;.<\/p>\n<h2>Tolerant comparisons<\/h2>\n<p>The famous formula for tolerant equality <code class=\"language-apl\">a\u2242b<\/code> in terms of intolerant inequality (introduced <a href=\"http:\/\/www.jsoftware.com\/papers\/satn23.htm\">here<\/a>) is<\/p>\n<pre><code class=\"language-apl\">(|a-b) \u2264 \u2395CT\u00d7(|a)\u2308(|b)<\/code><\/pre>\n<p>This allows for a little variation in the arguments, provided that neither one is zero. And it has some other nice properties, assuming <code class=\"language-apl\">\u2395CT\u22641<\/code>:<\/p>\n<ul>\n<li>It&#8217;s reflexive: <code class=\"language-apl\">a\u2242a<\/code> for any number <code class=\"language-apl\">a<\/code>.<\/li>\n<li>It&#8217;s symmetric in <code class=\"language-apl\">a<\/code> and <code class=\"language-apl\">b<\/code>: reversing them makes no difference in the result. This is because <code class=\"language-apl\">\u2308<\/code> and <code class=\"language-apl\">(|-)<\/code> are symmetric functions.<\/li>\n<li>It scales linearly: multiplying both <code class=\"language-apl\">a<\/code> and <code class=\"language-apl\">b<\/code> by the same nonzero value (even a negative number) does not change the result.<\/li>\n<li>It&#8217;s convex in each variable: if <code class=\"language-apl\">a\u2242b<\/code> and <code class=\"language-apl\">a\u2242d<\/code> for some <code class=\"language-apl\">a<\/code>, and <code class=\"language-apl\">c<\/code> is between <code class=\"language-apl\">b<\/code> and <code class=\"language-apl\">d<\/code>, then <code class=\"language-apl\">a\u2242c<\/code>.<\/li>\n<\/ul>\n<p>In fact, tolerant equality as in APL is the only solution (or family of solutions, taking the <code class=\"language-apl\">\u2395CT<\/code> parameter into account) to those constraints, other than the trivial one in which any two numbers are equal.<\/p>\n<p><a href=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig0.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig0.png\" alt=\"\" width=\"532\" height=\"532\" class=\"aligncenter size-full wp-image-5258\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig0.png 532w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig0-150x150.png 150w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig0-300x300.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>The above figure illustrates tolerant comparison of two real numbers. They are exactly equal only on the grey diagonal line, and tolerantly equal within the narrow strip around it. The diagram uses a much larger value for <code class=\"language-apl\">\u2395CT<\/code> than Dyalog&#8217;s default of <code class=\"language-apl\">1e\u00af14<\/code>, or even its maximum allowed value of <code class=\"language-apl\">2*\u00af32<\/code>, either of which would make that strip invisible.<\/p>\n<p>One way to define tolerant inequalities in terms of tolerant equality is to define, for instance, <code class=\"language-apl\">(a\u2272b) \u2261 (a&lt;b)\u2228(a\u2242b)<\/code> using intolerant less-than and tolerant equality. But we&#8217;ll find it more useful to define <code class=\"language-apl\">\u2272<\/code> and <code class=\"language-apl\">\u2273<\/code> directly in terms of intolerant comparisons as with <code class=\"language-apl\">\u2242<\/code>. The formula for <code class=\"language-apl\">\u2272<\/code> is:<\/p>\n<pre><code class=\"language-apl\">(a-b) \u2264 \u2395CT \u00d7 0\u2308a\u2308-b<\/code><\/pre>\n<p>and for <code class=\"language-apl\">\u2273<\/code> we can use either <code class=\"language-apl\">(a\u2273b) \u2261 (b\u2272a)<\/code> or <code class=\"language-apl\">(a\u2273b) \u2261 (-a)\u2272(-b)<\/code> to obtain<\/p>\n<pre><code class=\"language-apl\">(b-a) \u2264 \u2395CT \u00d7 0\u2308b\u2308-a<\/code><\/pre>\n<p>or, negating both sides and swapping the inequality,<\/p>\n<pre><code class=\"language-apl\">(a-b) \u2265 \u2395CT \u00d7 0\u230aa\u230a-b<\/code><\/pre>\n<p>The <code class=\"language-apl\">0\u2308<\/code> term isn&#8217;t strictly necessary (as we&#8217;ll show later), but it helps avoid some confusion when <code class=\"language-apl\">a<\/code> and <code class=\"language-apl\">b<\/code> have different signs. Sometimes it&#8217;s used to speed up comparison by checking if <code class=\"language-apl\">a\u2264b<\/code> intolerantly before performing any multiplication\u2014if it is, the multiplication isn&#8217;t needed.<\/p>\n<p><a href=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig1.png\" alt=\"\" width=\"532\" height=\"532\" class=\"aligncenter size-full wp-image-5261\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig1.png 532w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig1-150x150.png 150w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig1-300x300.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>Above we can see how the equation for tolerant <code class=\"language-apl\">\u2272<\/code> splits into two symmetrical half-formulas, each of which is just a little off from the exact inequality <code class=\"language-apl\">a\u2264b<\/code>. The tolerant inequality accepts either of these conditions.<\/p>\n<p>The region of tolerant equality is now the intersection of the regions for tolerant <code class=\"language-apl\">\u2272<\/code> and <code class=\"language-apl\">\u2273<\/code>. That is:<\/p>\n<pre><code class=\"language-apl\">(a\u2242b) \u2261 (a\u2272b)\u2227(a\u2273b)<\/code><\/pre>\n<p>We&#8217;ll find this decomposition handy later on since tolerant inequalities are so much easier to work with.<\/p>\n<h2>Tolerating an inequality<\/h2>\n<p>What happens if we fix one side of the inequality? Here&#8217;s a diagram for <code class=\"language-apl\">a\u2272b<\/code> with a dashed line drawn where <code class=\"language-apl\">b<\/code> is equal to some fixed <code class=\"language-apl\">B<\/code> and <code class=\"language-apl\">a<\/code> varies.<\/p>\n<p><a href=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig2.png\" alt=\"\" width=\"532\" height=\"532\" class=\"aligncenter size-full wp-image-5259\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig2.png 532w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig2-150x150.png 150w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2018\/08\/fig2-300x300.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>There&#8217;s only one place where the black line bounding the region where <code class=\"language-apl\">a\u2272b<\/code> intersects the line <code class=\"language-apl\">b=B<\/code>. The value of <code class=\"language-apl\">a<\/code> at this intersection has an interesting property: when <code class=\"language-apl\">a<\/code> is (exactly) less than or equal to that value, then it is tolerantly less than or equal to <code class=\"language-apl\">B<\/code>. If we want to compare <code class=\"language-apl\">B<\/code> to many other floating-point values, and we know the <code class=\"language-apl\">a<\/code> coordinate of the intersection above, then we can skip all of the tolerant comparison arithmetic and just use exact comparison with that value!<\/p>\n<p>Once we have a way to do this for <code class=\"language-apl\">\u2272<\/code> all other inequalities will follow, since <code class=\"language-apl\">(a\u2273B) \u2261 (-a)\u2272(-B)<\/code> and <code class=\"language-apl\">(a\u22e7B) \u2261 ~(a\u2272B)<\/code>.<\/p>\n<p>Maybe you can figure out the value from the graphs above. But it&#8217;s also easy to find the value (and prove it works!) using a fact about the maximum function: for real numbers <code class=\"language-apl\">x<\/code>, <code class=\"language-apl\">y<\/code>, and <code class=\"language-apl\">z<\/code>, <code class=\"language-apl\">x\u2264y\u2308z<\/code> is the same as <code class=\"language-apl\">(x\u2264y) \u2228 (x\u2264z)<\/code>. One of the advantages of treating inequalities as functions like any other is that formulas like these are easy to manipulate. Here we go:<\/p>\n<p>Start with the formula for tolerant inequality, writing <code class=\"language-apl\">q<\/code> in place of <code class=\"language-apl\">\u2395CT<\/code>.<\/p>\n<pre><code class=\"language-apl\">(a-B) \u2264 q \u00d7 0\u2308a\u2308-B<\/code><\/pre>\n<p>Distributing <code class=\"language-apl\">q\u00d7<\/code> over <code class=\"language-apl\">\u2308<\/code>, we have<\/p>\n<pre><code class=\"language-apl\">(a-B) \u2264 0 \u2308 (q\u00d7a) \u2308 (-q\u00d7B)<\/code><\/pre>\n<p>and using <code class=\"language-apl\">(x\u2264y\u2308z) \u2261 ((x\u2264y) \u2228 (x\u2264z))<\/code> twice (just like in the previous step, this can be thought of as distributing <code class=\"language-apl\">x\u2264<\/code> over three arguments of an associative function all at once), we can expand this formula to<\/p>\n<pre><code class=\"language-apl\">((a-B) \u2264 0) \u2228 ((a-B) \u2264 q\u00d7a) \u2228 ((a-B) \u2264 -q\u00d7B)<\/code><\/pre>\n<p>which simplifies to<\/p>\n<pre><code class=\"language-apl\">(a \u2264 B) \u2228 ((a\u00d71-q) \u2264 B) \u2228 (a \u2264 B\u00d71-q)<\/code><\/pre>\n<p>or (here we need <code class=\"language-apl\">q&lt;1<\/code>, so that <code class=\"language-apl\">1-q<\/code> is positive; otherwise we&#8217;d have to reverse the middle inequality)<\/p>\n<pre><code class=\"language-apl\">(a \u2264 B) \u2228 (a \u2264 B\u00f71-q) \u2228 (a \u2264 B\u00d71-q)<\/code><\/pre>\n<p>Recombining, we get<\/p>\n<pre><code class=\"language-apl\">a  \u2264  B \u2308 (B\u00f71-q) \u2308 (B\u00d71-q)<\/code><\/pre>\n<p>So that&#8217;s our formula: now the tolerant equality is an intolerant equality, where the right side depends only on <code class=\"language-apl\">B<\/code>!<\/p>\n<h2>Reading the signs<\/h2>\n<p>So we have a formula for a &#8220;tolerated&#8221; value <code class=\"language-apl\">b = B \u2308 (B\u00f71-q) \u2308 (B\u00d71-q)<\/code>. When do the different possible maxima come into play?<\/p>\n<ul>\n<li>If <code class=\"language-apl\">B=0<\/code>, then everything on the right is zero, and <code class=\"language-apl\">b=0<\/code>.<\/li>\n<li>If <code class=\"language-apl\">B&gt;0<\/code>, then <code class=\"language-apl\">B\u00d71-q<\/code> is smaller than <code class=\"language-apl\">B<\/code>, since <code class=\"language-apl\">q&gt;0<\/code>. But that means <code class=\"language-apl\">\u00f71-q<\/code> is larger than 1, and <code class=\"language-apl\">B\u00f71-q<\/code> is larger than <code class=\"language-apl\">B<\/code>. So <code class=\"language-apl\">b=B\u00f71-q<\/code>.<\/li>\n<li>If <code class=\"language-apl\">B&lt;0<\/code>, then the all the inequalities that we got with <code class=\"language-apl\">B&gt;0<\/code> are reversed. This means <code class=\"language-apl\">b=B\u00d71-q<\/code>.<\/li>\n<\/ul>\n<p>Note that none of these cases depend on the branch <code class=\"language-apl\">b=B<\/code>, even though the first one touches it. That means the definition of tolerant inequality would be the same even if we remove that branch. By working backwards through the above derivation we find that the initial <code class=\"language-apl\">0\u2308<\/code> has no effect, and we can remove it from the definition.<\/p>\n<h2>Equality<\/h2>\n<p>Tolerant equality obviously can&#8217;t be reduced to intolerant equality, since many values can be tolerantly equal to a number but only one is intolerantly equal to it. But recalling that <code class=\"language-apl\">(a\u2242b) \u2261 (a\u2272b)\u2227(a\u2273b)<\/code>, we can express it as two intolerant <em>in<\/em>equalities: tolerate <code class=\"language-apl\">b<\/code> for <code class=\"language-apl\">\u2272<\/code> as above. Then tolerate it for <code class=\"language-apl\">\u2273<\/code> (this always yields a value at least as large as the first). <code class=\"language-apl\">a<\/code> is tolerantly equal to <code class=\"language-apl\">b<\/code> if it lies between the two tolerated values.<\/p>\n<p>This case is particularly important for the set functions <code class=\"language-apl\">\u2373\u220a\u2229\u222a~<\/code>, which by definition use tolerant equality to match values in one argument to values in the other. If the non-principal argument (the one containing values to be looked up, like the right argument to <code class=\"language-apl\">\u2373<\/code>) is short enough, then each element in it is looked up using a simple linear search through the principal argument. This means that one number at a time is compared to the entire principal argument (up to the first match found). So for floats we can get a substantial improvement by tolerating each element on the right before comparison.<\/p>\n<h2>Caution!<\/h2>\n<p>That was the easy part.<\/p>\n<p>In this post we&#8217;ve been assuming that floating-point numbers behave like mathematical real numbers. Given that the whole reason tolerant comparison was introduced is that they don&#8217;t, maybe that wasn&#8217;t the best assumption.<\/p>\n<p>The formulas <code class=\"language-apl\">B\u00f71-q<\/code> and <code class=\"language-apl\">B\u00d71-q<\/code> for tolerated comparison are flawed: they produce values that differ slightly from the actual number that should be used in an intolerant equivalent to tolerant comparison. This is very dangerous: many programs could break if two values compare equal in one comparison but not in another! In the next post we&#8217;ll dive into the world of floating-point error analysis and find out how to get the right values. And we&#8217;ll discover that imperfect computation isn&#8217;t all bad when we find a faster alternative to a very slow floating-point division.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Warning: The formula for &#8220;tolerated&#8221; comparison in this post is not correct for actual floating-point numbers. Do not use it! The follow-up post discusses how to compute the required value precisely in the presence of floating-point error. Tolerant comparison is &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2018\/11\/tolerated-comparison-part-1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-5088","post","type-post","status-publish","format-standard","hentry","category-implementation"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/5088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=5088"}],"version-history":[{"count":26,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/5088\/revisions"}],"predecessor-version":[{"id":5901,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/5088\/revisions\/5901"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=5088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=5088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=5088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}