Skip to content

Commit

Permalink
improve checker messages - simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisZYJ committed Jun 24, 2024
1 parent fc85a76 commit 72b2712
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 196 deletions.
33 changes: 32 additions & 1 deletion src/common/m_helper.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module m_helper
f_create_bbox, &
s_print_2D_array, &
f_xor, &
f_logical_to_int
f_logical_to_int, &
f_approx_equal

contains

Expand Down Expand Up @@ -532,4 +533,34 @@ contains
end if
end function f_logical_to_int

!> This procedure checks if two floating point numbers of kind(0d0) are within tolerance.
!! @param a First number.
!! @param b Second number.
!! @param tol_input Relative error (default = 1d-6).
!! @return Result of the comparison.
logical function f_approx_equal(a, b, tol_input) result(res)
! Reference: https://floating-point-gui.de/errors/comparison/

real(kind(0d0)), intent(in) :: a, b
real(kind(0d0)), optional, intent(in) :: tol_input
real(kind(0d0)) :: tol

if (present(tol_input)) then
if (tol_input <= 0d0) then
call s_mpi_abort('tol_input must be positive. Exiting ...')
end if
tol = tol_input
else
tol = 1d-6
end if

if (a == b) then
res = .true.
else if (a == 0d0 .or. b == 0d0 .or. (abs(a) + abs(b) < tiny(a))) then
res = (abs(a - b) < (tol*tiny(a)))
else
res = (abs(a - b)/min(abs(a) + abs(b), huge(a)) < tol)
end if
end function f_approx_equal

end module m_helper
Loading

0 comments on commit 72b2712

Please sign in to comment.