title: How do I determine whether a number is within a percentage of another number
body: <p>I'm writing iPhone code that fuzzily recognizes whether a swiped line is straight-ish. I get the bearing of the two end points and compare it to 0, 90, 180 and 270 degrees with a tolerance of 10 degrees plus or minus. Right now I do it with a bunch of if blocks, which seems super clunky.</p>
  
  <p>How to write a function that, given the <strong>bearing</strong> 0..360, the <strong>tolerance</strong> percentage (say 20% = (-10° to +10°)) and a straight <strong>angle</strong> like 90 degrees, returns whether the bearing is within the tolerance?</p>
  
  <p><strong>Update:</strong> <em>I am, perhaps, being too specific.</em> I think a nice, general function that determines whether a number is within a percentage of another number has utility in many areas.</p>
  
  <p>For instance: Is the number <strong>swipeLength</strong> within <strong>10%</strong> of <strong>maxSwipe</strong>? That would be useful.</p>
  
  <pre><code> BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
        // dunno how to calculate
   }
  
   BOOL result;
  
   float swipeLength1 = 303; 
   float swipeLength2 = 310; 
  
   float tolerance = 10.0; // from -5% to 5%
   float maxSwipe = 320.0;
  
   result = isNumberWithinPercentOfNumber(swipeLength1, tolerance, maxSwipe); 
   // result = NO
  
   result = isNumberWithinPercentOfNumber(swipeLength2, tolerance, maxSwipe);
   // result = YES
  </code></pre>
  
  <p>Do you see what I'm getting at?</p>