Kani
Kani

Reputation: 43

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 ?

Upvotes: 1

Views: 418

Answers (2)

Somya Anchalia
Somya Anchalia

Reputation: 64

In Python, value of an integer is not restricted by the number of bits and can expand to the limit of the available memory.

For float have a look at sys.float_info:

>>> import sys
>>> sys.float_info
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2
250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsil
on=2.2204460492503131e-16, radix=2, rounds=1)

To answer the second part, please find the info in below links:

https://docs.python.org/3/library/fractions.html

https://docs.python.org/2/library/decimal.html

Upvotes: 2

tdelaney
tdelaney

Reputation: 77337

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.

Upvotes: 1

Related Questions