I am writing a piece of test code that add two float point int each other. I can get accuracy result,but the extra number of points appear when I insert the result in a python list, I have no idea what lead to this consequence. Please give me a hint!
My code:
center = [120.688281,30.500036]
coupon_list = []
for i in range(5):
seed_x = random.randint(1,100)
print 'seed_x:'
print seed_x
random.seed(seed_x)
rand_x = random.randrange(100,500,20)/float(100000)
seed_y = random.randint(1,100)
print 'seed_y:'
print seed_y
random.seed(seed_y)
rand_y = random.randrange(100,500,20)/float(100000)
print 'rand_x:'
print rand_x
print 'rand_y:'
print rand_y
print 'float convert:'
x = center[0]+ rand_x
y = center[1] + rand_y
print 'x:'
print x
print 'y:'
print y
coupon = []
coupon.append(x)
coupon.append(y)
print 'coupon:'
print coupon
coupon_list.append(coupon)
print coupon_list
My result:
seed_x:
22
seed_y:
15
rand_x:
0.0048
rand_y:
0.0048
float convert:
x:
120.693081
y:
30.504836
coupon:
[120.693081, 30.504836]
seed_x:
2
seed_y:
95
rand_x:
0.0048
rand_y:
0.004
float convert:
x:
120.693081
y:
30.504036
coupon:
[120.693081, 30.504036000000003]
seed_x:
52
seed_y:
6
rand_x:
0.0048
rand_y:
0.004
float convert:
x:
120.693081
y:
30.504036
coupon:
[120.693081, 30.504036000000003]
seed_x:
83
seed_y:
86
rand_x:
0.0028
rand_y:
0.004
float convert:
x:
120.691081
y:
30.504036
coupon:
[120.691081, 30.504036000000003]
seed_x:
4
seed_y:
11
rand_x:
0.0018
rand_y:
0.0028
float convert:
x:
120.690081
y:
30.502836
coupon:
[120.690081, 30.502836000000002]
[[120.693081, 30.504836], [120.693081, 30.504036000000003], [120.693081, 30.504036000000003], [120.691081, 30.504036000000003], [120.690081, 30.502836000000002]]
repr()output. Your float object has that exact value, but thestr()conversion limits the number of digits that are printed.