0

I am not looking for a fix of my code, I want to understand what mechanism causes my list periodically be populated with incorrect data.

x = []
h = 0.01

for i in range(101):
    x.append(1+i*h)

print x

"""
~$ python ~/dev/python/inf1100/coor1.py 
[1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.1400000000000001, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27, 1.28, 1.29, 1.3, 1.31, 1.32, 1.33, 1.34, 1.35, 1.3599999999999999, 1.37, 1.38, 1.3900000000000001, 1.4, 1.4100000000000001, 1.42, 1.43, 1.44, 1.45, 1.46, 1.47, 1.48, 1.49, 1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.56, 1.57, 1.58, 1.5899999999999999, 1.6, 1.6099999999999999, 1.62, 1.63, 1.6400000000000001, 1.65, 1.6600000000000001, 1.67, 1.6800000000000002, 1.69, 1.7000000000000002, 1.71, 1.72, 1.73, 1.74, 1.75, 1.76, 1.77, 1.78, 1.79, 1.8, 1.81, 1.82, 1.83, 1.8399999999999999, 1.85, 1.8599999999999999, 1.87, 1.88, 1.8900000000000001, 1.9, 1.9100000000000001, 1.92, 1.9300000000000002, 1.94, 1.9500000000000002, 1.96, 1.97, 1.98, 1.99, 2.0]
"""

When the list of numbers printed is inspected, the observer notices that the number 1.14 which was expected to appear in the list is not there, instead the float 1.1400000000000001 is found.

This happens multiple times throughout the list.

What is causing this to happen?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mikkel Rev
  • 863
  • 3
  • 12
  • 31

1 Answers1

5

This is related to floating point number representation. There is a maximum precision, or number of bit available to represent the number, so some things can't be shown exactly. The right numbers were put in.

The number 1.1400000000000001 is the closest possible representation of 1.14 using the standard python float.

joeButler
  • 1,643
  • 1
  • 20
  • 41