[Python-ideas] Fwd: Way to check for floating point "closeness"?

Ron Adam ron3200 at gmail.com
Sun Jan 18 20:27:40 CET 2015

On 01/17/2015 11:37 PM, Chris Barker wrote:
>        (Someone claimed that 'nothing is close to zero'.  This is
>     nonsensical both in applied math and everyday life.)
> I'm pretty sure someone (more than one of use) asserted that "nothing is
> *relatively* close to zero -- very different.

Yes, that is the case.

> And I really wanted a way to have a default behavior that would do a
> reasonable transition to an absolute tolerance near zero, but I no longer
> thing that's possible. (numpy's implimentaion kind of does that, but it is
> really wrong for small numbers, and if you made the default min_tolerance
> the smallest possible representable number, it really wouldn't be useful.

I'm going to try to summarise what I got out of this discussion.  Maybe it 
will help bring some focus to the topic.

I think there are two case's to consider.

      # The most common case.
      rel_is_good(actual, expected, delta)   # value +- %delta.

      # Testing for possible equivalence?
      rel_is_close(value1, value2, delta)    # %delta close to each other.

I don't think they are quite the same thing.

      rel_is_good(9, 10, .1) --> True
      rel_is_good(10, 9, .1) --> False

      rel_is_close(9, 10, .1) --> True
      rel_is_close(10, 9, .1) --> True

In the "is close" case, it shouldn't matter what order the arguments are 
given. The delta is the distance from the larger number the smaller number 
is.  (of the same sign)

So when calculating the relative error from two values, you want it to be 
consistent with the rel_is_close function.

      rel_is_close(a, b, delta) <---> rel_err(a, b) <= delta

And you should not use the rel_err function in the rel_is_good function.

The next issue is, where does the numeric accuracy of the data, significant 
digits, and the languages accuracy (ULPs), come into the picture.

My intuition.. I need to test the idea to make a firmer claim.. is that in 
the case of is_good, you want to exclude the uncertain parts, but with 
is_close, you want to include the uncertain parts.

Two values "are close" if you can't tell one from the other with certainty. 
  The is_close range includes any uncertainty.

A value is good if it's within a range with certainty.  And this excludes 
any uncertainty.

This is where taking in consideration of an absolute delta comes in. The 
minimum range for both is the uncertainty of the data. But is_close and 
is_good do different things with it.

Of course all of this only applies if you agree with these definitions of 
is_close, and is_good. ;)


More information about the Python-ideas mailing list