メインコンテンツ

hisl_0004: Usage of natural logarithm and base 10 logarithm operations

R2026b

Protect against invalid inputs to logarithm operations

Usage: High-Integrity System Modeling

Guideline ID: hisl_0004

Rules

hisl_0004: Usage of natural logarithm and base 10 logarithm operations
A

Protect the input from going negative.

Rationale

Support generation of robust code.

Verification

Check usage of log and log10 operations (Simulink Check)

Example — Incorrect

Logarithm operation with negative real input produces NaN output.

Simulink model with Constant -100 feeding a natural log block with real output, producing NaN

Example — Correct

Abs block protects the logarithm input from going negative. The following plot displays the resulting output for input values ranging from -100 to 100.

Simulink model using Abs block to protect against negative inputs before the natural log function

XY plot showing natural log output with Abs protection for inputs -100 to 100, with a spike near zero and symmetric logarithmic curves

B

Protect the input from equaling zero.

Rationale

Support generation of robust code.

Verification

Check usage of log and log10 operations (Simulink Check)

Example — Incorrect

Logarithm operation with zero input produces -inf output.

Simulink model with Constant 0 feeding a natural log block, producing -inf output

Example — Correct

MinMax block with a Constant block set to eps protects the logarithm input from equaling zero. The following plot displays the resulting output for input values ranging from -100 to 100.

Simulink model using MinMax block with eps to protect against zero before the natural log function

XY plot showing natural log output with MinMax eps protection for inputs -100 to 100, with output clamped near -36 for negative inputs and logarithmic curve for positive inputs

C

Account for complex numbers as the output value.

Rationale

Support generation of robust code.

Verification

Check usage of log and log10 operations (Simulink Check)

Example — Correct

Setting Output Data to Complex on the logarithm block produces valid complex output for negative inputs instead of NaN.

Simulink model with Constant -100 feeding a natural log block with Output Data set to Complex, producing output 4.605 + 3.142i

Tips

  • If you set the output data type to complex, the natural logarithm and base 10 logarithm functions output complex values for negative input values. If you set the output data type to real, the functions output NAN for negative numbers, and minus infinity (-inf) for zero values.

Industry Standards

  • 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(1d) 'Use of defensive implementation techniques'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'

  • EN 50657, Table A.4 (11) 'Language Subset'
    EN 50657, Table A.3 (1) 'Defensive Programming'

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

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

  • MISRA C:2012, Dir 4.1

  • MISRA C:2023, Dir 4.1

  • INT33-C. Ensure that division and remainder operations do not result in divide-by-zero errors

Version History

Introduced in R2010a

expand all