Understanding sys.float_info for maximum floats in python

Viewed 1859

I'm trying to understand the information given by sys.float_info to understand what the maximum floats in Python are. On my computer, this gives me the following:

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

The docs give the following example (https://docs.python.org/3/library/sys.html#sys.float_info):

>>> import sys
>>> sys.float_info.dig
15
>>> s = '3.14159265358979'    # decimal string with 15 significant digits
>>> format(float(s), '.15g')  # convert to float and back -> same value
'3.14159265358979'
>>> s = '9876543211234567'    # 16 significant digits is too many!
>>> format(float(s), '.16g')  # conversion changes value
'9876543211234568'

However, the following example works just fine for me, even though it also has 16 significant digits (?):

>>> s = '.9876543211234567' 
>>> format(float(s), '.16g')
'0.9876543211234567'

Also, sys.float_info.min yields 2.2250738585072014e-308 which is is obviously a lot smaller and also has 17 significant digits, if I'm correct? How does that work when sys.float_info.dig = 15? Am I confusing something here?

How does sys.float_info.dig = 15 relate to the attributes sys.float_info.mant_dig and sys.float_info.radix? As far as I understand, if I were to represent some decimal number as a base-2 number (since sys.float_info.radix = 2), does sys.float_info.mant_dig then give me the maximum integer of the mantissa?

Sorry if I'm confusing some things, maybe I'm not firm enough the mathematical basics here. Any help is much appreciated!

2 Answers

However, the following example works just fine for me, even though it also has 16 significant digits (?):

The documentation says “If X, then Y,” where X is:

  • A decimal numeral with up to 15 significant digits is converted to your Python implementation’s float and back to the nearest decimal numeral with at most 15 significant digits.

and Y is:

  • The number represented by the original numeral equals the resulting numeral.

That says only what happens if X is true. It does not say what happens if X is false. You have given us a situation where Y is true (you got back the original number) and complained that X is false. That is consistent with the documentation.

Also, sys.float_info.min yields 2.2250738585072014e-308 which is is obviously a lot smaller and also has 17 significant digits, if I'm correct? How does that work when sys.float_info.dig = 15? Am I confusing something here?

Every floating-point datum other than a NaN (Not a Number) represents one number exactly. It is exactly that number, regardless of how many decimal digits are required to express that number. sys_float_info.dig tells you a property about how fine the floating-point representation is. It tells you the floating-point representation is so fine, meaning that the numbers it does represent are so close together, that they can be used to distinguish between 15-digit decimal numerals. That just tells you how close the representable numbers are to each other. It does not tell you how many decimal digits it takes to exactly express any of those numbers.

In fact, the smallest positive number representable in your Python implementation’s floating-point format is 2−1074, which is 4.940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625•10−324.

You can learn more about the specifics of floats for your runtime environment, by checking the value of sys.float_info. This will also tell you what's the largest and smallest number that can be represented with them.

Related