0

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]]
3
  • 2
    There is no issue here. Objects in a list are represented by their repr() output. Your float object has that exact value, but the str() conversion limits the number of digits that are printed. Commented Nov 3, 2016 at 9:23
  • 1
    @ Martijin Pieters Why some results appear the extra number of points and some are not? If it is not a issue, all the result should print out the same number of points. Commented Nov 3, 2016 at 9:33
  • 1
    Floating point numbers use binary fractions to approximate values. Not all values can be represented exactly using binary fractions. You have some such numbers, so there is a very, very small deviation to make it fit. Commented Nov 3, 2016 at 9:36

1 Answer 1

2

If you can decide on how much precision you require, you could could always use something like this.

coupon.append(float(format(x,'.6f')))
coupon.append(float(format(y,'.6f')))

Which in this case gives 6 digits after the decimal point.
e.g.

>>> a=30.504036000000003
>>> print a
30.504036
>>> repr(a)
'30.504036000000003'
>>> coupon=[]
>>> coupon.append(a)
>>> coupon
[30.504036000000003]
>>> coupon.append(float(format(a,'.6f')))
>>> coupon
[30.504036000000003, 30.504036]
>>> coupon[0]+coupon[1]
61.008072
>>> type(coupon[0])
<type 'float'>
>>> type(coupon[1])
<type 'float'>

Edit: or as Mark pointed out in his comment you could instead use:

coupon.append(round(x,6))
coupon.append(round(y,6))

and get the same result.

Sign up to request clarification or add additional context in comments.

2 Comments

float(format(a, '.6f')) is a rather complicated way of saying round(a, 6).
@MarkDickinson coupon.append(round(a,6)) Your right! Checked it and you get a float stored in coupon. I didn't think of it but that's the beauty of python, there's always more than one way to skin a cat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.