if(0.41 == (41*0.01)) is False and if(0.42 == (42*0.01)) is True .. Why?
1 view (last 30 days)
Show older comments
In following script, I am getting 'false' for first condition and 'true' for second condition.
clc
clear all
if(0.41 == (41*0.01)) %First Condition
disp('true')
else
disp('false')
end
if(0.42 == (42*0.01)) %Second Condition
disp('true')
else
disp('false')
end
2 Comments
Stephen23
on 30 Jul 2020
The effect of accumulated error with binary floating point numbers is very well documented:
This is worth reading as well:
Accepted Answer
KSSV
on 30 Jul 2020
Read about comparing floating-point numbers.
tol = 10^-5 ;
if(abs(0.41-(41*0.01)<=tol)) %First Condition
disp('true')
else
disp('false')
end
if(abs(0.42 - (42*0.01))<=tol ) %Second Condition
disp('true')
else
disp('false')
end
More Answers (0)
See Also
Categories
Find more on Image Data Workflows 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!