Skip to content

Instantly share code, notes, and snippets.

@mimshwright
Created March 6, 2011 19:17
Show Gist options
  • Select an option

  • Save mimshwright/857563 to your computer and use it in GitHub Desktop.

Select an option

Save mimshwright/857563 to your computer and use it in GitHub Desktop.
Returns true if the two numbers are within "maxPercentDifferent" percent of each other.
package utils.number {
/**
* Returns true if the two numbers are within "maxPercentDifferent" percent of each other.
*
* @example <listing version='3.0'>
* FuzzyMath.roughlyEqual(0.7, 0.69); // true
* FuzzyMath.roughlyEqual(0.7, 0.5); // false
*
* FuzzyMath.roughlyEqual(123456789, 123450000); // true
* FuzzyMath.roughlyEqual(123456789, 100000000); // false
* FuzzyMath.roughlyEqual(123456789, 100000000, 0.25); // true
*
* </listing>
*/
public function isRoughlyEqual ( a:Number, b:Number, maxPercentDifferent:Number = 0.10 ):Boolean {
return Math.abs((a/b) - 1.0) < maxPercentDifferent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment