why is simulink matlab function block failing to evaluate a < operator correctly
1 view (last 30 days)
Show older comments
Robert Garton
on 24 Dec 2016
Edited: Walter Roberson
on 12 Jan 2017
I have a simply if else statment using <= and < operators. I have a situation where my code is incorrectly evaluating "remainder < Tfs*2" as true when the two values are 0.00004.
elseif (Tfs <= remainder & remainder < Tfs*2)
remainder < (Tfs*2) % this evaluates to 1
fprintf('%.15f, %.15f\n', remainder, Tfs*2); % output is 0.000040000000000, 0.000040000000000
vector = 1;
elseif (Tfs*2 <= remainder && remainder < Tfs*3)
0 Comments
Accepted Answer
Walter Roberson
on 24 Dec 2016
Edited: Walter Roberson
on 12 Jan 2017
0.000040000000000 is not enough digits of printout to see whether the two are equal or not.
4.00000000000000032721221565612523818344925530254840850830078125e-05
and
3.9999999999999996495858578526849669287912547588348388671875e-05
will both print out as 0.000040000000000 but are not equal.
You should either grab num2strexact from the File Exchange or you should print out
remainder - Tfs*2
You should also consider assigning Tfs*2 to a variable so that you avoid evaluating it multiple times. Floating point values calculated by even slightly different methods are permitted to give different results.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!