The result of 0.3+0.15==0.45 is true or false?

Hi, I have a problem. I ran this expression "0.3+0.15==0.45" in Matlab the result is "0", but I ran the same expression in Mathematica, the result is "True". Why is that? AND if a,b are both variables for floating-point numbers, is it acceptable if I use "a==b" as the condition in the "if function"? Could anybody help me out? Thanks a lot!

 Accepted Answer

3 Comments

And a workaround:
if abs(0.3 + 0.15 - 0.45) < 10*eps
But the limit 10*eps is arbitrary! A relative error would be smarter:
if abs(((0.3 + 0.15) - 0.45) / 0.45) < 10*eps
But if the values are very small, even this might fail.
@Jiasen: It is a good idea to read the complete FAQ - why not takine advantage from the problems of others...

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!