# unittest: Proposal to add failUnlessNear

Tim Peters tim.peters at gmail.com
Thu Jul 22 00:17:07 CEST 2004

```[John Roth]
>>> In addition, you're going to have to do significant
>>> type checking if you want to combine floats and
>>> complex in the same method call. That's just
>>> poor design.

[Christopher T King]
>> Why would you have to do that?  The Python operators, as well as every
>> function in cmath, transparently support both floats and complex numbers
>> (as they should).  Every operation between floats and complex numbers is
>> well-defined.

[John]
> Because the "distance" between two floats is
>
> abs(a - b)
>
> while the "distance" between two complex numbers
>
> is something like
>
>        math.sqrt(diff.real ** 2 + diff.imag ** 2)
>
> This requires a type check to differentiate which
> formula you need to use.

Nope.  abs(a - b) works for both cases, although for complex numbers
Python uses a numerical method less prone to spurious
overflow/underflow than the "square root of the sum of the squares"
formula.

>>> abs((12+29j) - (9+25j))
5.0
>>>

which = sqrt((12-9)**2 + (29-25)**2) = sqrt(3**2 + 4**2) = sqrt(9 +
16) = sqrt(25).

Note that if the difference in the imaginary components is 0, the
formula reduces to

math.sqrt(diff.real**2)

which = |diff.real|.  IOW, the definition of real abs() is a special
case of the definition of complex abs().

```