Python with float variable problems

Asked

Viewed 94 times

-1

Galera wrote a program where I decrease a variable in 0.01 and from the third decrease, python simply recognizes the value 0.01 as 0.009999999999988617 and ends up giving problems in my code. In the example code below, run as you are and then change the value from a to 0.03 or more, and see what I say. Why is this? a = 0.02 while a != 0.00: a -=0.01 print(a)

1 answer

2

This occurs naturally with decimal numbers because they are represented a "floating point" in the computer memory, which generates a certain imprecision in this type of variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.