Search code examples
pythonpython-3.xfloating-pointint

Python Int and Float datatype range


Can anyone please clarify whether there is no maximum or minimum limit at all to the int and float datatypes in python 3? Also what is the use of fractions.Fraction and decimal.Decimal ?


Solution

  • There is no limit to int. You can get the limits to float from a system call

    >>> 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.220446049250313e-16, radix=2, rounds=1)
    

    Python uses binary floats and you loose some precision converting between decimal and float. You also loose precision when dealing with fractions that are being estimated by floats. decimal does base 10 math, reducing that conversion problem at the cost of being slower. fractions tries to keep things as fractions to avoid that level of estimation.