Floating point problem

DL Neil PythonList at DancesWithMice.info
Sat Apr 18 09:25:00 EDT 2020


On 19/04/20 1:07 AM, Souvik Dutta wrote:
> 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?

Approximating decimal numbers as binary values.

Do NOT try this at home! How many lines will the following code display 
on-screen?

 >>> v = 0.1
 >>> while v != 1.0:
...     print(v)
...     v += 0.1

As an exercise, try dividing 1.0 by 10.0 and then adding the result to 
itself ten times.

Back in the ?good, old days, a Computer Science course would almost 
certainly involve some "Numerical Analysis", when such issues would be 
discussed. Not sure that many institutions offer such, these days...
-- 
Regards =dn


More information about the Python-list mailing list