A wrapper to sqrt which replaces -ve values with 0.0 to avoid NaNs arising from slight floating point discrepancies. We could consider adding a debug check to abort/warn if the passed value is too negative (i.e. if it looks like an error rather than small round off).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(in) | :: | arg |
elemental real function safe_sqrt(arg)
implicit none
real, intent(in) :: arg
safe_sqrt = sqrt(max(0.0, arg))
end function safe_sqrt