+ 1

Python question

n=int(input()) k=0 b=1 while k<=n: m=1/(10**k) k+=1 b+=m print ("m=",m) print ("b=",b) When I input 3 into this code, the output, I think, should be 2.111, but it's not. Instead, the output is 2.11099..Could somebody explain to me why?

28th Jun 2020, 12:34 PM
Sava
3 Answers
+ 8
This is because floating-point numbers can be represented to a certain degree of precision only, using the computer's undercore binary system: https://en.m.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems
28th Jun 2020, 12:39 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 1
Thank you very much.
28th Jun 2020, 2:47 PM
Sava
0
Sava this is a common issue in python. Google "python float precision" or visit: https://docs.python.org/3/tutorial/floatingpoint.html for more detail!
28th Jun 2020, 12:39 PM
Thống Nguyễn
Thống Nguyễn - avatar