HP Fortran Programmer's Reference (September 2007)
Program units and procedures
Modules
Chapter 7 195
Example 7-12 precision.f90
MODULE precision
! The named constant adequate is a kind number of a real
! representation with at least 10 digits of precision and 99
! digits range that normally results in 64-bit arithmetic.
! This constant ensures the same level of precision
! regardless of whether the program
! of whether the program is compiled on a 32-bit or 64-bit
! single-precision machine.
INTEGER, PARAMETER :: adequate = SELECTED_REAL_KIND(10,99)
END MODULE precision
Example 7-13 lin_eq_slv.f90
MODULE linear_equation_solver
USE precision
IMPLICIT NONE
PRIVATE adequate ! to avoid a "double definition" of adequate
! in program units that also use precision
! forbid outside access to these two module procedures
PRIVATE :: factor, back_substitution
CONTAINS ! module procedures defined here
SUBROUTINE solve_linear_equations (a, x, b, error)
! solve the system of linear equations ax = b; set error to
! true if the extents of a, x, and b are incompatible or
! a zero pivot is found
REAL (adequate), DIMENSION (:, :), INTENT (IN) :: a
REAL (adequate), DIMENSION (:), INTENT (OUT) :: x
REAL (adequate), DIMENSION (:), INTENT (IN) :: b
LOGICAL, INTENT (OUT) :: error
REAL (adequate), DIMENSION (SIZE (b), SIZE (b) + 1) :: m
INTEGER :: n
n = SIZE (b)
! check for compatible extents
error = SIZE(a, DIM=1) /= n .OR. SIZE(a, DIM=2) /= n &
.OR. SIZE(x).LT. n
IF (error) THEN
x = 0.0
RETURN
END IF
! append the right-hand side of the equation to m
m (1:n, 1:n) = a
m (1:n, n+1) = b
! factor m and perform forward substitution in the last
! column of m
CALL factor (m, error)
IF (error) THEN