What Is the Maximum Float in Python

What is the range of values a float can have in Python?

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

How to return the highest float value in a dictionary?

You can just calculate the maximum value and then use a list comprehension:

d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}

maxval = max(d.values())
res = [(k, v) for k, v in d.items() if v == maxval]

[(3, 0.08666666666666667), (4, 0.08666666666666667)]

Python: Usable Max and Min values

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

Maximum decimal place in python

python [...] how many significant digits can a floating point number contain

767 is the most (when it uses double-precision, which I think it does for pretty much everyone).

One such number is (253 - 1) / 21074.

Demo:

>>> len(('%.2000f' % ((2**53 - 1) / 2**1074)).strip('0.'))
767

Got it from this article which goes into more detail.

The number in its full beauty (broken into lines for readability:

>>> print('\n'.join(textwrap.wrap(('%.2000f' % ((2**53 - 1) / 2**1074)).rstrip('0'))))
0.00000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000044501477170144022721148195934182639518696
3909270329129604685221944964444404215389103305904781627017582829831782
6079242213740172877389189291055314414815641243486759976282126534658507
1045737627442980259622449029037796981144446145705102663115100318287949
5279596682360399864792509657803421416370138126133331198987655154514403
1526125381326665295130600018491776632866075559583739224098994780755659
4098101021612198814605258742579179000071675999344145086087205681577915
4359230189103349648694206140521828924314457976051636509036065141403772
1744226256159024466852576737244643007551333245007965068671949137768847
8005309963967709758965844137894433796621993967316936280457084866613206
7970177289160800206986794085513437288676754097207572324554347709124613
17493580281734466552734375

For a given precision, what is the maximum value for which a float32 will give the same result as a float64?

Assuming IEEE 754 representation, float32 has a 24-bit significand precision, while float64 has a 53-bit significand precision (except for “denormal” numbers).

In order to represent a number with an absolute error of at most 0.001, you need at least 9 bits to the right of the binary point, which means the numbers are rounded off to the nearest multiple of 1/512, thus having a maximum representation error of 1/1024 = 0.0009765625 < 0.001.

With 24 significant bits in total, and 9 to the right of the binary point, that leaves 15 bits to the left of the binary point, which can represent all integers less than 215 = 32768, as you have experimentally determined.

However, there are some numbers higher than this threshold that still have an error less than 0.001. As Eric Postpischil pointed out in his comment, all float64 values between 32768.0 and 32768.001 (the largest being exactly 32768+137438953/237), which the float32 conversion rounds down to exactly 32768.0, meet your accuracy requirement. And of course, any number that happens to be exactly representable in a float32 will have no representation error.

Finding the highest and lowest float values entered by user

You just need list to store the inputs. See below code

print("Input some floats. Input 0 to exit.")

count = 0
sum1 = 0.0
number = 1.0
list_n =[]

while number != 0:
number = float(input(""))
sum1 = sum1 + number
if number != 0 :
list_n.append(number)
count += 1

if count == 0:
print("Input some numbers")
else:
print("Average:", sum1 / (count-1))
print("Sum:", sum1)
print("Numbers entered:", count)
print("Max: ", max(list_n))
print("Min: ", min(list_n))

output:

Input some floats. Input 0 to exit.
4
5
8
1
0
Average: 4.5
Sum: 18.0
Numbers entered: 5
Max: 8.0
Min: 1.0

Python: Return max float instead of infs?

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


Related Topics



Leave a reply



Submit