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.

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 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 ABI kind. All four procedures are elemental, so they also apply element by element to arrays of any rank.

interface  f_undefined_module/f_undefined(one)#

Return an undefined sentinel value with the same intrinsic kind as the mold argument.

This is the constructor-like entry point of the undefined sentinel API. Preferred usage is to obtain a sentinel with x = f_undefined(x), assign one in place with call f_undefine(x), and test values with f_is_undefined(x) or f_is_defined(x).

Do not compare sentinel values with == or /= in client code. The real and complex sentinels may use quiet NaN bit patterns, for which ordinary numerical comparison is neither portable nor semantically correct. Logical kinds are intentionally unsupported because there is no portable sentinel value distinct from true and false.

Parameters:

one [any_numeric, in] :: Mold: only its type and kind are used, never its value.

interface  f_undefined_module/f_undefine(value)#

Assign the undefined sentinel to a scalar variable in place.

This is the in-place form of f_undefined: after the call, the scalar value carries the Futile undefined representation for its intrinsic kind. Test the result with f_is_undefined or f_is_defined rather than with direct equality comparisons.

Parameters:

value [any_numeric, out] :: Set to the undefined sentinel of its type and kind.

Called from:

f_maxdiff_i0(), f_maxdiff_li0(), f_maxdiff_d0(), f_maxdiff_r0(), f_maxdiff_c1i1(), f_maxdiff_c1li1(), f_maxdiff_c0i1(), f_maxdiff_c0li1(), f_maxdiff_d2d3(), f_maxdiff_d2d1(), f_maxdiff_d3d1(), f_maxdiff_d0d1(), f_maxdiff_d1d2(), f_maxdiff_d2(), f_maxdiff_d1(), f_maxdiff_i2i1(), f_maxdiff_i3i1(), f_maxdiff_li2li1(), f_maxdiff_i2(), f_maxdiff_i1(), f_maxdiff_li1(), f_maxdiff_i1i2(), f_maxdiff_li1li2(), f_maxdiff_li0li1(), f_maxdiff_i0i1()

interface  f_undefined_module/f_is_undefined(value)#

Test whether a scalar value has the Futile undefined sentinel representation.

This predicate is the portable way to recognize undefined values produced by f_undefined or f_undefine, including real and complex sentinels represented with quiet NaN bit patterns.

Parameters:

value [any_numeric, in] :: Value tested, of any rank (elemental).

interface  f_undefined_module/f_is_defined(value)#

Test whether a scalar value is different from the Futile undefined sentinel representation.

This is the logical complement of f_is_undefined and is preferred over direct comparisons with the sentinel constants, so generic code remains safe when the intrinsic kind changes.

Parameters:

value [any_numeric, in] :: Value tested, of any rank (elemental).

Typical calls#

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_<kind> = -huge(0_<kind>);

  • 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 ABI kind. Logical kinds are deliberately not supported: there is no portable sentinel distinct from .true. and .false.. Character values have no sentinel either.

10 specific procedures implement f_undefined (generated by FYPP; table emitted by the documentation stub f_undefined_doc.f90 from the same loops).

Type (kind)

ranks of one

specifics

integer (f_int8, f_short, f_integer, f_long); real (f_simple, f_double, f_long_double); complex (f_simple, f_double, f_long_double)

elemental (any rank)

1 each

10 specific procedures implement f_is_undefined (generated by FYPP; table emitted by the documentation stub f_undefined_doc.f90 from the same loops).

Type (kind)

ranks of value

specifics

integer (f_int8, f_short, f_integer, f_long); real (f_simple, f_double, f_long_double); complex (f_simple, f_double, f_long_double)

elemental (any rank)

1 each

f_undefine and f_is_defined have the same specifics, with the argument value.

Notes#

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 Memory handling.

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).

subroutine example_f_undefined()
  real(f_double) :: energy
  integer(f_integer), dimension(3) :: counts
  complex(f_double) :: phase

  energy = f_undefined(energy)        ! sentinel of the kind of the mold
  call f_undefine(counts)             ! elemental: every element
  call f_undefine(phase)
  call require(f_is_undefined(energy), 'real sentinel not recognized')
  call require(all(f_is_undefined(counts)), 'integer sentinels not recognized')
  call require(f_is_undefined(phase), 'complex sentinel not recognized')
  counts(2) = 0
  call require(f_is_defined(counts(2)), 'defined value taken as undefined')
  call require(count(f_is_defined(counts)) == 1, 'elemental predicate failed')
end subroutine example_f_undefined

See also#