What is Math.asin() in Scala?

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

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

Mathematical representation of the inverse sine 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 asin(Double number)

Parameters

This function requires a number as a parameter. The parameter must be a double value between -1 and 1.

  • -1 <= parameter <= 1.

Return value

asin() returns the inverse sine 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 an error value, i.e., greater than one or less than one, infinity, or NaN, then asin() returns NaN.

Example

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

Free Resources