Contenuto principale

hisl_0016: Usage of blocks that compute relational operators

R2026b

Avoid using equality and inequality operators on floating-point data types

Usage: High-Integrity System Modeling

Guideline ID: hisl_0016

Rules

hisl_0016: Usage of blocks that compute relational operators

To support the robustness of the operations, avoid using the equality and inequality operators on floating-point data types.

Rationale

Improve model robustness and prevent unexpected results.

Verification

Check relational comparisons on floating-point signals (Simulink Check)

Example — Correct

  • myDouble > 0.99 && myDouble < 1.01; % test range

Example — Incorrect

  • myDouble == 1.0

  • mySingle ~= 15.0

Example — Correct

Equality comparison operators are not used in floating-point operands.

Simulink model with single inputs d1, d2, and tol feeding a Stateflow chart that evaluates abs(d1 - d2) < tol to avoid equality comparison on floating-point data.

Example — Incorrect

Equality comparison operator == is used in floating-point operands.

Simulink model with single inputs d1 and d2 feeding a Stateflow chart that uses the equality operator d1 == d2 on floating-point data.

Example — Correct

To test whether two floating-point variables or expressions are equal, compare the difference of the two variables against a threshold that takes into account the floating-point relative accuracy (eps) and the magnitude of the numbers. The following pattern shows how to test two double-precision input signals, In1 and In2, for equality.

Simulink model using Subtract, Abs, Constant (threshold), and Relational Operator (<=) blocks to compare two double inputs for equality within a threshold.

Example — Incorrect

Equality comparison operator == is used in floating-point operands.

Simulink model with two double inputs connected to an equality operator block, producing a boolean output.

Tips

  • Due to floating-point precision issues, do not test floating-point expressions for equality (==) or inequality (~=, !=).

Industry Standards

  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language'

    IEC 61508-3, Table A.3 (3) 'Language subset'

    IEC 61508-3, Table A.4 (3) 'Defensive programming'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'

    ISO 26262-6, Table 1 (1c) 'Enforcement of strong typing'

  • EN 50128, Table A.4 (11) 'Language Subset'

    EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'

    EN 50128, Table A.3 (1) 'Defensive Programming'

  • EN 50657, Table A.4 (11) 'Language Subset'

    EN 50657, Table A.4 (8) 'Strongly Typed Programming Language'

    EN 50657, Table A.3 (1) 'Defensive Programming'

  • EN 50716, Table A.3 (1) 'Defensive Programming'

  • DO-331, Section MB.6.3.1.g 'Algorithms are accurate'

    DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • MISRA C:2012, Dir 1.1

  • MISRA C:2023, Dir 1.1

Version History

Introduced in R2010a

expand all