The tan()
function returns the tangent of a number in radians
.
The following illustration shows the mathematical representation of the tan()
function.
The
scala.math._
header file is required for this function.
This
tan()
function only works for right-angled triangles.
def tan(x: Double): Double
This function requires a number
that represents an angle
in radians
as a parameter.
To convert
degrees
toradians
, you can use the following formula.
radians = degrees * ( Pi / 180 )
tan()
returns the tangent of a number in radians
that is sent as a parameter.
If the parameter value is
NaN
orPositiveInfinity
orNegativeInfinity
, then it returnsNaN
.
import scala.math._object Main extends App {//positive number in radiansprintln(s"The value of tan(2.3) = ${tan(2.3)}");// negative number in radiansprintln(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 tanprintln(s"The value of tan(45.0) = ${tan(45.0 * (Pi / 180.0))}");//error outputsprintln(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)}");}