Floating-point comparisons
ID | CC-14 |
Title | Do not compare floating-point values using == or ~= . |
Priority | Strongly recommended |
Severity level | 3 |
Description | Do not compare floating-point values using == or ~= . Rounding errors due to algorithm design or machine precision can cause unexpected results. Use a tolerance instead. Generated code can produce floating-point results different from the MATLAB results. |
Rationale | Robustness |
Avoid:
out = myFcn(in) == sqrt(3);
Instead use:
out = abs(myFcn(in) - sqrt(3)) < 1e-12;