[Tutor] largest and smallest numbers

Terry Carroll carroll at tjc.com
Tue Sep 25 03:01:45 CEST 2007


On Mon, 24 Sep 2007, Christopher Spears wrote:

> How can I find the largest float and complex numbers?

That's an interesting question..

I just tried this:

x = 2.0
while True:
    x = x*2
    print x
    if repr(x) == "1.#INF": break

to just keep doubling X until Python began representing it as infinity.  
My output:

4.0
8.0
16.0
32.0
64.0
128.0
 . . .
137438953472.0
274877906944.0
549755813888.0
1.09951162778e+012
2.19902325555e+012
4.3980465111e+012
 . . .
2.24711641858e+307
4.49423283716e+307
8.98846567431e+307
1.#INF

So I'd say, the answer is somewhere between 8.98846567431e+307 and double 
that.

On complex numbers, I'm not so sure.  My math is rusty. Is there a concept
of "greater than" or "largest" in complex numbers on different axis?  
Which is larger, 4+2i or 2+4i?

>>> complex(4,2)
(4+2j)
>>> complex(2,4)
(2+4j)
>>> complex(4,2) > complex(2,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: no ordering relation is defined for complex numbers



More information about the Tutor mailing list