unittest: Proposal to add failUnlessNear

Antoon Pardon apardon at vub.ac.be
Thu Jul 22 20:12:11 CEST 2004

John Roth <newsgroups at jhrothjr.com> wrote:
> "Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message
> news:slrncfpk65.m9.apardon at trout.vub.ac.be...
> > Op 2004-07-19, John Roth schreef <newsgroups at jhrothjr.com>:
> > >
> > > An example may help. Let's say I want to
> > > test against '6.23e14'. This method would
> > > establish bounds of 6.235e14 and 6.225e14
> > > and then test whether the other number was
> > > within those bounds. (Note that the '5' is
> > > appended before the numbers are converted
> > > to float, however the second bound is calculated
> > > after the conversion.)
> >
> > Why don't you just test whether the second
> > number lies within a fault tolerance of
> > 5e11 of 6.23e14

> For one blatently obvious reason. It's harder
> to calculate that in the character form than it
> is to insert the '5', and you don't have the
> information to tell what you wanted if you
> convert from character to float first.

And by making it easier here you make it more
difficult for yourself later on because now
you need two different ways to see whether
two numbers are near enough depending on
whether you are working with floats or complex

> My objective here is to ***not have to***
> separately enter a precision. The semantics
> of saying I want something to be treated as equal
> if it's "close enough" to 6.23e14 is that it should
> be closer to that than to either 6.24e14 or
> 6.22e14.

But you don't follow this same procedure with complex
numbers. If you are working with complex numbers you
are prepared to check whether a number is within a
certain distance from another number. if you have

6.23e14 + 3.49e11j you can work with the same procedure
and test that your second number is within the rectangle
that lies between 6.235e14 and 6.225e14 on the real
axis and between 3.485j and 3.495j on the imaganary axis.

Antoon Pardon

More information about the Python-list mailing list