Skip to content

Instantly share code, notes, and snippets.

@jamescostian
Last active August 29, 2015 14:18
Show Gist options
  • Save jamescostian/217aa6fd9487fc96834b to your computer and use it in GitHub Desktop.
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|
// 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