Skip to content

Commit d428ad8

Browse files
committed
Add appropriate tolerance level
Demand a lower tolerance value for higher precision tests
1 parent efa82b2 commit d428ad8

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/tests/math/test_linspace.f90

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ program test_linspace
77

88
integer :: iunit
99
logical :: warn = .false.
10-
real(dp), parameter :: TOLERANCE = 0.00001 ! Percentage of the range for which the actual gap must not exceed
10+
real(sp), parameter :: TOLERANCE_SP = 1000 * epsilon(1.0_sp)
11+
real(dp), parameter :: TOLERANCE_DP = 1000 * epsilon(1.0_dp) ! Percentage of the range for which the actual gap must not exceed
1112

1213
! Testing linspace.
1314
!
@@ -63,7 +64,7 @@ subroutine test_linspace_sp
6364
do i = 1, n-1
6465

6566
true_difference = x(i + 1) - x(i)
66-
call check(abs(true_difference - expected_interval) < abs(expected_interval) * TOLERANCE)
67+
call check(abs(true_difference - expected_interval) < abs(expected_interval) * TOLERANCE_SP)
6768

6869
end do
6970

@@ -92,7 +93,7 @@ subroutine test_linspace_dp
9293
do i = 1, n-1
9394

9495
true_difference = x(i + 1) - x(i)
95-
call check(abs(true_difference - expected_interval) < abs(expected_interval) * TOLERANCE)
96+
call check(abs(true_difference - expected_interval) < abs(expected_interval) * TOLERANCE_DP)
9697

9798
end do
9899

@@ -133,7 +134,7 @@ subroutine test_linspace_cmplx
133134
! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
134135
do i = 1, n-1
135136

136-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
137+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_DP)
137138

138139
end do
139140

@@ -172,7 +173,7 @@ subroutine test_linspace_cmplx_2
172173
! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
173174
do i = 1, n-1
174175

175-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
176+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_DP)
176177

177178
end do
178179

@@ -211,7 +212,7 @@ subroutine test_linspace_cmplx_3
211212
! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
212213
do i = 1, n-1
213214

214-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
215+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_DP)
215216

216217
end do
217218

@@ -250,7 +251,7 @@ subroutine test_linspace_cmplx_sp
250251
! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
251252
do i = 1, n-1
252253

253-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
254+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_SP)
254255

255256
end do
256257

@@ -293,7 +294,7 @@ subroutine test_linspace_cmplx_sp_2
293294

294295
true_interval = (z(i + 1) - z(i))
295296
offset = abs(true_interval - expected_interval)
296-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
297+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_SP)
297298
! print *, i
298299

299300
end do
@@ -333,7 +334,7 @@ subroutine test_linspace_int16
333334
! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
334335
do i = 1, n-1
335336

336-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
337+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_DP)
337338

338339
end do
339340

@@ -375,7 +376,7 @@ subroutine test_linspace_int8
375376
! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
376377
do i = 1, n-1
377378

378-
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE)
379+
call check(abs( ( z(i + 1) - z(i) ) - expected_interval) < abs(expected_interval) * TOLERANCE_DP)
379380

380381
end do
381382

src/tests/math/test_logspace.f90

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ program test_logspace
1919
! Also test to verify that the proportion between adjacent elements is constant within
2020
! a certain tolerance
2121

22-
real(dp), parameter :: TOLERANCE = 0.00001
22+
real(sp), parameter :: TOLERANCE_SP = 1000 * epsilon(1.0_sp)
23+
real(dp), parameter :: TOLERANCE_DP = 1000 * epsilon(1.0_dp) ! Percentage of the range for which the actual gap must not exceed
2324

2425
open(newunit=iunit, file="test_logspace_log.txt", status="unknown") ! Log the results of the function
2526

@@ -57,7 +58,7 @@ subroutine test_logspace_sp
5758

5859
do i = 1, n-1
5960

60-
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE)
61+
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE_SP)
6162

6263
end do
6364

@@ -93,7 +94,7 @@ subroutine test_logspace_dp
9394

9495
do i = 1, n-1
9596

96-
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE)
97+
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE_DP)
9798

9899
end do
99100

@@ -128,7 +129,7 @@ subroutine test_logspace_default
128129

129130
do i = 1, n-1
130131

131-
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE)
132+
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE_DP)
132133

133134
end do
134135

@@ -163,7 +164,7 @@ subroutine test_logspace_base_2
163164

164165
do i = 1, n-1
165166

166-
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE)
167+
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE_DP)
167168

168169
end do
169170

@@ -199,7 +200,7 @@ subroutine test_logspace_base_2_cmplx_start
199200

200201
do i = 1, n-1
201202

202-
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE)
203+
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE_DP)
203204

204205
end do
205206

@@ -234,7 +235,7 @@ subroutine test_logspace_base_i_int_start
234235

235236
do i = 1, n-1
236237

237-
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE)
238+
call check(abs(x(i + 1) / x(i) - expected_proportion) < abs(expected_proportion) * TOLERANCE_DP)
238239

239240
end do
240241

0 commit comments

Comments
 (0)