+ 4

[Solved] 0.1+0.1+0.1==0.3 evaluates to False

Why the above expression evaluates to false in Python?

11th Feb 2020, 8:10 PM
Hare Krishna Bhardwaj
Hare Krishna Bhardwaj - avatar
7 odpowiedzi
+ 5
python cant represent 0.1 exactly as binary also not 0.3 as binary. try yourself with 1/2,1/4,1/8,1/16.... Error will become worse with adding.
11th Feb 2020, 10:00 PM
Oma Falk
Oma Falk - avatar
+ 2
Thanks for your support. I now understand this concept clearly. The precision and never-ending sequence makes this problem critical and conversion to binary makes it a top-tier limitation. Other points are there but I think this should be enough. Thanks again
11th Feb 2020, 10:08 PM
Hare Krishna Bhardwaj
Hare Krishna Bhardwaj - avatar
+ 2
Well your computer cannot really correctly store data in its memory,and the only form of language your computer understands is machine language,its a low level language in which all other languages are compiled or interpreted into,and machine language converts your high level code into low level language(binary),so 0.1 converted to binary form is like this:0.00011001100110011001100110011001100110011001100110011001100110...till infinity,so 0.1 in 3 places will be like 0.01001100 till infinity,so it has to truncate a part of this binary digit called the least significant digit,so for example 11.00000000002 may be stored in your computer's memory as 11.0but 11.000000002 is not equal to 11
12th Feb 2020, 3:41 AM
Codebeast**
Codebeast** - avatar
+ 1
it is a problem of binary representation. You had it yet in school?
11th Feb 2020, 9:47 PM
Oma Falk
Oma Falk - avatar
0
maybe because python is not strongly typed language and both sides are compared as string instead of float.
11th Feb 2020, 8:33 PM
Aoun Naqvi
Aoun Naqvi - avatar
0
Oma Falk, Well I am about to pass 11th and we had a crash course [i.e. Syllabus of one year in just three months] therefore my basics are somewhat strong but not fully complete. So please explain this problem as detailed as possible
11th Feb 2020, 9:55 PM
Hare Krishna Bhardwaj
Hare Krishna Bhardwaj - avatar