R语言 计算一个数字的平方根 – sqrt()函数
R语言中的 sqrt() 函数是用来计算作为参数传递给它的数值的数学平方根的。
语法: sqrt(x)
参数:
x: 任何大于0的数值
例1 :
# R code to calculate square root of a number
x1 <- 16
x2 <- 18
# Using sqrt() Function
sqrt(x1)
sqrt(x2)
输出
[1] 4
[1] 4.242641
例2 :
# R code to calculate square root of a number
x1 <- 8.34526
x2 <- -18
# Using sqrt() Function
sqrt(x1)
sqrt(x2)
输出
[1] 2.888816
[1] NaN
Warning message:
In sqrt(x2) : NaNs produced
在这里,在上述代码中,产生NaN是因为我们在sqrt()函数中传递了一个负值。