Scala Tutorial Scala References

Scala - Math copySign() Method



The Scala Math copySign() method returns a number with magnitude of first argument and sign of second argument. The method can be overloaded and it can take Double and Float arguments.

Syntax

def copySign(magnitude: Double, sign: Double): Double = 
                    java.lang.Math.copySign(magnitude, sign)
def copySign(magnitude: Float, sign: Float): Float = 
                    java.lang.Math.copySign(magnitude, sign)

Parameters

magnitude Specify a value providing the magnitude of the result.
sign Specify a value providing the sign of the result.

Return Value

Returns a number with magnitude of first argument and sign of second argument.

Exception

NA.

Example:

In the example below, Math.copySign() method returns a number with magnitude of first argument and sign of second argument.

object MainObject {
  def main(args: Array[String]) {
    println(s"Math.copySign(-10.3, 5) = ${Math.copySign(-10.3, 5)}"); 
    println(s"Math.copySign(20, -5.7) = ${Math.copySign(20, -5.7)}"); 
    println("Math.copySign(-20, Double.PositiveInfinity) = "
            + Math.copySign(-20, Double.PositiveInfinity));
    println("Math.copySign(Double.NegativeInfinity, -20) = "
            + Math.copySign(Double.NegativeInfinity, -20));
  }
}

The output of the above code will be:

Math.copySign(-10.3, 5) = 10.3
Math.copySign(20, -5.7) = -20.0
Math.copySign(-20, Double.PositiveInfinity) = 20.0
Math.copySign(Double.NegativeInfinity, -20) = -Infinity

❮ Scala - Math Methods