-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_test.py
More file actions
66 lines (47 loc) · 1.74 KB
/
sum_test.py
File metadata and controls
66 lines (47 loc) · 1.74 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
56
57
58
59
60
61
62
63
64
65
66
# Description : a test class to test sum of a fraction
# Auther : Ouye Xie
# Created on : 20/05/2014
__author__ = 'ouyexie'
__version__ = "0.1"
import random
class SumTest(object):
def __init__(self):
random.seed(10)
num = 100
self.a = [random.randint(1, 10) * 10 for i in range(num)]
self.b = [random.randint(0, 10) for i in range(num)]
def compute(self):
print("a: " + str(self.a))
print("b: " + str(self.b))
exact = 0.0
for i in range(0, len(self.a)):
if self.b[i] != 0:
exact += self.a[i] / self.b[i]
print("exact value: %f" % exact)
print("#########################################")
sum_a = sum(self.a)
print("sum_a: %f" % sum_a)
sum_b = sum(self.b)
print("sum_b: %f" % sum_b)
approximate = ((sum_a * len(self.a)) / sum_b)
print("approxiate value: %f" % approximate)
gap = exact - approximate
print("gap value; %f" % gap)
gap_percentage = gap / exact
print("gap percentage value: %f%%" % (gap_percentage * 100.0))
print("#########################################")
sum_a = sum(self.a)
print("sum_a: %f" % sum_a)
sum_b = sum([(1.0 / item) for item in self.b if item != 0])
print("sum_b: %f" % sum_b)
approximate = (sum_a * sum_b) / len(self.a)
print("approxiate value: %f" % approximate)
gap = exact - approximate
print("gap value; %f" % gap)
gap_percentage = gap / exact
print("gap percentage value: %f%%" % (gap_percentage * 100.0))
if __name__ == "__main__":
print("start computing")
sum_test = Sum_test()
sum_test.compute()
print("finish computing")