[Numpy-discussion] [RFC] new function for floating point comparison

David Cournapeau david@ar.media.kyoto-u.ac...
Thu Oct 29 02:17:10 CDT 2009


    I have added a couple of utilities for floating point comparison, to
be used in unit tests mostly, and would like some comments, especially
from people knowledgeable about floating point.


The main difference compared to other functions is that they are
'amplitude-independent', and use IEEE-754-specific properties. The
tolerance is based on ULP, and two numbers x, y are closed depending on
how many numbers are representable between x and y at the given
precision. The branch contains the following new functions:

    * spacing(x): equivalent to the F90 intrinsic. Returns the smallest
representable number needed so that spacing(x) + x > x. Spacing(1) is
EPS by definition.
    * assert_array_almost_equal_nulp(x, y, nulp=1): assertion is defined
as abs(x - y) <= nulps * spacing(max(abs(x), abs(y))).
    * assert_array_max_ulp(a, b, maxulp=1, dtype=None): given two
numbers a and b, raise an assertion if there are more than maxulp
representable numbers between a and b.

They only support single and double precision - for complex number, one
could arbitrarily define a distance between numbers based on nulps, say
max of number of representable number for real and imag parts. Extended
precision would be a bit more painful, because of the variety of

I hope that they can give more robust/meaningful comparison for most of
our unit tests,



More information about the NumPy-Discussion mailing list