# [Tutor] Complex numbers can't be ordered algebraically

Lloyd Kvam pythonTutor at venix.com
Tue Apr 20 20:54:50 EDT 2004

```Obviously, I was not terribly clear.  I DID mean to point out that
complex numbers were NOT ordered.  I was trying to offer the example of
complex numbers as keys or values in a dictionary and not having a
natural default ordering.

Thanks for taking the time to make sure there was no misunderstanding.

On Tue, 2004-04-20 at 19:17, Danny Yoo wrote:
> On 19 Apr 2004, Lloyd Kvam wrote:
>
> > The ordering of keys and values retrieved from a dictionary is
> > undefined.  There is no requirement that keys or values support an
> > ordering.  For instance, they could be complex numbers.
>
>
> Hi Lloyd,
>
>
> I just wanted to raise awareness of the fact that the complex numbers are
> not "ordered" from a mathematical perspective.  That is, they can't be
> compared by using the less-than or greater-than operators that follow the
> math definition of "order":
>
>     http://en.wikipedia.org/wiki/Ordered_field
>
>
> ###
> >>> x = 3+4j
> >>> y = 4+5j
> >>> x < y
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: cannot compare complex numbers using <, <=, >, >=
> ###
>
>
> In this, Python follows what mathematicians have defined.  But why?  This
> might seem weird --- why shouldn't y be 'greater' than x?  All the numbers
> are bigger!
>
>
> But there's a strong math basis for the restriction.  For an "ordered
> field", the square of any number 'x' is nonnegative:
>
>     x**2 >= 0
>
> This is a combination of "Positive times positive is positive" / "negative
> times negative is positive" rules from elementary school.  *grin*
>
>
> Unfortunately, complex numbers violate this law:
>
> ###
> >>> (-1j) * (-1j)
> (-1-0j)
> ###
>
>
> The conclusion we can make from this is that complex numbers aren't
> compatible with the concept of algebraic order.  So that's why the complex
> numbers are excluded from being compared by '<', '<=', '>' or '>='.
>
>
> Hope this helps!
--

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582

```