Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
math
tangent
communitycreator

What is Math.tanh() in Scala?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The tanh() function returns the hyperbolic tangent of a number. To be more specific, it returns the hyperbolic tangent of a number in radians.

Figure 1 shows the mathematical representation of the tanh() function.

Figure 1: Mathematical representation of the hyperbolic tangent function

The scala.math._ header file is required for this function.

Syntax

Double tanh(Double number)

Parameter

This function requires a number that represents an angle in radians as a parameter.

You can use the following formula to convert degrees to radians.

radians = degrees * ( Pi / 180 )

Return value

tanh() returns the hyperbolic tangent of a numberin radians that is sent as a parameter.

  • If the parameter value is positive infinity, then it returns 1.
  • If the parameter value is negative infinity, then it returns -1.
  • If the parameter value is NaN, then it returns NaN.

Example

import scala.math._
object Main extends App {
//positive number in radians
println(s"The value of tanh(2.3) = ${tanh(2.3)}");
// negative number in radians
println(s"The value of tanh(-2.3) = ${tanh(-2.3)}");
//converting the degrees angle into radians and then applying tanh()
// degrees = 45.0
// PI = 3.14159265
// result first converts degrees to radians then apply tanh
println(s"The value of tanh(45.0) = ${tanh(45.0 * (Pi / 180.0))}");
//error outputs
println(s"The value of tanh(Double.PositiveInfinity) = ${tanh(Double.PositiveInfinity)}");
println(s"The value of tanh(Double.NegativeInfinity) = ${tanh(Double.NegativeInfinity)}");
println(s"The value of tanh(Double.NaN) = ${tanh(Double.NaN)}");
}

RELATED TAGS

scala
math
tangent
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring