Floating point problem

DL Neil PythonList at DancesWithMice.info
Sat Apr 18 10:21:54 EDT 2020


On 19/04/20 1:51 AM, Souvik Dutta wrote:
> I literally tried it!!! And it did not stop because I did not get any 
> 1.0 rather I got 0.99999999999 But why does this happen. This is a 
> simple math which according to normal human logic should give perfect 
> numbers which are not endless. Then why does a computer behave so 
> differently?

Please don't top-post - (human) conversations are normally question 
followed by answer, not the other way around!

Computers use binary, not decimal - asked and answered (see previous 
first response, below)


> On Sat, 18 Apr, 2020, 7:02 pm DL Neil via Python-list, 
> <python-list at python.org <mailto: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...
>     -- 
>     Regards =dn
>     -- 
>     https://mail.python.org/mailman/listinfo/python-list
> 

-- 
Regards =dn


More information about the Python-list mailing list