7

Specs: Python 3.3.1

What I was trying to do: "Using Python, determine the largest and smallest ints, longs, floats, and complex numbers that your system can handle."

What I did: I went through Python's math modules and all built-in functions relating to math and numbers, but couldn't find a way to do this. I also tried something like max(range(0,)) but it returned ValueError: max() arg is an empty sequence error.

Question: How to determine largest/smallest int/long/float/complex numbers my system can handle using Python? As a total beginner, I know I must have missed something, but I tried and wasn't able to figure it out. I appreciate your help!

hakuna121
  • 243
  • 5
  • 10
  • 18
  • There is no largest or smallest integer in Python 3, since arbitrary precision is used. Complex numbers are not fully ordered, so there is no largest or smallest number. (As an example, 1 is neither less than, equal to, nor greater than *i*.) – chepner Jun 08 '13 at 14:51
  • 1
    Thanks for point the other thread out! Well since it didn't answer the "long, floats, complex numbers" part of my question (or did it? did I missed something?), I might as well leave this thread here so if anyone asking about long/float/complex might need it? – hakuna121 Jun 08 '13 at 15:10
  • @chepner thanks for taking the time! Forgive me for this but I don't fully understand about `arbitrary precision` you mentioned. Does that mean it depends on the box's memory size? And if so, is there a way to see the result in the interpreter? Thanks! – hakuna121 Jun 08 '13 at 15:13
  • Correct. There is no maximum size from Python's point of view. There would simply be a OutOfMemory error at some point. – chepner Jun 08 '13 at 15:20
  • @hakuna121 Thanks for leaving this open. I was one of those people looking for that information (and you're right, the question this was marked as a duplicate of doesn't really address that point). – Knowledge Cube Jun 13 '17 at 20:00

2 Answers2

13

The python numeric limitations, such as there are any, are available on the sys module:

  • sys.float_info is a named tuple with floating point limitations for your platform. Floating point numbers consist of a exponent and a precision; you'd have to be more precise about what you mean by the largest number here; the number with the largest exponent and the full precision in use is sys.float_info.max.

  • sys.int_info; not so much limitations as the implementation detail; you should be able to estimate the largest integer possible from this. Python integers are only limited by your available memory.

  • sys.maxsize; the platform word size and limit to lists and tuples and the likes.

So for integers, there basically is a soft limit to the maximum and minimum values. It depends on how much memory your process can use, and how much memory your process is already using for other things.

In Python 3, there no longer is a separate long type, but in Python 2, sys.maxsize + 1 would have to be a long, as would -sys.maxsize - 2. Between those two extremes lies the range of possible 'short' integers.

For complex numbers, ordering is a little more.... complex anyway. Complex numbers have a real and imaginary component, both are floats. Guess what? These are python floats and you already have their limit info above:

>>> type(1j)
<type 'complex'>
>>> type(1j.real)
<type 'float'>
>>> type(1j.imag)
<type 'float'>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Very well said. Thank you very much! – hakuna121 Jun 08 '13 at 15:17
  • I use Python 2.7.3, and there is no `sys.int_info`, but `sys.long_info` – Suzana Oct 15 '13 at 22:30
  • Correct; the question was tagged with `python-3.x` so I focused on that version. Python 3 removed the distinction between `long` and `int` altogether. Since the info documents how Python 2 `long` is handled the 2.x `sys` attribute name naturally reflects that. – Martijn Pieters Oct 15 '13 at 22:55
4

sys.float_info provides the desired information for floating-point values.

>>> sys.float_info.max
1.7976931348623157e+308

Python 3 does not have upper or lower limits on integers, and there exists no mathematical definition for ordering arbitrary complex numbers (although the real or imaginary parts of two complex numbers can be ordered separately).

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks for the answer! Though on my system with Python 3.3.1 it is `sys.float_info`. But the fact that you pointed this whole path out is very helpful. Thank you! – hakuna121 Jun 08 '13 at 15:06
  • Fixed. I got confused, since `sys.float_info` is an instance of the `sys.floatinfo` class. – chepner Jun 08 '13 at 15:17
  • I don't quite understand what you mean yet. But if it's because I missed something, I sincerely apologize, total beginner to Python! – hakuna121 Jun 08 '13 at 15:21
  • Try `type(sys.float_info)`. The name of the class and the name of the instance of that class are very similar. – chepner Jun 08 '13 at 15:37