abs(0.01) > 0.01 is false? The answer is weired!
Show older comments
K>> abs(currentTradePrice - lastPrice)
ans =
0.0100
K>> 0.0100 >= 0.01
ans =
1
K>> abs(currentTradePrice - lastPrice) >= 0.01
ans =
0
Accepted Answer
More Answers (1)
John D'Errico
on 26 Feb 2012
You have just won the award for being the millionth person to ask this question! Sorry, but first prize in this contest is only a bobble head doll modeled after me. Second prize is TWO bobble head dolls of me. :)
Try this:
X = 0.01 - eps
X =
0.01
See that X is displayed as 0.01, even though it is just a tiny bit less. Now, if we compare it to 0.01, we see that MATLAB sees a difference, even though the number was displayed as 0.01.
X < 0.01
ans =
1
In fact, if we display X in a long format, we will see that it is not truly 0.01, even though the short format rounded it off for display purposes.
format long g
X
X =
0.00999999999999978
Of course, a deeper investigation will reveal that 0.01 is not even truly 0.01. If I convert the number that you put into MATLAB as 0.01 from the IEEE double precision form into a true decimal form...
hpf(0.01)
ans =
0.01000000000000000020816681711721685132943093776702880859375
This is true because MATLAB stores the number as a binary number. But most decimal numbers are not exactly representable in binary. Avoid testing for equality in floating point arithmetic. And always be careful with tests done on non-integers.
The moral of the story is to understand floating point numbers. And never totally trust what you see.
Categories
Find more on Programming 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!