@@ -36,7 +36,10 @@ module m_helper
3636 f_create_bbox, &
3737 s_print_2D_array, &
3838 f_xor, &
39- f_logical_to_int
39+ f_logical_to_int, &
40+ f_approx_equal, &
41+ f_is_default, &
42+ f_all_default
4043
4144contains
4245
@@ -532,4 +535,56 @@ contains
532535 end if
533536 end function f_logical_to_int
534537
538+ !> This procedure checks if two floating point numbers of kind (0d0 ) are within tolerance.
539+ !! @param a First number.
540+ !! @param b Second number.
541+ !! @param tol_input Relative error (default = 1d-6 ).
542+ !! @return Result of the comparison.
543+ logical function f_approx_equal (a , b , tol_input ) result(res)
544+ ! Reference: https:// floating- point- gui.de/ errors/ comparison/
545+
546+ real (kind (0d0 )), intent (in ) :: a, b
547+ real (kind (0d0 )), optional, intent (in ) :: tol_input
548+ real (kind (0d0 )) :: tol
549+
550+ if (present (tol_input)) then
551+ if (tol_input <= 0d0 ) then
552+ call s_mpi_abort(' tol_input must be positive. Exiting ...' )
553+ end if
554+ tol = tol_input
555+ else
556+ tol = 1d-6
557+ end if
558+
559+ if (a == b) then
560+ res = .true.
561+ else if (a == 0d0 .or. b == 0d0 .or. (abs (a) + abs (b) < tiny (a))) then
562+ res = (abs (a - b) < (tol* tiny (a)))
563+ else
564+ res = (abs (a - b)/ min (abs (a) + abs (b), huge (a)) < tol)
565+ end if
566+ end function f_approx_equal
567+
568+ !> Checks if a real (kind (0d0 )) variable is of default value.
569+ !! @param var Variable to check.
570+ logical function f_is_default (var ) result(res)
571+ real (kind (0d0 )), intent (in ) :: var
572+
573+ res = f_approx_equal(var, dflt_real)
574+ end function f_is_default
575+
576+ !> Checks if ALL elements of a real (kind (0d0 )) array are of default value.
577+ !! @param var_array Array to check.
578+ logical function f_all_default (var_array ) result(res)
579+ real (kind (0d0 )), intent (in ) :: var_array(:)
580+ logical :: res_array(size (var_array))
581+ integer :: i
582+
583+ do i = 1 , size (var_array)
584+ res_array(i) = f_is_default(var_array(i))
585+ end do
586+
587+ res = all (res_array)
588+ end function f_all_default
589+
535590end module m_helper
0 commit comments