Last active
August 29, 2015 14:18
-
-
Save jamescostian/217aa6fd9487fc96834b to your computer and use it in GitHub Desktop.
Sort of like the difference between x and y, except relative to |x+y|
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
| // Sort of like the difference between x and y, except it's relative to |x+y|. | |
| // So it returns a percentage (so the value is between 0 and 1) | |
| const discrepancy = (x, y) => { | |
| if (y === 0 && x === 0) { | |
| return 0 | |
| } | |
| else if (x + y === 0) { | |
| // They're on opposite ends of 0, so they have the largest discrepancy possible | |
| return 1 | |
| } | |
| else { | |
| let difference = Math.abs(x - y) | |
| return difference / Math.abs(x + y) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment