Why do two of the floats have 13 zero's? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Why do two of the floats have 13 zero's?

i = 1 while i <= 20: print(i) i = i + 2.1 print("Finished!") Output: 1 3.1 5.2 7.300000000000001 9.4 11.5 13.6 15.7 17.8 19.900000000000002 Finished! I have played around with different integers and floats but i can't find a pattern. The program seems to be adding zero's to random floats? Can anybody tell me why?

19th Jun 2017, 10:21 AM
Ryan
2 Antworten
+ 1
When a computer calculates a float it calculates it until it reaches a specific size. In this case it is 16 characters (or numbers behind the comma) But the computer will calculate it till the last bit. The best option you have is to round it to a number
19th Jun 2017, 10:26 AM
Limitless
Limitless - avatar
+ 1
Quick response, Thankyou 👍🏼
19th Jun 2017, 10:27 AM
Ryan