-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmaths.py
More file actions
58 lines (50 loc) · 794 Bytes
/
maths.py
File metadata and controls
58 lines (50 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def add(a, b):
"""
Compute the sum of two numbers
>>> add(1, 2)
3
>>> add(1, 0)
1
>>> add(-2, 1)
-1
"""
return a + b
def div(a, b):
"""
Compute the quotient of two numbers
>>> div(6, 2)
3.0
>>> div(3, -1)
-3.0
"""
if b == 0:
raise DivisionByZeroError()
return a / b
def mul(a, b):
"""
Compute the product of two numbers
>>> mul(3, 2)
6
>>> mul(3, 0)
0
>>> mul(-1, 5)
-5
"""
m = 0
for _ in range(b):
m += a
return m
def sub(a, b):
"""
Compute the difference of two numbers
>>> sub(1, 2)
-1
>>> sub(2, 1)
1
>>> sub(1, 0)
1
"""
return a - b
if __name__ == '__main__':
import doctest
doctest.testmod()