Skip to content

Instantly share code, notes, and snippets.

@westc
Created September 2, 2018 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/e1dc5ccb4771b1df5f3ed4f378465aac to your computer and use it in GitHub Desktop.
Save westc/e1dc5ccb4771b1df5f3ed4f378465aac to your computer and use it in GitHub Desktop.
YourJS candidate function: copySign()
function copySign(a, b) {
return (a < 0 || 1 / a < 0) === (b < 0 || 1 / b < 0) ? a : -a;
}
//\\
copySign(4, -0); // -> -4
//\\
copySign(-8, -0); // -> -8
//\\
copySign(0, -1); // -> -0
//\\
copySign(-0, -1); // -> -0
//\\
copySign(-0, 1); // -> 0
//\\
copySign(-0, 0); // -> 0
//\\
copySign(0, -0); // -> -0
//\\
copySign(-0, -0); // -> -0
//\\
copySign(0, 0); // -> 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment