Skip to content

Commit d6f9773

Browse files
committed
Updated PR as per the feedback received
1 parent 8922365 commit d6f9773

File tree

4 files changed

+136
-147
lines changed

4 files changed

+136
-147
lines changed

doc/specs/stdlib_math.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ title: math
1919

2020
#### Description
2121

22-
Limits the input value `x` to the given interval [`xmin`, `xmax`] (interval is `xmin` and `xmax` inclusive). Returns a value which lies in the given interval and is closest to the input value `x`.
23-
If the input value `x` already lies in the given interval, then the output value will be equal to the input value.
22+
Returns a value which lies in the given interval [`xmin`, `xmax`] (interval is `xmin` and `xmax` inclusive) and is closest to the input value `x`.
2423

2524
#### Syntax
2625

@@ -36,25 +35,25 @@ Elemental function.
3635

3736
#### Argument(s)
3837

39-
`x`: scalar of either `integer` or `real`. This argument is `intent(in)`.
40-
`xmin`: scalar of either `integer` or `real`. This argument is `intent(in)`.
41-
`xmax`: scalar of either `integer` or `real`, which must be greater than or equal to `xmin`. This argument is `intent(in)`.
38+
`x`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
39+
`xmin`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
40+
`xmax`: scalar of either `integer` or `real` type, which must be greater than or equal to `xmin`. This argument is `intent(in)`.
4241

4342
Note: All arguments must have same `type` and same `kind`.
4443

4544
#### Output value or Result value
4645

47-
Output is a scalar of either `integer` or `real` depending on the arguments. The output value will have `type` and `kind` same as to that of the arguments.
46+
The output is a scalar of `type` and `kind` same as to that of the arguments.
4847

4948
#### Examples
5049

5150
##### Example 1:
5251

5352
Here inputs are of type `integer` and kind `int32`
5453
```fortran
55-
program demo
54+
program demo_clip_integer
5655
use stdlib_math
57-
use iso_fortran_env
56+
use stdlib_kinds
5857
implicit none
5958
integer(int32) :: x
6059
integer(int32) :: xmin
@@ -70,30 +69,30 @@ program demo
7069
7170
clipped_value = clip(x, xmin, xmax)
7271
! clipped_value <- 5
73-
end program demo
72+
end program demo_clip_integer
7473
```
7574

7675
##### Example 2:
7776

78-
Here inputs are of type `real` and kind `real32` (or `sp`)
77+
Here inputs are of type `real` and kind `sp`
7978
```fortran
80-
program demo
79+
program demo_clip_real
8180
use stdlib_math
82-
use iso_fortran_env
81+
use stdlib_kinds
8382
implicit none
84-
real(real32) :: x
85-
real(real32) :: xmin
86-
real(real32) :: xmax
87-
real(real32) :: clipped_value
83+
real(sp) :: x
84+
real(sp) :: xmin
85+
real(sp) :: xmax
86+
real(sp) :: clipped_value
8887
89-
xmin = -5.769_real32
88+
xmin = -5.769_sp
9089
! xmin <- -5.76900005
91-
xmax = 3.025_real32
90+
xmax = 3.025_sp
9291
! xmax <- 3.02500010
93-
x = 3.025_real32
92+
x = 3.025_sp
9493
! x <- 3.02500010
9594
9695
clipped_value = clip(x, xmin, xmax)
9796
! clipped_value <- 3.02500010
98-
end program demo
97+
end program demo_clip_real
9998
```

src/stdlib_math.fypp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#: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")]
52
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES
63

74
module stdlib_math
@@ -13,21 +10,21 @@ module stdlib_math
1310

1411
interface clip
1512
#:for k1, t1 in IR_KINDS_TYPES
16-
module procedure clip_${t1}$_${k1}$
13+
module procedure clip_${k1}$
1714
#:endfor
1815
end interface clip
1916

2017
contains
2118

