Python Forum
floating point not increasing properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
floating point not increasing properly
#1
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
Reply
#2
https://docs.python.org/3/tutorial/floatingpoint.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
x=0.
while (x<=2):
	x+=.1
	print(format(x,'.1f'))
You need to format output to limit digits after decimal point.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#4
Visit this page:
print(f"http://{0.1 + 0.2}.com")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
thanks guys. The explanation and solution were very good on the links attached. Thanks for helping me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace String with increasing numer [SOLVED] AlphaInc 13 4,918 Aug-07-2021, 08:16 AM
Last Post: perfringo
  Increasing Line Space In Spyder IDE JoeDainton123 2 2,864 Aug-19-2020, 08:37 PM
Last Post: JoeDainton123
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,524 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Complex floating issue arshad 2 11,768 Nov-05-2019, 03:26 PM
Last Post: arshad
  floating point arithmetic exDeveloper 2 2,072 Sep-25-2019, 04:33 PM
Last Post: DeaD_EyE
  finding the closest floating point number in a list Skaperen 17 8,112 Sep-19-2019, 10:39 PM
Last Post: Skaperen
  finding the next higher representable floating point value Skaperen 0 1,916 Sep-13-2019, 11:16 PM
Last Post: Skaperen
  Subtracting values between two dictionaries/ floating point numbers FloppyPoppy 5 5,827 Mar-04-2019, 01:00 PM
Last Post: snippsat
  Conversion needed from bytearray to Floating point braveYug 1 4,063 May-07-2018, 12:23 PM
Last Post: snippsat
  print floating point number Regulus 19 11,000 Aug-14-2017, 02:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020