Skip to content

Commit 03244a2

Browse files
authored
feat: Added signum (#81)
1 parent d097f69 commit 03244a2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Test
3333
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
3434
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
35+
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
3536

3637
## Other
3738
* [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts)

maths/signum.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @function Signum
3+
* @description Returns the sign of a number
4+
* @summary The signum function is an odd mathematical function, which returns the
5+
* sign of the provided real number.
6+
* It can return 3 values: 1 for values greater than zero, 0 for zero itself,
7+
* and -1 for values less than zero
8+
* @param {Number} input
9+
* @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is not a number)
10+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Sign_function)
11+
* @example Signum(10) = 1
12+
* @example Signum(0) = 0
13+
* @example Signum(-69) = -1
14+
* @example Signum("hello world") = NaN
15+
*/
16+
export const Signum = (num: number) => {
17+
if (num === 0) return 0
18+
if (num > 0) return 1
19+
if (num < 0) return -1
20+
21+
return NaN
22+
}

maths/test/signum.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Signum } from "../signum";
2+
3+
test.each([[10, 1], [0, 0], [-69, -1], [NaN, NaN]])("The sign of %i is %i", (num, expected) => {
4+
expect(Signum(num)).toBe(expected)
5+
})

0 commit comments

Comments
 (0)