https://github.com/cran/pracma
Raw File
Tip revision: d50cda4811eb3cdb8d2ce9e01ddb5e07d4a8c24f authored by HwB on 11 September 2013, 00:00:00 UTC
version 1.5.5
Tip revision: d50cda4
nthroot.R
###
### NTHROOT.R - Compute the real n-th root
###


nthroot <- function(x, n) {
    if (! is.numeric(x))
        stop("Argument 'x' must be numeric.")
    if (missing(n) || n <= 0 || ceiling(n) != floor(n))
        stop("Argument 'n' must be a positive integer.")
    if (any(x[!is.na(x)] < 0) && n %% 2 == 0)
        stop("If argument 'x' is negative, 'n' must be an odd integer.")

    sx <- sign(x)
    return(sx * (sx * x)^(1/n))
}
back to top