Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-How to determine largest/smallest int/long/float/complex numbers my system can handle [duplicate]

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!

like image 985
hakuna121 Avatar asked Jun 08 '13 14:06

hakuna121


2 Answers

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'>
like image 105
Martijn Pieters Avatar answered Oct 30 '22 23:10

Martijn Pieters


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).

like image 27
chepner Avatar answered Oct 30 '22 23:10

chepner