1

I found how to compare floats with equality here:

Compare `float` and `float64` in python

Working with floating point NumPy arrays for comparison and related operations

and in other similar questions.

But I can't find the best way how to compare correctly floats with threshold(greater or less).

Example: We want to check if elements in float matrix is less than float threshold.

eps = 0.1
xx = np.array([[1,2,3], [4,5,6], [7,8,9]])
yy = np.array([[1.1,2.1,3.1], [4.1,5.1,6.1], [7.1,8.2,9.3]])
dif = np.absolute(xx - yy)
print dif
print dif < eps

Print:

[[ 0.1  0.1  0.1]
 [ 0.1  0.1  0.1]
 [ 0.1  0.2  0.3]]
[[False False False]
 [ True  True  True]
 [ True False False]]

The only solution I found is to create a vectorize function and compare every element of matrix with treshold: first determine they're not equal and then compare with < or >. Thanks for @MarkRansom.

Community
  • 1
  • 1
Gusev Slava
  • 2,136
  • 3
  • 21
  • 26
  • Please be a little more clear. Do you mean you have one float value, say `threshold`, and you want to see if another value `x` is greater than that (or perhaps less than that)? What is wrong with `if x > threshold`? – Rory Daulton Mar 11 '17 at 16:51
  • Is http://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python what you're looking for? – Mark Ransom Mar 11 '17 at 17:03
  • @MarkRansom thanks. But in this answer I found only how to compare floats for equality but not for greater or less. Maybe I'm not attentive enough. – Gusev Slava Mar 11 '17 at 17:05
  • @RoryDaulton I think because of floating point. – Gusev Slava Mar 11 '17 at 17:07
  • 1
    Once you've determined they're *not* equal, what's wrong with `<` and `>`? – Mark Ransom Mar 11 '17 at 17:09
  • @MarkRansom Yes, it's a good way) Thaks! But I hope to find simpler way. Using one super command for example) – Gusev Slava Mar 11 '17 at 17:13
  • @MarkRansom What about such sutiation: We want to compare elements in matrix with threshold. I add example. First we check them with equality and get `False`. Then we try to check less and we get incorrect answers because of `0.1 < 0.1` in the second row get us `True`. – Gusev Slava Mar 11 '17 at 17:27
  • 1
    The idea is to check for equality first, and only use `<` or `>` if they're *not* equal. So you'd never be comparing `0.1 < 0.1`. – Mark Ransom Mar 11 '17 at 18:01

2 Answers2

0

In most practical circumstances an exact comparison will be not be possible because of the little errors you collect while doing calculations.

If you want to do proper numerics you'll have to carry an error estimate along with all your results which is quite tedious.

(There is a library called flint with a python interface but I haven't used it so cannot vouch for it. It is designed to do the carrying error bounds (more rigorous than estimates) along all results for you.)

In any case you will have to change the list of possible outcomes from greater, equal, less to something more like greater, probably greater, indistinguishable, probably less, less

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0

Keep in mind that a ≤ b is equal to not a > b.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94