Skip to content

Commit 65cceae

Browse files
authored
algorithm: signum (#1266)
1 parent f954fb1 commit 65cceae

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Maths/Signum.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
A program to demonstrate the implementation of the signum function,
3+
also known as the sign function, in JavaScript.
4+
5+
The signum function is an odd mathematical function, which returns the
6+
sign of the provided real number.
7+
It can return 3 values: 1 for values greater than zero, 0 for zero itself,
8+
and -1 for values less than zero
9+
10+
Wikipedia: https://en.wikipedia.org/wiki/Sign_function
11+
*/
12+
13+
/**
14+
* @param {Number} input
15+
* @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is not a number)
16+
*/
17+
function signum (input) {
18+
if (input === 0) return 0
19+
if (input > 0) return 1
20+
if (input < 0) return -1
21+
22+
return NaN
23+
}
24+
25+
export { signum }

Maths/test/Signum.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { signum } from '../Signum'
2+
3+
describe('The sign of a number', () => {
4+
it('Sign of 10', () => {
5+
expect(signum(10)).toBe(1)
6+
})
7+
8+
it('Sign of 0', () => {
9+
expect(signum(0)).toBe(0)
10+
})
11+
12+
it('Sign of -420', () => {
13+
expect(signum(-420)).toBe(-1)
14+
})
15+
16+
it('Sign of NaN', () => {
17+
expect(signum(NaN)).toBe(NaN)
18+
})
19+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy