Skip to contents

Computes sqrt(x) with protection against negative inputs. Negative values are clamped to min_val before taking the root.

Usage

safe_sqrt(x, min_val = 0, warn = TRUE, param_name = "value")

Arguments

x

Numeric value or vector

min_val

Minimum value before sqrt, default 0

warn

Logical; issue a warning when clamping occurs, default TRUE

param_name

Name used in warning messages

Value

sqrt(x), or sqrt(min_val) for negative values

Examples

if (FALSE) { # \dontrun{
safe_sqrt(4)   # 2
safe_sqrt(-1)  # 0 with warning
} # }