Scala Tutorial Scala References

Scala - Math cos() Method



The Scala Math cos() method returns trigonometric cosine of an angle (angle should be in radians). In special cases it returns the following:

  • If the argument is NaN or an infinity, then the result is NaN.

In the graph below, cos(x) vs x is plotted.

Cos Function

Syntax

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

Parameters

x Specify the angle in radian.

Return Value

Returns the trigonometric cosine of the argument.

Exception

NA.

Example:

In the example below, cos() method is used to find out the trigonometric cosine of the given angles.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"cos(Pi/6) = ${cos(Pi/6)}");   
    println(s"cos(Pi/4) = ${cos(Pi/4)}"); 
    println(s"cos(Pi/3) = ${cos(Pi/3)}"); 
    println(s"cos(Pi/2) = ${cos(Pi/2)}"); 
    println(s"cos(Double.NaN) = ${cos(Double.NaN)}"); 
    println(s"cos(2.0/0.0) = ${cos(2.0/0.0)}");
    println("cos(Double.PositiveInfinity) = "
            + cos(Double.PositiveInfinity));
  }
}

The output of the above code will be:

cos(Pi/6) = 0.8660254037844387
cos(Pi/4) = 0.7071067811865476
cos(Pi/3) = 0.5000000000000001
cos(Pi/2) = 6.123233995736766E-17
cos(Double.NaN) = NaN
cos(2.0/0.0) = NaN
cos(Double.PositiveInfinity) = NaN

❮ Scala - Math Methods