Scala Tutorial Scala References

Scala - Math hypot() Method



The Scala Math hypot() method returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2). In special cases it returns the following:

  • If either argument is infinity, then the result is positive infinity.
  • If either argument is NaN and neither argument is infinite, then the result is NaN.

Syntax

def hypot(x: Double, y: Double): Double = java.lang.Math.hypot(x, y)

Parameters

x Specify a value.
y Specify a value.

Return Value

Returns sqrt(x2 +y2).

Exception

NA.

Example:

In the example below, hypot() method returns sqrt(x2 +y2).

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"hypot(3, 4) = ${hypot(3, 4)}"); 
    println(s"hypot(5, 12) = ${hypot(5, 12)}"); 
    println(s"hypot(8, 15) = ${hypot(8, 15)}");  
    println(s"hypot(8, -15) = ${hypot(8, -15)}"); 
    println(s"hypot(10, Double.NaN) = ${hypot(10, Double.NaN)}"); 
    println("hypot(10, Double.PositiveInfinity) = "
            + hypot(10, Double.PositiveInfinity)); 
    println("hypot(Double.PositiveInfinity, Double.NaN) = "
            + hypot(Double.PositiveInfinity, Double.NaN));             
  }
}

The output of the above code will be:

hypot(3, 4) = 5.0
hypot(5, 12) = 13.0
hypot(8, 15) = 17.0
hypot(8, -15) = 17.0
hypot(10, Double.NaN) = NaN
hypot(10, Double.PositiveInfinity) = Infinity
hypot(Double.PositiveInfinity, Double.NaN) = Infinity

❮ Scala - Math Methods