Scala Tutorial Scala References

Scala - Math floorMod() Method



The Scala Math floorMod() method returns floor modulus of the arguments. The floor modulus of two arguments is same as modulo of two arguments. The method can be overloaded and it can take Int and Long arguments. In special cases it returns the following:

  • If the second argument is zero, this method throws ArithmeticException.

Syntax

def floorMod(x: Int, y: Int): Int = java.lang.Math.floorMod(x, y)
def floorMod(x: Long, y: Long): Long = java.lang.Math.floorMod(x, y)

Parameters

x Specify the dividend.
y Specify the divisor.

Return Value

Returns the floor modulus of the arguments.

Exception

Throws ArithmeticException, if the divisor y is zero.

Example:

In the example below, Math.floorMod() method returns the floor modulus (remainder) of two arguments.

object MainObject {
  def main(args: Array[String]) {
    println(s"Math.floorMod(100, 10) = ${Math.floorMod(100, 10)}"); 
    println(s"Math.floorMod(121, 11) = ${Math.floorMod(121, 11)}"); 
    println(s"Math.floorMod(10, 7) = ${Math.floorMod(10, 7)}"); 
    println(s"Math.floorMod(50, 7) = ${Math.floorMod(50, 7)}"); 
    println(s"Math.floorMod(-50, 7) = ${Math.floorMod(-50, 7)}"); 
    println(s"Math.floorMod(50, 7) = ${Math.floorMod(50, -7)}"); 
    println(s"Math.floorMod(-50, -7) = ${Math.floorMod(-50, -7)}"); 
  }
}

The output of the above code will be:

Math.floorMod(100, 10) = 0
Math.floorMod(121, 11) = 0
Math.floorMod(10, 7) = 3
Math.floorMod(50, 7) = 1
Math.floorMod(-50, 7) = 6
Math.floorMod(50, 7) = -6
Math.floorMod(-50, -7) = -1

❮ Scala - Math Methods