python search float return wrong because of precision problem?

Viewed 109

I want to find a float in a arry like this:

arr = np.asarray([1351.1 , 1351.11, 1351.14, 1351.16, 1351.17])
index = np.searchsorted(arr, 1351.14, side="right") - 1 # return 2

But I find that it return wrong like this:

index = np.searchsorted(arr, 1351.1 + 0.04, side="right") - 1 # return 1

Because I want to search value like this:

indexes = np.searchsorted(arr, arr[0] + np.arange(10) * 0.01, side="right") - 1  # this will be wrong because of the problem above
2 Answers

Pretty sure this is due to the representation error.

In my attempts, as 1351.14 * 10000 == 13511400.000000002 and (1351.1 + 0.04) * 10000 == 13511399.999999998, 1351.14 != 1351.1 + 0.04. You can see some extra information on this in Python in this stackoverflow question.

For a quick fix I tried to replace 1351.1 + 0.04 with round(1351.1 + 0.04, 2) and it seem to work (return 2), although I am not sure this is the best method.

As @dm2 suggested, forcing the precision on the second expression can be a solution. Forcing the values to float32 seems to work. But I ask someone with a better understanding of arithmetic operations in numpy to give a detailed answer.

import numpy as np

arr = np.asarray([1351.1 , 1351.11, 1351.14, 1351.16, 1351.17],dtype=np.float32)
index = np.searchsorted(arr, np.float32(1351.1 + 0.04), side="right") - 1 
print(index)
Related