[PYTHON MATRIX-SIG] floating point equalities

Hinsen Konrad hinsenk@ere.umontreal.ca
Tue, 23 Jan 1996 14:05:49 -0500

   I'm now working on improving the floating point equality operator.  My
   understanding is that doing a == b is unreasonable for floating point
   numbers, and instead the test should be something like 

   abs(a-b) < TOLERANCE*abs(a)

I don't agree. First, there are cases where testing for equality
makes sense (e.g. if you know that the number represents an integer,
or if you want to detect an underflow by comparing to zero).
Second, standard Python floats use normal equality, so arrays
should behave identically.

On the other hand, having a test with tolerance would be very
useful and make code more readable. APL has such a feature
(using a global variable for the tolerance, which for Python
is not such a great idea), which I like a lot. And since
the comparison "operators" for arrays are methods anyway,
it is easy to have both, with an optional second argument
for the tolerance. I.e. you would write
for a normal equality test and
   a.equal(b, 1e-6)
for a test with a given tolerance. As for the form of a test with
tolerance, I am not sure whether there is a generally accepted best
one. I have seen (and used) the form you have given above, but
also the more symmetric form
   abs(a-b) < TOLERANCE*(abs(a)+abs(b))
(although I wonder whether this is ever necessary). Hopefully
the mathematicians among us can say something more definite.

And now for something completely different...

While rereading the discussion about the beta release, I noticed that
there was no mention at all of how to deal with functions of finite
rank. Although there are none in the current package, they will
appear in the application-specific packages to be written later
(e.g. linear algebra), so there should be a general mechanism for
dealing with them.

Konrad Hinsen                     | E-Mail: hinsenk@ere.umontreal.ca
Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. Centre-Ville     | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org