+ 2
Error
if 0.1+0.2==0.3: print("True") else: print("False") #output: False #explain plz
7 ответов
+ 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.
+ 4
Following Afnan Irtesum Chowdhury answer here is how to make the answer
True
https://sololearn.com/compiler-playground/cpkRcI8FR0ra/?ref=app
+ 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
+ 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.
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")
0
Import math
If math.isclose(0.1 + 0.2, 0.3) :
print("True")
else:
print("false")
0
Python
S="шындық"
Print (S[0:3])
#нәт:шын