Scala Tutorial Scala References

Scala - Math min() Method



The Scala Math min() method returns minimum number between the two arguments. The method can be overloaded and it can take Int, Double, Float and Long arguments. If either of the arguments is NaN, then the result is NaN.

Syntax

def min(x: Int, y: Int): Int = java.lang.Math.min(x, y)
def min(x: Long, y: Long): Long = java.lang.Math.min(x, y)
def min(x: Float, y: Float): Float = java.lang.Math.min(x, y)
def min(x: Double, y: Double): Double = java.lang.Math.min(x, y)

Parameters

x Specify value to compare.
y Specify value to compare.

Return Value

Returns the numerically minimum value.

Exception

NA.

Example:

In the example below, min() method is used to find out the minimum value between the two arguments.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"min(10, 20) = ${min(10, 20)}");   
    println(s"min(10.5, 5.5) = ${min(10.5, 5.5)}"); 
    println(s"min(10, Double.NaN) = ${min(10, Double.NaN)}");  
    println("min(10.5, Double.PositiveInfinity) = "
            + min(10.5, Double.PositiveInfinity));
    println("min(10.5, Double.NegativeInfinity) = "
            + min(10.5, Double.NegativeInfinity));
  }
}

The output of the above code will be:

min(10, 20) = 10
min(10.5, 5.5) = 5.5
min(10, Double.NaN) = NaN
min(10.5, Double.PositiveInfinity) = 10.5
min(10.5, Double.NegativeInfinity) = -Infinity

❮ Scala - Math Methods