What is Math.atan() in Scala?

The atan() function, also called the arc tangent function, returns the inverse tangent of a number. To be more specific, it returns the inverse tangent of a number in radians.

The image below shows the mathematical representation of the atan() function.

Mathematical representation of the inverse tangent function

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

To convert degrees to radians, use the formula below:

radians = degrees * ( Pi / 180 )

Syntax

Double atan(Double number)

Parameters

This function requires a number as a parameter.

Return value

atan() returns the inverse tangent of the number (in radians) that is sent as a parameter. The return value lies in the interval [-pi/2,pi/2] radians.

If the parameter value is NaN, then atan() returns NaN.

Example

import scala.math._
object Main extends App {
//positive number in radians
println(s"The value of atan(0.5) = ${atan(0.5)} Radians");
println(s"The value of atan(2) = ${atan(2)} Radians");
println(s"The value of atan(Double.PositiveInfinity) = ${atan(Double.PositiveInfinity)} Radians");
// negative number in radians
println(s"The value of atan(-0.5) = ${atan(-0.5)} Radians");
println(s"The value of atan(-2) = ${atan(-2)} Radians");
println(s"The value of atan(Double.NegativeInfinity) = ${atan(Double.NegativeInfinity)} Radians");
//applying atan() and then converting the result in radians to degrees
// radians = 1.0
// PI = 3.14159265
println(s"The value of atan(1) = ${atan(1) * (180 / Pi)} Degrees");
//error outputs
println(s"The value of atan(Double.NaN) = ${atan(Double.NaN)}");
}

Free Resources