unittest: Proposal to add failUnlessNear

Antoon Pardon apardon at forel.vub.ac.be
Tue Jul 20 06:28:03 EDT 2004


Op 2004-07-20, Roy Smith schreef <roy at panix.com>:
> In article <slrncfpk65.m9.apardon at trout.vub.ac.be>,
>  Antoon Pardon <apardon at forel.vub.ac.be> wrote:
>
>> Only if you want them to be. Floats are a subset
>> of complex and as such can be treated exactly
>> the same.
>
> Why do you say floats are a subset of complex? 

because if x is a float it is equal to x + 0j.

So whatever algorithm you use to see whether
two complex numbers a and b are near enough,
you can use that same algorithm to see whether
two floats f and g are near enough by using
f + 0j and g + 0j for a and b.

more specifically you can use the test:

    abs(a - b) < tolerance.

for complex numbers as well as floats, and
for every numberlike object you wish to
define that has a __sub__ and an __abs__
method.

> There's a reasonable 
> argument that could be made that int/float and scalar/complex are 
> orthogonal properties.  No reason why you shouldn't be able to have a 
> set of numbers which are 2-tuples with real and imaginary parts, each of 
> which has an integral value.  The set would, I believe, be closed for 
> the same operations that integers are (addition, subtraction, and 
> multiplication).

Indeed no reason, but you wouldn't call them complex and I would
call the integers a subset of these defined numbers for the same
reason as I called the floats a subset of complex.

-- 
Antoon Pardon



More information about the Python-list mailing list