Floating point problem
Dan Sommers
2QdxY4RzWzUUiLuE at potatochowder.com
Sat Apr 18 09:43:35 EDT 2020
On Sun, 19 Apr 2020 01:25:00 +1200
DL Neil via Python-list <python-list at python.org> wrote:
> 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...
These days, I continue to recommend _What Every Computer Scientist
Should Know About Floating-Point Arithmetic_ by David Goldberg. The
first link that pops up in my search bubble is
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html; YMMV.
Yes, it's highly technical, but well worth the effort.
Or an extended session at the feet of the timbot, which is much harder
to come by.
Dan
--
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
More information about the Python-list
mailing list