Skip to content

Commit

Permalink
✨ feat: Add sign function.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Apr 29, 2021
1 parent d42d030 commit 6c32451
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/compare/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Computes the sign of the input number.
*
* @param {number} v - The number to compute the sign of.
* @return {number} -1 if v < 0, 1 if v > 0, 0 otherwise.
*/
export const sign = (v) => (v < 0 ? -1 : v > 0 ? 1 : 0);
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {shu} from './arithmetic/shu.js';
export {sub} from './arithmetic/sub.js';
export {decreasing} from './compare/decreasing.js';
export {increasing} from './compare/increasing.js';
export {sign} from './compare/sign.js';
export {$0} from './constants/$0.js';
export {$1} from './constants/$1.js';
export {$2} from './constants/$2.js';
Expand Down
29 changes: 29 additions & 0 deletions test/src/compare/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from 'ava';

import {sign} from '../../../src/index.js';

const macro = (t, input, output) => {
t.is(sign(input), output);
};

macro.title = (title, input, output) => title ?? `sign(${input}) = ${output}`;

test(macro, 0, 0);

test(macro, -1, -1);
test(macro, 1, 1);

test(macro, -999, -1);
test(macro, 999, 1);

test(macro, Number.MIN_SAFE_INTEGER, -1);
test(macro, Number.MAX_SAFE_INTEGER, 1);

test(macro, Number.NEGATIVE_INFINITY, -1);
test(macro, Number.POSITIVE_INFINITY, 1);

test(macro, Number.MIN_VALUE, 1);
test(macro, Number.MAX_VALUE, 1);

test(macro, Number.EPSILON, 1);
test(macro, Number.NaN, 0);

0 comments on commit 6c32451

Please sign in to comment.