Floating point problem
Pieter van Oostrum
pieter-l at vanoostrum.org
Sun Apr 19 07:49:03 EDT 2020
"R.Wieser" <address at not.available> writes:
> Souvik,
>
>> I have one question here. On using print(f"{c:.32f}") where c= 2/5
>> instead of getting 32 zeroes I got some random numbers. The exact
>> thing is 0.40000000000000002220446049250313 Why do I get
>> this and not 32 zeroes?
>
> Simple answer ? The conversion routine runs outof things to say.
>
> A bit more elaborate answer ? You should not even have gotten that many
> zeroes after the 0.4. The precision of a 32-bit float is about 7 digits.
> That means that all you can depend on is the 0.4 followed by 6 more digits.
> Anything further is, in effect, up for grabs.
>
Most Python implementations use 64-bit doubles (53 bits of precision). See https://docs.python.org/3.8/tutorial/floatingpoint.html
--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
More information about the Python-list
mailing list