[PYTHON MATRIX-SIG] floating point equalities

James Hugunin jjh@Goldilocks.LCS.MIT.EDU
Tue, 23 Jan 96 13:48:21 EST

   From: Guido van Rossum <guido@CNRI.Reston.VA.US>

   > 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)

   Hm.  If you put this in the array module it would be a reasonable
   expectation that it would also work for ordinary Python floats, which
   it doesn't.  Also, the tolerance may be application dependent or
   dependent on the part of the application that's doing this, so it may
   need to be specified in the operation.

   I'd advise to continue using C's '==' operator for floats (which is
   what ordinary Python floats do).  Users who know enough to write
   numerical code should also know not to compare for equality.

   (What you could do is provide a method or function for comparisons
   where you can pass the tolerance in as an argument.)

I'm still curious to see what the more numerically inclined folks have
to say on this, but you're obviously right that this should be
provided as an additional function, not the default comparision

I'd like to have a method approxEqual (or some similar name) so that
a.approxEqual(b) will "do the right thing".  I had believed that there
was some sort of consensus in the numerical programming community on
what the right thing is for floating point numbers, if I'm wrong on
this, I'd like to know that too.

Note: part of why this came up is the fact that on my development
system (1j)**2 == (-1+1.22460635382e-16j).  I'd really like to be able
to have some way to say approxEqual((1j)**2, -1) and have it come out
true (OK, I have an ad hoc solution that works, what I want is the
"right" solution if there is one out there).


MATRIX-SIG  - SIG on Matrix Math for Python

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