Float number subtracking gives many unnecessary characters

Viewed 14

I set the first number to 0.9 and then used while to subtract some value from the general number, such as 0.1.

But after the second loop, it looks weird. What I was expecting was 0.9 --> 0.8 --> 0.7 --> and so on.

But there is a 0.9 --> 0.8 --> 0.7000000000000001 --> ...


This example is just to show what I mean.

Example

number = 0.9

while True:
    print(number)
    number -= 0.1
    
    if number < 0.2: # 
        break

Output

0.9
0.8
0.7000000000000001
0.6000000000000001
0.5000000000000001
0.40000000000000013
0.30000000000000016
0.20000000000000015

What is there wrong?

0 Answers
Related