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?
Finding max value in a series of floats
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.