Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit65cceae

Browse files
authored
algorithm: signum (#1266)
1 parentf954fb1 commit65cceae

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+
functionsignum(input){
18+
if(input===0)return0
19+
if(input>0)return1
20+
if(input<0)return-1
21+
22+
returnNaN
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)

[8]ページ先頭

©2009-2025 Movatter.jp