Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 65cceae

Browse filesBrowse files
authored
algorithm: signum (TheAlgorithms#1266)
1 parent f954fb1 commit 65cceae
Copy full SHA for 65cceae

File tree

Expand file treeCollapse file tree

2 files changed

+44
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+44
-0
lines changed
Open diff view settings
Collapse file

‎Maths/Signum.js‎

Copy file name to clipboard
+25Lines 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 }
Collapse file

‎Maths/test/Signum.test.js‎

Copy file name to clipboard
+19Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.