I know that Python's float
type is actually a double
. Knowing that, I'm having a hard time understanding the results shown below. My expectations are based on the output of https://babbage.cs.qc.cuny.edu/IEEE-754/ and https://www.h-schmidt.net/FloatConverter/IEEE754.html. What is going on here?
>>> 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)
>>>
>>> decimal_value = 1.23450005054473876953125 # This number is perfectly represented by a 32 bit float. No rounding necessary.
>>> type(decimal_value)
<class 'float'>
>>> decimal_value
1.2345000505447388 # Why did Python truncate this?
>>>
>>> decimal_value = 1.2345000505447389915758549250313080847263 # This number is perfectly represented by a 64 bit double. No rounding necessary.
>>> # The comment on the line above is a lie. That number is not a perfect double. The closest perfect double to that value is 1.2345000505447389915758549250313080847263336181640625
>>> type(decimal_value)
<class 'float'>
>>> decimal_value
1.234500050544739 # Why did Python truncate this, and why isn't this truncated double value the same as the trucanted float value above?