0
for i in np.arange(0.0,1.1,0.1):
    print(i)

Output:

0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
1.0

Expected output:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • Refer this to understand why it's happening [Floating point issues in Python](https://docs.python.org/3/tutorial/floatingpoint.html) – Sociopath Sep 10 '20 at 06:51
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – FBruzzesi Sep 10 '20 at 06:58

1 Answers1

0

It's not incrementing by the wrong step size, those are just floating point errors. From here:

This can be considered as a bug in Python, but it is not. This has little to do with Python, and much more to do with how the underlying platform handles floating-point numbers. It’s a normal case encountered when handling floating-point numbers internally in a system. It’s a problem caused when the internal representation of floating-point numbers, which uses a fixed number of binary digits to represent a decimal number. It is difficult to represent some decimal number in binary, so in many cases, it leads to small roundoff errors.

qmeeus
  • 2,341
  • 2
  • 12
  • 21