R Tutorial R Charts & Graphs R Statistics R References

R - tanh() Function



The R tanh() function returns hyperbolic tangent of a value. The hyperbolic tangent of x is defined as:

tanh

where e is an Euler's number.

In special cases it returns the following:

  • If the argument is NaN, then the result is NaN.
  • If the argument is zero, then the result is a zero.
  • If the argument is positive infinity, then the result is +1.0.
  • If the argument is negative infinity, then the result is -1.0.

Syntax

tanh(x)

Parameters

x Required. Specify column to compute on.

Return Value

Returns the hyperbolic tangent of the value.

Example:

In the example below, tanh() function is used to find out the hyperbolic tangent of the value.

#operating on single element atomic vector
print(tanh(-5))
print(tanh(0))
print(tanh(5))

cat("\nOperating on vector\n")
#operating on vector
v <- c(0, 3, 5)
print(tanh(v))

cat("\nOperating on matrix\n")
#operating on matrix
m <- matrix(c(-5, -2, 0, 2, 5, NaN), nrow=2)
print(tanh(m))

cat("\nOperating on first column of matrix\n")
#operating on first column of matrix
print(tanh(m[,1]))

The output of the above code will be:

[1] -0.9999092
[1] 0
[1] 0.9999092

Operating on vector
[1] 0.0000000 0.9950548 0.9999092

Operating on matrix
           [,1]      [,2]      [,3]
[1,] -0.9999092 0.0000000 0.9999092
[2,] -0.9640276 0.9640276       NaN

Operating on first column of matrix
[1] -0.9999092 -0.9640276

❮ R Math Functions