2219
#:for k1, t1 in IR_KINDS_TYPES
23-
elemental function clip_${t1}$_${k1}$(x, xmin, xmax) result(res)
24-
${t1}$(${k1}$), intent(in) :: x
25-
${t1}$(${k1}$), intent(in) :: xmin
26-
${t1}$(${k1}$), intent(in) :: xmax
27-
${t1}$(${k1}$) :: res
20+
elemental function clip_${k1}$(x, xmin, xmax) result(res)
21+
${t1}$, intent(in) :: x
22+
${t1}$, intent(in) :: xmin
23+
${t1}$, intent(in) :: xmax
24+
${t1}$ :: res
25+
2826
res = max(min(x, xmax), xmin)
29-
end function clip_${t1}$_${k1}$
27+
end function clip_${k1}$
3028

3129
#:endfor
32-
3330
end module stdlib_math

src/tests/math/test_math.f90

Lines changed: 91 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
! SPDX-Identifier: MIT
22

3-
43
module test_math
54
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp
65
use stdlib_error, only : check
76
use stdlib_math
87

98
implicit none
109

11-
contains
12-
subroutine test_clip_integer_int8(x, xmin, xmax, compare)
13-
integer(int8), intent(in) :: x, xmin, xmax, compare
10+
contains
1411

15-
call check(clip(x, xmin, xmax) == compare)
16-
17-
end subroutine test_clip_integer_int8
12+
subroutine test_clip_int8(x, xmin, xmax, compare)
13+
integer(int8), intent(in) :: x, xmin, xmax, compare
1814

19-
subroutine test_clip_integer_int16(x, xmin, xmax, compare)
20-
integer(int16), intent(in) :: x, xmin, xmax, compare
15+
call check(clip(x, xmin, xmax) == compare)
16+
17+
end subroutine test_clip_int8
2118

22-
call check(clip(x, xmin, xmax) == compare)
23-
24-
end subroutine test_clip_integer_int16
19+
subroutine test_clip_int16(x, xmin, xmax, compare)
20+
integer(int16), intent(in) :: x, xmin, xmax, compare
2521

26-
subroutine test_clip_integer_int32(x, xmin, xmax, compare)
27-
integer(int32), intent(in) :: x, xmin, xmax, compare
22+
call check(clip(x, xmin, xmax) == compare)
23+
24+
end subroutine test_clip_int16
2825

29-
call check(clip(x, xmin, xmax) == compare)
30-
31-
end subroutine test_clip_integer_int32
26+
subroutine test_clip_int32(x, xmin, xmax, compare)
27+
integer(int32), intent(in) :: x, xmin, xmax, compare
3228

33-
subroutine test_clip_integer_int64(x, xmin, xmax, compare)
34-
integer(int64), intent(in) :: x, xmin, xmax, compare
29+
call check(clip(x, xmin, xmax) == compare)
30+
31+
end subroutine test_clip_int32
3532

36-
call check(clip(x, xmin, xmax) == compare)
37-
38-
end subroutine test_clip_integer_int64
33+
subroutine test_clip_int64(x, xmin, xmax, compare)
34+
integer(int64), intent(in) :: x, xmin, xmax, compare
3935

40-
subroutine test_clip_real_sp(x, xmin, xmax, compare)
41-
real(sp), intent(in) :: x, xmin, xmax, compare
36+
call check(clip(x, xmin, xmax) == compare)
37+
38+
end subroutine test_clip_int64
4239

43-
call check(clip(x, xmin, xmax) == compare)
44-
45-
end subroutine test_clip_real_sp
40+
subroutine test_clip_sp(x, xmin, xmax, compare)
41+
real(sp), intent(in) :: x, xmin, xmax, compare
4642

47-
subroutine test_clip_real_dp(x, xmin, xmax, compare)
48-
real(dp), intent(in) :: x, xmin, xmax, compare
43+
call check(clip(x, xmin, xmax) == compare)
44+
45+
end subroutine test_clip_sp
4946

50-
call check(clip(x, xmin, xmax) == compare)
51-
52-
end subroutine test_clip_real_dp
47+
subroutine test_clip_dp(x, xmin, xmax, compare)
48+
real(dp), intent(in) :: x, xmin, xmax, compare
5349

