Created
March 6, 2011 19:17
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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