Skip to content

Commit 1bbb009

Browse files
arjitarora26pre-commit-ci[bot]cclauss
authored
Add signum function (#7526)
* Add signum function * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add typehints for functions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update signum.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent a0cbc20 commit 1bbb009

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

maths/signum.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Signum function -- https://en.wikipedia.org/wiki/Sign_function
3+
"""
4+
5+
6+
def signum(num: float) -> int:
7+
"""
8+
Applies signum function on the number
9+
10+
>>> signum(-10)
11+
-1
12+
>>> signum(10)
13+
1
14+
>>> signum(0)
15+
0
16+
"""
17+
if num < 0:
18+
return -1
19+
return 1 if num else 0
20+
21+
22+
def test_signum() -> None:
23+
"""
24+
Tests the signum function
25+
"""
26+
assert signum(5) == 1
27+
assert signum(-5) == -1
28+
assert signum(0) == 0
29+
30+
31+
if __name__ == "__main__":
32+
print(signum(12))
33+
print(signum(-12))
34+
print(signum(0))

0 commit comments

Comments
 (0)