checkComparisons

In this check, all comparisons made with == or ~= are reported if any of its operands may be a non-integer number. Comparing two doubles using == or ~= that are not guaranteed to be whole numbers is not recommended because it can lead to unexpected results. This report lets you inspect the relevant comparisons yourself to make sure the comparisons work as intended. If necessary, you can adjust certain comparisons by adding a tolerance. The code analysis of CC4M attempts to determine what data types the operands of the comparison have. This can have three different results:

  • When any of the operands is known to be a single or double that is not always a whole number, the comparison is reported as a violation and you will see the result in red text.

  • When both operands are known to be whole numbers or non-numeric, the comparison is not reported.

  • In other cases, the comparison is reported and manual inspection is required to determine whether the comparison is acceptable, or whether a tolerance must be added. These cases will only be reported if the Report violations when not entirely sure preference is switched on. See section Preferences for a description of this preference.

The following example shows what will be reported and what should be used instead:

out = myFcn(in) == sqrt(3);

% Instead use:
out = abs(myFcn(in) - sqrt(3)) < 1e-12;

Exemption tag: %@ok<COMPA>