First of all, I already know that :
3.001
and6.001
don't have an exact binary representation.3.001 <> 6.001
from a "floating point of view".
Now let's consider the following code.
for val in ["3 + 3.001", "6.001"]:
d = eval(val)
print("\nCODE :", val)
print("DEFAULT :", d)
for prec in [16, 20]:
message = "{" + "0:.{0}f".format(prec) + "}"
print(
"{0}: {1}".format(
message,
message.format(d)
)
)
This prints :
CODE : 3 + 3.001
DEFAULT : 6.0009999999999994
{0:.16f}: 6.0009999999999994
{0:.20f}: 6.00099999999999944578
CODE : 6.001
DEFAULT : 6.001
{0:.16f}: 6.0010000000000003
{0:.20f}: 6.00100000000000033396
We can see that 3 + 3.001
seems to be printed by default using 16 digits and rounding. But for 6.001
it seems that at most 15 digits are used before rounding.
Why ?