1

I'm working with the ASE library where in one of its functions I'm interesed we have this line of code:

bin_index_ic = np.floor(scaled_positions_ic*nbins_c).astype(int)

The result of print(type(scaled_positions_ic), scaled_positions_ic.size) is: <class 'numpy.ndarray'> 1296 while the same print with nbins_c is <class 'numpy.ndarray'> (3,).

For instance, the index 264 of scaled_positions_ic is array([0.55555567, 0.77777767, 0.5 ] and nbins_c is [2 2 2].

The result, with ASE's code described above, with the index 264 of the array scaled_positions_ic is: array([1, 1, 0])

I obtained a different result with an example I constructed in the Python prompt. Both python and numpy versions are the same since I'm using the same venv. Here is the result of the python prompt:

>>> type(a)
<class 'numpy.ndarray'>
>>> print(a)
[0.55555567 0.77777767 0.5       ]
>>> type(b)
<class 'numpy.ndarray'>
>>> b
array([2, 2, 2])
>>> np.floor(a * b).astype(int)
array([1, 1, 1])

Moreover, I obtain (1, 1, 1) with a C++ version of this code that uses std::floor.

Why do I obtain two different results ?

rbw
  • 187
  • 5
  • 1
    I think that's an issue how floats are stored. This might be related: https://stackoverflow.com/questions/33019698/how-to-properly-round-up-half-float-numbers-in-python – S. Strempfer Jul 14 '20 at 18:30

0 Answers0