Skip to content

FOR007 default-real-literal-precision-loss ​

warning enabled by default

A real literal without kind is assigned to a higher-precision real and cannot be represented in default precision.

Why ​

A literal without kind suffix is a default (single-precision) real, whatever it is assigned to: x = 0.1 with x double precision stores 0.100000001490116..., not 0.1. The error (about 1e-8) is far above double-precision round-off and silently caps the accuracy of the whole computation. Suffix every real literal with the kind of the computation (0.1_R8P); literals exactly representable in single precision (0.5, 2.0) are not reported, though the suffix keeps the code correct when the working precision changes. Kinds are resolved through the project's named constants (R8P = selected_real_kind(15, 307), real64, ...).

Reported ​

fortran
real(R8P) :: dt
dt = 0.1 * cfl

Preferred ​

fortran
real(R8P) :: dt
dt = 0.1_R8P * cfl

Suppressing ​

For one occurrence, with the reason on the same line (or alone on the line before the statement):

fortran
! fobis: ignore[FOR007] <why this occurrence is fine>

For the whole project, ignore = FOR007 in the fobos [lint] section.

References ​

Sections of the shipped knowledge packs behind this rule:

  • hpc-practice:fortran-practice#kind-specifications-portability (fobis kb show hpc-practice:fortran-practice#kind-specifications-portability)
  • hpc-practice:fortran-practice#forbidden-kind-violating-patterns (fobis kb show hpc-practice:fortran-practice#forbidden-kind-violating-patterns)