Why is logical array fasle?
2 views (last 30 days)
Show older comments
Hitesh Pradhan on 15 Jun 2021
Answered: Kunal Kandhari on 15 Jun 2021
g=9.8000 %value for g
p=3.14; %value of mathematical pie
k1==g %generation of logical array
Scott MacKenzie on 15 Jun 2021
The result is false because k1 DOES NOT EQUAL g
Note that both k1 and g are scalars, not vectors. Therefore, the result of k1==g is a logical scalar which is false or 0.
Steven Lord on 15 Jun 2021
Floating point arithmetic. While k1 and g are displayed the same they do not contain the same value down to the last bit.
Kunal Kandhari on 15 Jun 2021
It is because most decimal fractions are not exactly representable in binary form, just like 1/3 is not representable exactly in a finite number of decimal digits.
if you print 55 decimal places of both the variables you will get something like this:
NEVER compare two floating point doubles for exact equality. Well, there are some notable exceptions. So unless you know the exceptions, then assume never applies.
You can see a very detailed description of the same here:
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!