54-
subroutine test_clip_real_qp(x, xmin, xmax, compare)
55-
real(qp), intent(in) :: x, xmin, xmax, compare
50+
call check(clip(x, xmin, xmax) == compare)
51+
52+
end subroutine test_clip_dp
5653

57-
call check(clip(x, xmin, xmax) == compare)
58-
59-
end subroutine test_clip_real_qp
54+
subroutine test_clip_qp(x, xmin, xmax, compare)
55+
real(qp), intent(in) :: x, xmin, xmax, compare
6056

57+
call check(clip(x, xmin, xmax) == compare)
58+
59+
end subroutine test_clip_qp
6160

6261
end module test_math
6362

@@ -71,62 +70,60 @@ program tester
7170
! invalid case: xmin is greater than xmax
7271

7372

74-
75-
! type: integer, kind: int8
76-
! valid test cases
77-
call test_clip_integer_int8(2_int8, -2_int8, 5_int8, 2_int8)
78-
call test_clip_integer_int8(127_int8, -127_int8, 0_int8, 0_int8)
79-
80-
! invalid test cases
81-
call test_clip_integer_int8(2_int8, 5_int8, -2_int8, 5_int8)
82-
call test_clip_integer_int8(127_int8, 0_int8, -127_int8, 0_int8)
83-
! type: integer, kind: int16
84-
! valid test cases
85-
call test_clip_integer_int16(2_int16, -2_int16, 5_int16, 2_int16)
86-
call test_clip_integer_int16(32767_int16, -32767_int16, 0_int16, 0_int16)
87-
88-
! invalid test cases
89-
call test_clip_integer_int16(2_int16, 5_int16, -2_int16, 5_int16)
90-
call test_clip_integer_int16(32767_int16, 0_int16, -32767_int16, 0_int16)
91-
! type: integer, kind: int32
92-
! valid test cases
93-
call test_clip_integer_int32(2_int32, -2_int32, 5_int32, 2_int32)
94-
call test_clip_integer_int32(-2147483647_int32, 0_int32, 2147483647_int32, 0_int32)
95-
96-
! invalid test cases
97-
call test_clip_integer_int32(2_int32, 5_int32, -2_int32, 5_int32)
98-
call test_clip_integer_int32(-2147483647_int32, 2147483647_int32, 0_int32, 2147483647_int32)
99-
! type: integer, kind: int64
100-
! valid test cases
101-
call test_clip_integer_int64(2_int64, -2_int64, 5_int64, 2_int64)
102-
call test_clip_integer_int64(-922337203_int64, -10_int64, 25_int64, -10_int64)
103-
104-
! invalid test cases
105-
call test_clip_integer_int64(2_int64, 5_int64, -2_int64, 5_int64)
106-
call test_clip_integer_int64(-922337203_int64, 25_int64, -10_int64, 25_int64)
107-
! type: real, kind: sp
108-
! valid test cases
109-
call test_clip_real_sp(3.025_sp, -5.77_sp, 3.025_sp, 3.025_sp)
110-
call test_clip_real_sp(0.0_sp, -1578.025_sp, -59.68_sp, -59.68_sp)
111-
112-
! invalid test cases
113-
call test_clip_real_sp(3.025_sp, 3.025_sp, -5.77_sp, 3.025_sp)
114-
call test_clip_real_sp(0.0_sp, -59.68_sp, -1578.025_sp, -59.68_sp)
115-
! type: real, kind: dp
116-
! valid test cases
117-
call test_clip_real_dp(3.025_dp, -5.77_dp, 3.025_dp, 3.025_dp)
118-
call test_clip_real_dp(-7.0_dp, 0.059668_dp, 1.00268_dp, 0.059668_dp)
119-
120-
! invalid test cases
121-
call test_clip_real_dp(3.025_dp, 3.025_dp, -5.77_dp, 3.025_dp)
122-
call test_clip_real_dp(-7.0_dp, 1.00268_dp, 0.059668_dp, 1.00268_dp)
123-
! type: real, kind: qp
124-
! valid test cases
125-
call test_clip_real_qp(3.025_qp, -5.77_qp, 3.025_qp, 3.025_qp)
126-
call test_clip_real_qp(-55891546.2_qp, -8958133457.23_qp, -689712245.23_qp, -689712245.23_qp)
127-
128-
! invalid test cases
129-
call test_clip_real_qp(3.025_qp, 3.025_qp, -5.77_qp, 3.025_qp)
130-
call test_clip_real_qp(-55891546.2_qp, -689712245.23_qp, -8958133457.23_qp, -689712245.23_qp)
73+
! type: integer(int8), kind: int8
74+
! valid test cases
75+
call test_clip_int8(2_int8, -2_int8, 5_int8, 2_int8)
76+
call test_clip_int8(127_int8, -127_int8, 0_int8, 0_int8)
77+
! invalid test cases
78+
call test_clip_int8(2_int8, 5_int8, -2_int8, 5_int8)
79+
call test_clip_int8(127_int8, 0_int8, -127_int8, 0_int8)
80+
81+
! type: integer(int16), kind: int16
82+
! valid test cases
83+
call test_clip_int16(2_int16, -2_int16, 5_int16, 2_int16)
84+
call test_clip_int16(32767_int16, -32767_int16, 0_int16, 0_int16)
85+
! invalid test cases
86+
call test_clip_int16(2_int16, 5_int16, -2_int16, 5_int16)
87+
call test_clip_int16(32767_int16, 0_int16, -32767_int16, 0_int16)
88+
89+
! type: integer(int32), kind: int32
90+
! valid test cases
91+
call test_clip_int32(2_int32, -2_int32, 5_int32, 2_int32)
92+
call test_clip_int32(-2147483647_int32, 0_int32, 2147483647_int32, 0_int32)
93+
! invalid test cases
94+
call test_clip_int32(2_int32, 5_int32, -2_int32, 5_int32)
95+
call test_clip_int32(-2147483647_int32, 2147483647_int32, 0_int32, 2147483647_int32)
96+
97+
! type: integer(int64), kind: int64
98+
! valid test cases
99+
call test_clip_int64(2_int64, -2_int64, 5_int64, 2_int64)
100+
call test_clip_int64(-922337203_int64, -10_int64, 25_int64, -10_int64)
101+
! invalid test cases
102+
call test_clip_int64(2_int64, 5_int64, -2_int64, 5_int64)
103+
call test_clip_int64(-922337203_int64, 25_int64, -10_int64, 25_int64)
104+
105+
! type: real(sp), kind: sp
106+
! valid test cases
107+
call test_clip_sp(3.025_sp, -5.77_sp, 3.025_sp, 3.025_sp)
108+
call test_clip_sp(0.0_sp, -1578.025_sp, -59.68_sp, -59.68_sp)
109+
! invalid test cases
110+
call test_clip_sp(3.025_sp, 3.025_sp, -5.77_sp, 3.025_sp)
111+
call test_clip_sp(0.0_sp, -59.68_sp, -1578.025_sp, -59.68_sp)
112+
113+
! type: real(dp), kind: dp
114+
! valid test cases
115+
call test_clip_dp(3.025_dp, -5.77_dp, 3.025_dp, 3.025_dp)
116+
call test_clip_dp(-7.0_dp, 0.059668_dp, 1.00268_dp, 0.059668_dp)
117+
! invalid test cases
118+
call test_clip_dp(3.025_dp, 3.025_dp, -5.77_dp, 3.025_dp)
119+
call test_clip_dp(-7.0_dp, 1.00268_dp, 0.059668_dp, 1.00268_dp)
120+
121+
! type: real(qp), kind: qp
122+
! valid test cases
123+
call test_clip_qp(3.025_qp, -5.77_qp, 3.025_qp, 3.025_qp)
124+
call test_clip_qp(-55891546.2_qp, -8958133457.23_qp, -689712245.23_qp, -689712245.23_qp)
125+
! invalid test cases
126+
call test_clip_qp(3.025_qp, 3.025_qp, -5.77_qp, 3.025_qp)
127+
call test_clip_qp(-55891546.2_qp, -689712245.23_qp, -8958133457.23_qp, -689712245.23_qp)
131128

132129
end program tester

0 commit comments

Comments
 (0)