Skip to content

feat: Added signum #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* Test
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)

## Other
* [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts)
Expand Down
22 changes: 22 additions & 0 deletions maths/signum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @function Signum
* @description Returns the sign of a number
* @summary The signum function is an odd mathematical function, which returns the
* sign of the provided real number.
* It can return 3 values: 1 for values greater than zero, 0 for zero itself,
* and -1 for values less than zero
* @param {Number} input
* @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is not a number)
* @see [Wikipedia](https://en.wikipedia.org/wiki/Sign_function)
* @example Signum(10) = 1
* @example Signum(0) = 0
* @example Signum(-69) = -1
* @example Signum("hello world") = NaN
*/
export const Signum = (num: number) => {
if (num === 0) return 0
if (num > 0) return 1
if (num < 0) return -1

return NaN
}
5 changes: 5 additions & 0 deletions maths/test/signum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Signum } from "../signum";

test.each([[10, 1], [0, 0], [-69, -1], [NaN, NaN]])("The sign of %i is %i", (num, expected) => {
expect(Signum(num)).toBe(expected)
})