Skip to content

Commit f3c9deb

Browse files
committed
fix: use precision-aware default tolerance in f_approx_equal
Single precision (wp=real(4)) has ~7 decimal digits of precision, so a default tolerance of 1e-10 is below machine epsilon (~1.2e-7) and f_approx_equal would almost never return true. Use 1e-6 for single precision builds and keep 1e-10 for double precision. The wp == single_precision comparison uses integer parameter constants that the compiler resolves at compile time, so there is no runtime cost.
1 parent 3b201e3 commit f3c9deb

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/common/m_helper_basic.fpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
module m_helper_basic
99

1010
use m_derived_types
11+
use m_precision_select
1112

1213
implicit none
1314

@@ -29,7 +30,11 @@ contains
2930
if (present(tol_input)) then
3031
tol = tol_input
3132
else
32-
tol = 1.e-10_wp
33+
if (wp == single_precision) then
34+
tol = 1.e-6_wp
35+
else
36+
tol = 1.e-10_wp
37+
end if
3338
end if
3439

3540
if (a == b) then
@@ -58,7 +63,11 @@ contains
5863
if (present(tol_input)) then
5964
tol = tol_input
6065
else
61-
tol = 1e-10_wp
66+
if (wp == single_precision) then
67+
tol = 1.e-6_wp
68+
else
69+
tol = 1.e-10_wp
70+
end if
6271
end if
6372

6473
do i = 1, size(b)

0 commit comments

Comments
 (0)