Scala Tutorial Scala References

Scala - Math acos() Method



The Scala Math acos() method returns arc cosine of a value. The returned value will be in the range 0 through 𝜋. In special cases it returns the following:

  • If the argument is NaN or its absolute value is greater than 1, then the result is NaN.

Note: acos() is the inverse of cos().

Syntax

def acos(x: Double): Double = scala.lang.Math.acos(x)

Parameters

x Specify the value.

Return Value

Returns the arc cosine of the value.

Exception

NA.

Example:

In the example below, acos() method is used to find out the arc cosine of a given value.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"acos(-1) = ${acos(-1)}");   
    println(s"acos(0.5) = ${acos(0.5)}"); 
    println(s"acos(0) = ${acos(0)}"); 
    println(s"acos(0.5) = ${acos(0.5)}");
    println(s"acos(1) = ${acos(1)}");
    println(s"acos(Double.NaN) = ${acos(Double.NaN)}"); 
    println(s"acos(2) = ${acos(2)}");
    println("acos(Double.PositiveInfinity) = "
            + acos(Double.PositiveInfinity));
  }
}

The output of the above code will be:

acos(-1) = 3.141592653589793
acos(0.5) = 1.0471975511965979
acos(0) = 1.5707963267948966
acos(0.5) = 1.0471975511965979
acos(1) = 0.0
acos(Double.NaN) = NaN
acos(2) = NaN
acos(Double.PositiveInfinity) = NaN

❮ Scala - Math Methods