Python Forum

Full Version: floating point not increasing properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

whenever i run the below loop as part of a code

x=0.
while(x<=2):
x+=.1
print(x)


i am getting the following result. why it is not accurately increasing by .1? i am a beginner. anyone could help please..




.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


i am using python 3.7 with pycharm 020
x=0.
while (x<=2):
	x+=.1
	print(format(x,'.1f'))
You need to format output to limit digits after decimal point.
Visit this page:
print(f"http://{0.1 + 0.2}.com")
thanks guys. The explanation and solution were very good on the links attached. Thanks for helping me.