forked from mdrazak2001/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
55 lines (48 loc) · 1.4 KB
/
calculator.py
File metadata and controls
55 lines (48 loc) · 1.4 KB
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
def add(a, b):
# ISSUE 1: This function incorrectly handles negative numbers
if a < 0 or b < 0:
return abs(a) + abs(b) # BUG: Should preserve negatives
return a + b
def subtract(a, b):
return a - b # This one is correct
def multiply(a, b):
# ISSUE 2: This function has performance issues with large numbers
result = 0
if b > 0:
for i in range(b):
result += a
elif b < 0:
for i in range(abs(b)):
result -= a
return result
def divide(a, b):
# ISSUE 3: This function doesn't handle division by zero properly
if b == 0:
return float('inf') # BUG: Should raise an exception instead
return a / b
def power(base, exponent):
# ISSUE 4: This function has multiple bugs
if exponent == 0:
return 1
elif exponent < 0:
# BUG: Doesn't handle negative exponents correctly
return 1 / power(base, exponent)
result = 1
for i in range(exponent):
result *= base
return result
def factorial(n):
# ISSUE 5: This function has recursion issues
if n < 0:
return -1 # BUG: Should raise ValueError for negative inputs
if n == 0:
return 1
return n * factorial(n - 1)
def is_prime(n):
# ISSUE 6: This function has logic errors
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True