Skip to content

Commit 2608434

Browse files
committed
implemented generic clip function
1 parent b522bbb commit 2608434

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(fppFiles
2222
stdlib_quadrature_trapz.fypp
2323
stdlib_quadrature_simps.fypp
2424
stdlib_stats_distribution_PRNG.fypp
25+
stdlib_math.fypp
2526
)
2627

2728

src/Makefile.manual

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SRCFYPP =\
1818
stdlib_stats_moment_mask.fypp \
1919
stdlib_stats_moment_scalar.fypp \
2020
stdlib_stats_var.fypp \
21+
stdlib_math.fypp \
2122
stdlib_stats_distribution_PRNG.fypp
2223

2324
SRC = f18estop.f90 \

src/stdlib_math.fypp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
module procedure clip_${t1}$_${k1}$
18+
#:endfor
19+
end interface clip
20+
21+
22+
contains
23+
#:for k1, t1 in IR_KINDS_TYPES
24+
elemental function clip_${t1}$_${k1}$(x, xmin, xmax) result(res)
25+
${t1}$(${k1}$), intent(in) :: x
26+
${t1}$(${k1}$), intent(in) :: xmin
27+
${t1}$(${k1}$), intent(in) :: xmax
28+
${t1}$(${k1}$) :: res
29+
res = max(min(x, xmax), xmin)
30+
end function clip_${t1}$_${k1}$
31+
#:endfor
32+
33+
end module stdlib_math

0 commit comments

Comments
 (0)