Scala - Math signum() Method
The Scala Math signum() method returns the sign of a given value. It returns zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. The method can be overloaded and it can take Int, Long, Float and Double arguments. In special cases it returns the following:
- If the argument is NaN, then the result is NaN.
Syntax
def signum(x: Int): Int = java.lang.Integer.signum(x) def signum(x: Long): Long = java.lang.Long.signum(x) def signum(x: Float): Float = java.lang.Math.signum(x) def signum(x: Double): Double = java.lang.Math.signum(x)
Parameters
x |
Specify a value whose signum is to be returned. |
Return Value
Returns zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
Exception
NA.
Example:
In the example below, signum() method returns the sign of a given value.
import scala.math._ object MainObject { def main(args: Array[String]) { println(s"signum(-10.3) = ${signum(-10.3)}"); println(s"signum(5.7) = ${signum(5.7)}"); println(s"signum(0) = ${signum(0)}"); println(s"signum(Double.NaN) = ${signum(Double.NaN)}"); println("signum(Double.PositiveInfinity) = " + signum(Double.PositiveInfinity)); println("signum(Double.NegativeInfinity) = " + signum(Double.NegativeInfinity)); } }
The output of the above code will be:
signum(-10.3) = -1.0 signum(5.7) = 1.0 signum(0) = 0 signum(Double.NaN) = NaN signum(Double.PositiveInfinity) = 1.0 signum(Double.NegativeInfinity) = -1.0
❮ Scala - Math Methods