simple-statistics
Version:
28 lines (26 loc) • 601 B
JavaScript
/**
* [Sign](https://en.wikipedia.org/wiki/Sign_function) is a function
* that extracts the sign of a real number
*
* @param {number} x input value
* @returns {number} sign value either 1, 0 or -1
* @throws {TypeError} if the input argument x is not a number
* @private
*
* @example
* sign(2); // => 1
*/
function sign(x) {
if (typeof x === "number") {
if (x < 0) {
return -1;
} else if (x === 0) {
return 0;
} else {
return 1;
}
} else {
throw new TypeError("not a number");
}
}
export default sign;