Scala Tutorial Scala References

Scala - Math negateExact() Method



The Scala Math negateExact() method returns the negation of the argument. The method can be overloaded and it can take Int and Long arguments. It throws an exception, if the result overflows an Int or a Long.

Syntax

def negateExact(x: Int): Int = java.lang.Math.negateExact(x)
def negateExact(x: Long): Long = java.lang.Math.negateExact(x)

Parameters

x Specify the value.

Return Value

Returns the negation of the argument.

Exception

Throws ArithmeticException, if the result overflows an Int or a Long.

Example:

In the example below, Math.negateExact() method is used to negate the argument.

object MainObject {
  def main(args: Array[String]) {
    var x1 : Int = 10;
    var x2 : Long = 100;
    println("Math.negateExact(x1) = "
            + Math.negateExact(x1)); 
    println("Math.negateExact(x2) = "
            + Math.negateExact(x2)); 
  }
}

The output of the above code will be:

Math.negateExact(x1) = -10
Math.negateExact(x2) = -100

❮ Scala - Math Methods

5