3

Try this:

import numpy as np
np.arange(0,3*0.1,0.1)

Output will be: array([ 0. , 0.1, 0.2, 0.3])

This is incredible because for np.arange 'Values are generated within the half-open interval [start, stop)'. I tried other numbers and found only the multiples of 3 would trigger such phenomenon:

np.arange(0,2*0.1,0.1).shape
# 2
np.arange(0,3*0.1,0.1).shape
# 4
np.arange(0,4*0.1,0.1).shape
# 4
np.arange(0,5*0.1,0.1).shape
# 5
np.arange(0,6*0.1,0.1).shape
# 7

I'm so confused now. Can somebody help me?

jpp
  • 159,742
  • 34
  • 281
  • 339
Shiang Hoo
  • 363
  • 1
  • 3
  • 9

2 Answers2

4

The problem is your endpoint: 3 * 0.1, which is not considered equal to 0.3 (remember that Python and NumPy use floating point arithmetic where some numbers, i.e. 0.1, cannot be represented exactly).

>>> 3 * 0.1
0.30000000000000004
>>> 0.3  
0.3  # or more exactly 0.299999999999999988897769753748...
>>> 3 * 0.1 == 0.3
False

So it's not really surprising that 0.3 is included because the endpoint is (very slightly) bigger.

Note that the numpy.arange also contains the formula how many elements will be in the result array:

ceil((stop - start)/step

>>> (3 * 0.1 - 0)/0.1
3.0000000000000004
>>> ceil(_)
4

Floating point math is tricky, especially when comparing floats for equality. Why not just create an integer array and create the desired float array by division:

>>> import numpy as np
>>> np.arange(0, 3, 1) / 10
array([0. , 0.1, 0.2])

Or the numpy.linspace function which offers more options for floating point values:

>>> import numpy as np
>>> np.linspace(0.0, 3 * 0.1, 3, endpoint=False)
array([0. , 0.1, 0.2])
MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

I try to solve this problem by multiplying the floating number like 0.30000000000000004 by a large power of 10, rounding it and dividing it back

for i in np.arange(0,3*0.1,0.1):
    i=np.round(i*10**9)/10**9
    print(i)

The number such as 9 needs to be selected so that it does not lose the necessary accuracy (not too small) and does not add the redundant number at the end (not too large). For example, 2.000001 * 10 ** 5 will lose the number 1 at the end, while 30000.000000000004 * 10 ** 12 will retain the redundant number 4. The numbers 5 and 12 here are not suitable. Here I choose 9 because it is the average of the scale 1 of 0.1 and the scale 17 of 0.30000000000000004.

vector
  • 1
  • 1