f_undefined =========== Mark numeric variables as undefined and test them portably: ``f_undefined`` returns the undefined sentinel of the kind of its argument, ``f_undefine`` assigns it in place, and ``f_is_undefined`` / ``f_is_defined`` recognize it. .. code-block:: fortran use futile ! or: use f_utils, only: f_undefined, f_undefine, f_is_undefined, f_is_defined ! or: use f_undefined_module, only: f_undefined, f_undefine, f_is_undefined, f_is_defined ``f_undefined_module`` (``flib/f_undefined.f90``) defines the four generic interfaces; ``f_utils`` and ``futile`` re-export them unchanged. The sentinel values themselves are the ``undefined_f_*`` constants, which remain in :doc:`f_precisions ` so that they can be used in initialization expressions. ``f_precisions`` no longer provides the generics: code importing them from ``f_precisions`` should import them from ``f_undefined_module`` (or ``f_utils``) instead and recompile. Signature --------- Generated from the FYPP documentation stub ``doc/generated/f_undefined_doc.f90`` (emitted by ``fypp/undefined-doc.fypp`` from the same loops as ``flib/f_undefined.f90``, which ``fypp/generate-undefined.py`` generates from ``fypp/undefined.fypp``). In the signatures, ``any_numeric`` is a documentation placeholder: the argument may be an integer, real or complex value of any Futile :ref:`ABI kind `. All four procedures are elemental, so they also apply element by element to arrays of any rank. .. f:autointerface:: f_undefined .. f:autointerface:: f_undefine .. f:autointerface:: f_is_undefined .. f:autointerface:: f_is_defined Typical calls ------------- .. code-block:: fortran x = f_undefined(x) ! sentinel of the type and kind of x call f_undefine(x) ! the same, in place call f_undefine(array) ! elemental: every element if (f_is_undefined(x)) ... ! never compare with == or /= n = count(f_is_defined(array)) ! elemental predicate Sentinel values --------------- The sentinel of each kind is the corresponding ``f_precisions`` constant: * integer kinds (``f_int8``, ``f_short``, ``f_integer``, ``f_long``): ``undefined_f_ = -huge(0_)``; * ``f_simple`` and ``f_double`` reals: a quiet-NaN bit pattern, ``undefined_f_simple`` and ``undefined_f_double``; * ``f_long_double`` reals: the finite value ``-huge(0.0_f_long_double)``, as a compile-time NaN is not portable for long double; * complex kinds: both the real and imaginary parts carry the sentinel of the corresponding real kind. ``f_is_undefined`` compares the bit patterns of the ``f_simple`` and ``f_double`` (real and complex) values, so a quiet-NaN sentinel is recognized even though ``NaN == NaN`` is false, and other NaNs are not mistaken for it. Integer and ``f_long_double`` values (real and complex) are compared with ``==``. ``f_is_defined(x)`` is always ``.not. f_is_undefined(x)``. Supported kinds --------------- One elemental specific per generic covers each integer, real and complex :ref:`ABI kind `. Logical kinds are deliberately not supported: there is no portable sentinel distinct from ``.true.`` and ``.false.``. Character values have no sentinel either. .. fortran-specifics:: f_undefined :args: one .. fortran-specifics:: f_is_undefined :args: value ``f_undefine`` and ``f_is_defined`` have the same specifics, with the argument ``value``. Notes ----- .. rubric:: Do not compare sentinels numerically A real NaN sentinel compares unequal to everything, itself included, and taking part in arithmetic, tolerance tests or reductions propagates it. Use ``f_is_undefined`` / ``f_is_defined`` only; in error paths (for instance after a failed ``f_maxdiff``) check the error status first, see :doc:`/memory`. .. rubric:: Generated source ``fypp/undefined.fypp`` refuses to generate for a target kind map in which two of these kinds share a kind value (for instance ``f_long_double`` equal to ``f_double``), because one specific would otherwise be dropped silently. Ordinary builds compile the checked-in source and never need FYPP. Examples -------- The example is compiled and run by CI (``tests/flib/f_precisions_examples.f90``). .. fortranliteral:: tests/flib/f_precisions_examples.f90 :marker: f_undefined :dedent: 2 See also -------- * :doc:`/f_prec` -- kind parameters and the ``undefined_f_*`` sentinel constants. * :doc:`/memory` -- ``f_maxdiff`` and ``fmpi_maxdiff`` return undefined values on error.