3.3 Numeric Polymorphism



next up previous
Next: 3.4 The Numeric Approximation Up: 3 Numerical Robustness Previous: 3.2 Precision Selection

3.3 Numeric Polymorphism

 

All of the computational intrinsic functions are generic over all of the type kinds provided by the implementation. Thus, for example, the result returned by COS(X) is the appropriate value of kind SINGLE, DOUBLE, IEEE, or P9, depending on whether X is of kind SINGLE, DOUBLE, IEEE, or P9, respectively. This generic property aids significantly in the development of portable robust application code. Intrinsic functions are similarly generic in Fortran 77, but a robustness deficiency of Fortran 77 is that user and implementation (and third party software vendor) supplied procedures cannot be made generic over argument types. Fortran 90 remedies this deficiency. (In this chapter ``polymorphism" can be assumed to mean that generic properties, familiar in regard to the Fortran 77 intrinsic functions, may be specified for and are therefore extended to user-defined procedures.)

The interface block can be used to specify a generic name for a set of user supplied procedures, or to add procedures to an existing generic name. In the following two examples, the first interface block defines a new generic name (SMOOTH) associated with four specific procedures, and the second interface block extends the COS intrinsic functions to arguments of type RATIONAL.

INTERFACE SMOOTH                      ! SMOOTH is the generic name

 INTEGER FUNCTION SMOOTH_INT(AA)      ! for procedures   SMOOTH_INT
  INTEGER :: AA(:,:)                  !    SMOOTH_SINGLE
 END FUNCTION SMOOTH_INT              !  SMOOTH_DOUBLE
                                      !   SMOOTH_RATIONAL
 INTEGER FUNCTION SMOOTH_SINGLE(AA)
  REAL(SINGLE) :: AA(:,:)             ! AA is an assumed shape two-
 END FUNCTION SMOOTH_SINGLE           ! dimensional array in each case.

 INTEGER FUNCTION SMOOTH_DOUBLE(AA)
  REAL(DOUBLE) :: AA(:,:)
 END FUNCTION SMOOTH_DOUBLE

 INTEGER FUNCTION SMOOTH_RATIONAL(AA)
  TYPE(RATIONAL) :: AA(:,:)
 END FUNCTION SMOOTH_RATIONAL

END INTERFACE

INTERFACE COS                        ! Extends the generic properties
 FUNCTION RATIONAL_COS(X)            !  of COS to return results of
  TYPE(RATIONAL) :: RATIONAL_COS     !  type RATIONAL, assuming the
  TYPE(RATIONAL) :: X                !  argument is of type RATIONAL.
 END FUNCTION RATIONAL_COS 
END INTERFACE

In the SMOOTH example notice that the argument (AA) is a different type in each case but the function result is the same type (INTEGER in each case). The function result could also have been different in some or all cases. The only requirement for such a generic set is that the type/kind/rank pattern for the set of dummy arguments be unique in each specific case. In the COS example, note the conceptual similarity with the extension of the ``+" operator in section 1 on page 1, User Defined Types and Operators.

These generic definition capabilities make it practical for the programmer to specify precision with the SELECTED_REAL_KIND function. The program can then be geared toward the optimal precision for the application rather than focussing on the specific precisions provided by the implementation. This not only is a more natural way to develop numerical software, but it contributes to numerical robustness as well. Fortran 77, C, and Fortran 90 versions of (one variation of) the routine SMOOTH are given in section 2.1.



next up previous
Next: 3.4 The Numeric Approximation Up: 3 Numerical Robustness Previous: 3.2 Precision Selection



verena@csep1.phy.ornl.gov