libSUFR
a LIBrary of Some Useful Fortran Routines
All Classes Namespaces Files Functions Variables Pages
kinds.f90
Go to the documentation of this file.
1!> \file kinds.f90 Procedures to distribute variable kinds
2
3
4! Copyright (c) 2002-2025 Marc van der Sluys - Nikhef/Utrecht University - marc.vandersluys.nl
5!
6! This file is part of the libSUFR package,
7! see: http://libsufr.sourceforge.net/
8!
9! This is free software: you can redistribute it and/or modify it under the terms of the European Union
10! Public Licence 1.2 (EUPL 1.2). This software is distributed in the hope that it will be useful, but
11! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12! PURPOSE. See the EU Public Licence for more details. You should have received a copy of the European
13! Union Public Licence along with this code. If not, see <https://www.eupl.eu/1.2/en/>.
14!
15!
16
17
18
19
20!***********************************************************************************************************************************
21!> \brief Provides kinds and related constants/routines
22!!
23!! Contains the integers double and dbl, which can be used to provide the kind of a (often double-precision) variable type.
24!! Variables can be declared using e.g. "real(double) :: x"; constants can be defined as e.g. "x = 3.0_dbl".
25
27 implicit none
28 save
29
30 ! Integer, double precision:
31 integer, parameter :: long = selected_int_kind(18) !< Long integer
32 integer, parameter :: lng = selected_int_kind(18) !< Long integer
33
34 ! Real, double precision:
35 integer, parameter :: double = selected_real_kind(15,307) !< Double-precision float. Precision = 15, range = 307
36 integer, parameter :: dbl = selected_real_kind(15,307) !< Double-precision float. Precision = 15, range = 307
37
38 !> Maximum integer kind:
39 integer, parameter :: intkindmax = max(selected_int_kind(9),selected_int_kind(18),selected_int_kind(38),selected_int_kind(99))
40
41 !> Maximum real kind:
42 integer, parameter :: realkindmax = max(selected_real_kind(6),selected_real_kind(15),selected_real_kind(18), &
43 selected_real_kind(31),selected_real_kind(33),selected_real_kind(99))
44
45 ! Problem with g95: not all functions have been defined in the maximum accuracy yet!
46 ! integer, parameter :: realmaxkind = double ! If the above doesn't work
47
48contains
49
50
51 !*********************************************************************************************************************************
52 !> \brief Get the kinds of the most accurate integer and real for the current compiler/system
53 !!
54 !! \param ikindmax Maximum integer kind (output)
55 !! \param rkindmax Maximum real kind (output)
56 !!
57 !! \param warn Warn if something funny happens (output, optional)
58 !!
59 !! \note Problem: ikindmax,rkindmax are not parameters (defined at compile time)!
60
61 subroutine max_accuracy_kinds(ikindmax,rkindmax, warn)
62 implicit none
63 integer, intent(out) :: ikindmax,rkindmax
64 logical, intent(in), optional :: warn
65 integer :: acc,rng,kind
66 integer :: rkindmax2 !,accmax,rngmax
67 logical :: warning
68
69 warning = .false.
70 if(present(warn)) warning = warn
71
72 ! Integer:
73 do rng=1,1000000
74 kind = selected_int_kind(rng)
75 if(kind.lt.0) exit
76 ! rngmax = rng
77 ikindmax = kind
78 end do
79
80 ! Real:
81 rng = 1
82 do acc=1,10000
83 kind = selected_real_kind(acc,rng)
84 if(kind.lt.0) exit
85 ! accmax = acc
86 rkindmax = kind
87 end do
88
89 acc = 1
90 do rng=1,1000000
91 kind = selected_real_kind(acc,rng)
92 if(kind.lt.0) exit
93 ! rngmax = rng
94 rkindmax2 = kind
95 end do
96
97 if(rkindmax2.ne.rkindmax) then
98 if(warning) then
99 write(6,'(/,A,2I6)')' Warning: max_accuracy_kinds found two different values for max kind: ',rkindmax,rkindmax2
100 write(6,'(A,/)')' You should check what is going on...'
101 end if
102 rkindmax = min(rkindmax,rkindmax2) ! Play it safe
103 end if
104
105 end subroutine max_accuracy_kinds
106 !*********************************************************************************************************************************
107
108
109
110end module sufr_kinds
111!***********************************************************************************************************************************
112
113
114
Provides kinds and related constants/routines.
Definition kinds.f90:26
integer, parameter intkindmax
Maximum integer kind:
Definition kinds.f90:39
integer, parameter lng
Long integer.
Definition kinds.f90:32
integer, parameter double
Double-precision float. Precision = 15, range = 307.
Definition kinds.f90:35
integer, parameter dbl
Double-precision float. Precision = 15, range = 307.
Definition kinds.f90:36
integer, parameter long
Long integer.
Definition kinds.f90:31
integer, parameter realkindmax
Maximum real kind:
Definition kinds.f90:42
subroutine max_accuracy_kinds(ikindmax, rkindmax, warn)
Get the kinds of the most accurate integer and real for the current compiler/system.
Definition kinds.f90:62