Real cube root of a negative number

前端 未结 3 2019
长情又很酷
长情又很酷 2020-12-03 21:10

I\'m trying to see if there is a function to directly get the real cube root of a negative number. For example, in Java, there is the Math.cbrt() function. I\'m

3条回答
  •  眼角桃花
    2020-12-03 21:45

    Sounds like you just need to define your own Math.cbrt() function.

    That will turn performing the operation from something inelegant and cumbersome to something clean, expressive, and easy to apply:

    Math.cbrt <- function(x) {
        sign(x) * abs(x)^(1/3)
    }
    
    x <- c(-1, -8, -27, -64)
    
    Math.cbrt(x)
    # [1] -1 -2 -3 -4
    

提交回复
热议问题