From 1bbb0092f3fc311fac9e56e12c1fa223dbe16465 Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Sun, 23 Oct 2022 16:47:30 +0530 Subject: [PATCH] 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 --- maths/signum.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 maths/signum.py diff --git a/maths/signum.py b/maths/signum.py new file mode 100644 index 000000000..148f93176 --- /dev/null +++ b/maths/signum.py @@ -0,0 +1,34 @@ +""" +Signum function -- https://en.wikipedia.org/wiki/Sign_function +""" + + +def signum(num: float) -> int: + """ + Applies signum function on the number + + >>> signum(-10) + -1 + >>> signum(10) + 1 + >>> signum(0) + 0 + """ + if num < 0: + return -1 + return 1 if num else 0 + + +def test_signum() -> None: + """ + Tests the signum function + """ + assert signum(5) == 1 + assert signum(-5) == -1 + assert signum(0) == 0 + + +if __name__ == "__main__": + print(signum(12)) + print(signum(-12)) + print(signum(0))