What is Math.toDegrees() in Scala?

The toDegrees() function in Scala converts a radian value to degrees.

Figure 1 below shows a mathematical representation of the toDegrees() function:

Figure 1: Mathematical representation of toDegrees() function

Following module is required for this function:

import scala.math._

Syntax


Double toDegrees(Double number)

Parameter

The toDegrees() function takes a numberradian value as a parameter.

Return value

The toDegrees() function then converts the radian value sent as a parameter to degrees.

  • If the parameter value is positive infinity, then it returns positive infinity.
  • If the parameter value is negative infinity, then it returns negative infinity.
  • If the parameter value is NaN, then it returns NaN.

Code

The following code shows how to use toDegrees() in scala.

import scala.math._
object Main extends App {
//pi
println(s"The value of toDegrees(Pi) = ${toDegrees(Pi)}");
//pi/2
println(s"The value of toDegrees(Pi/2) = ${toDegrees(Pi/2)}");
//pi/4
println(s"The value of toDegrees(Pi/4) = ${toDegrees(Pi/4)}");
//0
println(s"The value of toDegrees(0) = ${toDegrees(0)}");
//-pi
println(s"The value of toDegrees(-Pi) = ${toDegrees(-Pi)}");
//-pi/2
println(s"The value of toDegrees(-Pi/2) = ${toDegrees(-Pi/2)}");
//-pi/4
println(s"The value of toDegrees(-Pi/4) = ${toDegrees(-Pi/4)}");
//error outputs
println(s"The value of toDegrees(Double.PositiveInfinity) = ${toDegrees(Double.PositiveInfinity)}");
println(s"The value of toDegrees(Double.NegativeInfinity) = ${toDegrees(Double.NegativeInfinity)}");
println(s"The value of toDegrees(Double.NaN) = ${toDegrees(Double.NaN)}");
}

Free Resources