What is Math.tan() in Scala?

The tan() function returns the tangent of a number in radians.

The following illustration shows the mathematical representation of the tan() function.

Figure 1: Mathematical representation of tangent function

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

This tan() function only works for right-angled triangles.

Syntax

def tan(x: Double): Double

Parameter

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

To convert degrees to radians, you can use the following formula.

radians = degrees * ( Pi / 180 )

Return value

tan() returns the tangent of a number in radians that is sent as a parameter.

If the parameter value is NaN or PositiveInfinity or NegativeInfinity, then it returns NaN.

Code

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

Free Resources