0

Strange output? I just don't understand, please help!

My code from an early lesson I messed with i = 0 while 1==1: print(i) i +=0.1 if i >= 5: print("Breaking") break print("Finished") I would expect it to count up from 0 at exactly increments of .1 up til 5, yet I get this: 0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6 0.7 0.7999999999999999 0.8999999999999999 0.9999999999999999 1.0999999999999999 1.2 1.3 1.4000000000000001 1.5000000000000002 1.6000000000000003 1.7000000000000004 1.8000000000000005 1.9000000000000006 2.0000000000000004 2.1000000000000005 2.2000000000000006 2.3000000000000007 2.400000000000001 2.500000000000001 2.600000000000001 2.700000000000001 2.800000000000001 2.9000000000000012 3.0000000000000013 3.1000000000000014 3.2000000000000015 3.3000000000000016 3.4000000000000017 3.5000000000000018 3.600000000000002 3.700000000000002 3.800000000000002 3.900000000000002 4.000000000000002 4.100000000000001 4.200000000000001 4.300000000000001 4.4 4.5 4.6 4.699999999999999 4.799999999999999 4.899999999999999 4.999999999999998 Breaking Finished

25th Apr 2020, 2:49 PM
Jon Koch
Jon Koch - avatar
2 Antworten
0
You should do precision handling in python to correct this error. Check out this for more info: https://www.google.com/amp/s/www.geeksforgeeks.org/precision-handling-python/amp/
25th Apr 2020, 11:05 PM
! Bad Coder
! Bad Coder - avatar