{"id":2104,"date":"2016-09-10T00:01:21","date_gmt":"2016-09-10T07:01:21","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2104"},"modified":"2016-09-17T09:32:06","modified_gmt":"2016-09-17T16:32:06","slug":"fuzzy-matching-tolerance","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2104","title":{"rendered":"Fuzzy Matching Tolerance"},"content":{"rendered":"<p>Comparing values is a discrete process; an <em>if<\/em> comparison is absolute. The result is either <strong>true<\/strong> or <strong>false<\/strong>. Never is the result <strong>perhaps<\/strong> or <strong>somewhat<\/strong> &mdash; unless you add some <em>fudge<\/em> to the comparison.<br \/>\n<!--more--><br \/>\nWhat is fudge? Well, it&#8217;s delicious. When it comes to coding, however, you fudge allows a smidgen of wiggle room just in case two values don&#8217;t compare exactly.<\/p>\n<p>The issue, of course, is how much fudge to add?<\/p>\n<p>For programming purposes, I refer to the fudge value as <em>variation<\/em>. It can be calculated discretely or as a percentage. Choosing one over the other depends on the type of data you&#8217;re comparing.<\/p>\n<p>A discrete amount of variation is a specific value. For example, you could add a tolerance of 5 points to meter readings to do a fuzzy match. Here&#8217;s how that might look:<\/p>\n<pre><code>if( abs( a - b ) &gt; 5 )\r\n    match = FALSE;\r\nelse\r\n    match = TRUE;<\/code><\/pre>\n<p>The <em>abs()<\/em> function (defined in <code>stdlib.h<\/code>) determines the absolute value of a number, which is always positive. So the statement <code>abs( a - b )<\/code> returns a positive value. If the difference between variables <code>a<\/code> and <code>b<\/code> is greater than 5 (the fudge or variation) then the fuzzy match fails. Otherwise, the items match &mdash; more-or-less.<\/p>\n<p>The following code modifies the example from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2098\">last week&#8217;s Lesson<\/a>. A new variable, <code>variation<\/code>, is added. It&#8217;s set to 5 to allow for a tolerance of 5 points difference between the two array elements.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define COUNT 10\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint main()\r\n{\r\n    int target[COUNT] = { 24, 28, 32, 45, 50,\r\n                          66, 67, 70, 80, 95 };\r\n    int sample[COUNT] = { 26, 26, 30, 42, 50,\r\n                          61, 67, 75, 85, 99 };\r\n    int x,match,variation;\r\n\r\n    match = TRUE;       <span class=\"comments\">\/* initialize match *\/<\/span>\r\n    variation = 5;      <span class=\"comments\">\/* set tolerance *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* compare arrays *\/<\/span>\r\n    for(x=0; x&lt;COUNT; x++)\r\n    {\r\n        if( abs(target[x]-sample[x]) &gt; variation )\r\n        {\r\n            match = FALSE;\r\n            break;\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* display results *\/<\/span>\r\n    if(match)\r\n        printf(\"The arrays match\\n\");\r\n    else\r\n        printf(\"The arrays do not match\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code is pretty much identical (in a fuzzy way) to the original presented last week. The difference is in the <em>if<\/em> comparison at Line 22. The <code>variation<\/code> value allows forgiveness in the comparison. Here&#8217;s a sample run:<\/p>\n<pre><code>The arrays match<\/code><\/pre>\n<p>You can tighten the value of variable <code>variation<\/code> up or down to determine how well the arrays match, but 5 is the minimum for the sample data; when I modified the code so that <code>variation<\/code> is set to 4, the arrays didn&#8217;t match.<\/p>\n<p>Another way to calculate the fuzz is to use a percentage instead of a discrete value. This type of fuzzy matching works better for values that may change dramatically in size, as shown in the sample code. I&#8217;ll demonstrate that approach in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2124\">next week&#8217;s Lesson.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Direct your code to add a smidgen of fudge when it compares two values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2104\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2104","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2104"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2104\/revisions"}],"predecessor-version":[{"id":2145,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2104\/revisions\/2145"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}