+ 2

Error

if 0.1+0.2==0.3: print("True") else: print("False") #output: False #explain plz

3rd Mar 2025, 4:31 PM
Ravi Anand
Ravi Anand - avatar
7 Respostas
+ 4
In most programming languages including Python, floating-point values are stored in the IEEE 754 format. This format represents numbers in binary. In binary, these floating-point numbers don't have an exact representation. Think of it as 10/3, the result is something like 3.3333... it doesn't have an exact representation. For instance, 0.1 in binary isn't actually just 0.100000.. it's 0.000110011... and it's the same case with 0.2. So when you add the values together, the result is something like 0.3000004, which is not equal to exactly 0.3.
3rd Mar 2025, 4:47 PM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
3rd Mar 2025, 7:50 PM
BroFar
BroFar - avatar
M
+ 2
Follow up with Afnan Irtesum Chowdhury and BroFar answers. This reading contains a longer yet easy to understand explanation with examples. https://inventwithpython.com/beyond/chapter8.html#calibre_link-177
4th Mar 2025, 10:03 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
0.1 + 0.2 in Python is not exactly 0.3. If you check the result: print(0.1 + 0.2) # Output: 0.30000000000000004 Since 0.30000000000000004 is not exactly 0.3, the condition 0.1 + 0.2 == 0.3 evaluates to False.
5th Mar 2025, 2:00 PM
Chitranshi Gupta
Chitranshi Gupta - avatar
0
Floating-point precision error: print(0.1 + 0.2) # 0.30000000000000004 Fix: if round(0.1 + 0.2, 10) == 0.3: print("True") # Output: True else: print("False")
5th Mar 2025, 2:13 AM
Muhammad Abdul Wasea
Muhammad Abdul Wasea - avatar
0
Import math If math.isclose(0.1 + 0.2, 0.3) : print("True") else: print("false")
5th Mar 2025, 5:47 AM
Badshan Gaming
Badshan Gaming - avatar
0
Python S="шындық" Print (S[0:3]) #нәт:шын
5th Mar 2025, 1:43 PM
Araylum Asilkhan
Araylum Asilkhan - avatar