MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / signum

Function signum

maths/signum.py:6–36  ·  view source on GitHub ↗

Applies signum function on the number Custom test cases: >>> signum(-10) -1 >>> signum(10) 1 >>> signum(0) 0 >>> signum(-20.5) -1 >>> signum(20.5) 1 >>> signum(-1e-6) -1 >>> signum(1e-6) 1 >>> signum("Hello") Traceback (most r

(num: float)

Source from the content-addressed store, hash-verified

4
5
6def signum(num: float) -> int:
7 """
8 Applies signum function on the number
9
10 Custom test cases:
11 >>> signum(-10)
12 -1
13 >>> signum(10)
14 1
15 >>> signum(0)
16 0
17 >>> signum(-20.5)
18 -1
19 >>> signum(20.5)
20 1
21 >>> signum(-1e-6)
22 -1
23 >>> signum(1e-6)
24 1
25 >>> signum("Hello")
26 Traceback (most recent call last):
27 ...
28 TypeError: '<' not supported between instances of 'str' and 'int'
29 >>> signum([])
30 Traceback (most recent call last):
31 ...
32 TypeError: '<' not supported between instances of 'list' and 'int'
33 """
34 if num < 0:
35 return -1
36 return 1 if num else 0
37
38
39def test_signum() -> None:

Callers 2

test_signumFunction · 0.85
signum.pyFile · 0.85

Calls

no outgoing calls

Tested by 1

test_signumFunction · 0.68