Python Forum
floating point not increasing properly - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: floating point not increasing properly (/thread-26364.html)



floating point not increasing properly - rakeshpe43 - Apr-29-2020

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


RE: floating point not increasing properly - buran - Apr-29-2020

https://docs.python.org/3/tutorial/floatingpoint.html


RE: floating point not increasing properly - Shahmadhur13 - Apr-29-2020

x=0.
while (x<=2):
	x+=.1
	print(format(x,'.1f'))
You need to format output to limit digits after decimal point.


RE: floating point not increasing properly - DeaD_EyE - Apr-29-2020

Visit this page:
print(f"http://{0.1 + 0.2}.com")



RE: floating point not increasing properly - rakeshpe43 - Apr-30-2020

thanks guys. The explanation and solution were very good on the links attached. Thanks for helping me.