One way to reliably get the limits of floating point formats is to construct the values from their binary representation with struct.
import struct
FLOAT_MAX = struct.unpack('>f', b'\x7f\x7f\xff\xff')
FLOAT_MIN = struct.unpack('>f', b'\xff\x7f\xff\xff')
MAX_DOUBLE = struct.unpack('>d', b'\x7f\xef\xff\xff\xff\xff\xff\xff')
MIN_DOUBLE = struct.unpack('>d', b'\xff\xef\xff\xff\xff\xff\xff\xff')
Answer from javidcf on Stack OverflowOne way to reliably get the limits of floating point formats is to construct the values from their binary representation with struct.
import struct
FLOAT_MAX = struct.unpack('>f', b'\x7f\x7f\xff\xff')
FLOAT_MIN = struct.unpack('>f', b'\xff\x7f\xff\xff')
MAX_DOUBLE = struct.unpack('>d', b'\x7f\xef\xff\xff\xff\xff\xff\xff')
MIN_DOUBLE = struct.unpack('>d', b'\xff\xef\xff\xff\xff\xff\xff\xff')
Found a solution like below after referring this
DOUBLE_MAX = 1.7976931348623158E+308 # 64 bit floating point max value
DOUBLE_MIN = 2.2250738585072014E-308 # 64 bit floating point max value
def is_double(value):
return DOUBLE_MIN <= value <= DOUBLE_MAX
FLOAT_MAX = 3.402823466E+38 # 32 bit floating point max value
FLOAT_MIN = 1.175494351E-38 # 32 bit floating point max value
def is_float(value):
return FLOAT_MIN <= value <= FLOAT_MAX
is_float(123e20)
is_double(123e20)
is_float(123e203)
is_double(123e203)
is_float(123e2030)
is_double(123e2030)
floating point - Python: Return max float instead of infs? - Stack Overflow
Python: Usable Max and Min values - Stack Overflow
how to get the max value if that is a float?
floating point - What is the range of values a float can have in Python? - Stack Overflow
You can use a decorator:
import sys
def noInf(f):
def wrapped(*args, **kwargs):
res = f(*args, **kwargs)
if res == float('inf'):
return sys.float_info.max
return res
return wrapped
@noInf
def myMult(x, y):
return x*y
print(myMult(sys.float_info.max, 2)) # prints 1.79769313486e+308
I don't know about 'replacing' infinity value, but what about using min()?
Something like (assuming x yields your value):
return min ( [ x, sys.float_info.max ] )
For numerical comparisons, +- float("inf") should work.
It doesn't always work (but covers the realistic cases):
print(list(sorted([float("nan"), float("inf"), float("-inf"), float("nan"), float("nan")])))
# NaNs sort above and below +-Inf
# However, sorting a container with NaNs makes little sense, so not a real issue.
To have objects that compare as higher or lower to any other arbitrary objects (including inf, but excluding other cheaters like below), you can create classes that state their max/min-ness in their special methods for comparisons:
class _max:
def __lt__(self, other): return False
def __gt__(self, other): return True
class _min:
def __lt__(self, other): return True
def __gt__(self, other): return False
MAX, MIN = _max(), _min()
print(list(sorted([float("nan"), MAX, float('inf'), MIN, float('-inf'), 0,float("nan")])))
# [<__main__._min object at 0xb756298c>, nan, -inf, 0, inf, nan, <__main__._max object at 0xb756296c>]
Of course, it takes more effort to cover the 'or equal' variants. And it will not solve the general problem of being unable to sort a list containing Nones and ints, but that too should be possible with a little wrapping and/or decorate-sort-undecorate magic (e.g. sorting a list of tuples of (typename, value)).
You have the most obvious choices in your question already: float('-inf') and float('inf').
Also, note that None being less than everything and the empty tuple being higher than everything wasn't ever guaranteed in Py2, and, eg, Jython and PyPy are perfectly entitled to use a different ordering if they feel like it. All that is guaranteed is consistency within one running copy of the interpreter - the actual order is arbitrary.
Hi, I have calculated some values and now need the program to print which is the largest. The problem is that these numbers are floats and I get an error: TypeError: 'float' object is not iterable when using the max function. And I can't change it to int for example, because I need to include numbers after the comma. I would be really grateful if someone could help me.
>>> import sys
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,
min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)
The smallest is sys.float_info.min (2.2250738585072014e-308) and the biggest is sys.float_info.max (1.7976931348623157e+308). See documentation for other properties.
sys.float_info.min is the normalized min. You can usually get the denormalized min as sys.float_info.min * sys.float_info.epsilon. Note that such numbers are represented with a loss of precision. As expected, the denormalized min is less than the normalized min.
See this post.
Relevant parts of the post:
In [2]: import kinds In [3]: kinds.default_float_kind.M kinds.default_float_kind.MAX kinds.default_float_kind.MIN kinds.default_float_kind.MAX_10_EXP kinds.default_float_kind.MIN_10_EXP kinds.default_float_kind.MAX_EXP kinds.default_float_kind.MIN_EXP In [3]: kinds.default_float_kind.MIN Out[3]: 2.2250738585072014e-308
Python 3
In Python 3, this question doesn't apply. The plain int type is unbounded.
However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.
Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2). See this answer for more information.
Python 2
In Python 2, the maximum value for plain int values is available as sys.maxint:
>>> sys.maxint # on my system, 2**63-1
9223372036854775807
You can calculate the minimum value with -sys.maxint - 1 as shown in the docs.
Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
If you just need a number that's bigger than all others, you can use
float('inf')
in similar fashion, a number smaller than all others:
float('-inf')
This works in both python 2 and 3.