Skip to content

Commit ebe924a

Browse files
committed
implemented generic clip function in stdlib_math.fypp
1 parent 060db2d commit ebe924a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/stdlib_math.fypp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#:include "common.fypp"
2+
3+
#:set INT_KINDS_TYPES = [('int8', 'integer'), (int16, 'integer'), ('int32', 'integer'), (int64, 'integer')]
4+
#:set REAL_KINDS_TYPES = [('sp', 'real'), ('dp', 'real'), ('qp', 'real')]
5+
6+
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES
7+
module stdlib_math
8+
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp
9+
10+
implicit none
11+
private
12+
public :: clip
13+
14+
15+
interface clip
16+
#:for k1, t1 in IR_KINDS_TYPES
17+
#:set RName = rname('clip', t1, k1)
18+
module function ${RName}$(x, xmin, xmax) result(res)
19+
${t1}$(${k1}$), intent(in) :: x
20+
${t1}$(${k1}$), intent(in) :: xmin
21+
${t1}$(${k1}$), intent(in) :: xmax
22+
${t1}$(${k1}$) :: res
23+
end function ${RName}$
24+
#:endfor
25+
end interface clip
26+
27+
28+
contains
29+
#:for k1, t1 in IR_KINDS_TYPES
30+
#:set RName = rname('clip', t1, k1)
31+
elemental function ${RName}$(x, xmin, xmax) result(res)
32+
! I hope that I am not required to redefine these inputs and output again, because I have already
33+
! done that in the interface
34+
res = max(min(x, xmax), xmin)
35+
end function ${RName}$
36+
#:endfor
37+
38+
! What should I do if xmin(int8) is given with xmax(int16) as inputs:
39+
! will fortran automatically convert xmin(int8) to xmin(int16) ??
40+
! OR, I will have to take care of that explicity by not specifying
41+
! kind for xmin, xmax and x in line 17, 18, and 19.
42+
end module stdlib_math

0 commit comments

Comments
 (0)